mirror of
https://github.com/misskey-dev/misskey.git
synced 2024-12-22 06:35:10 +01:00
39 lines
893 B
TypeScript
39 lines
893 B
TypeScript
import App, { IApp } from '../../models/app';
|
|
import { default as User, IUser } from '../../models/user';
|
|
import AccessToken from '../../models/access-token';
|
|
import isNativeToken from './common/is-native-token';
|
|
|
|
export default (token: string) => new Promise<[IUser, IApp]>(async (resolve, reject) => {
|
|
if (token == null) {
|
|
resolve([null, null]);
|
|
return;
|
|
}
|
|
|
|
if (isNativeToken(token)) {
|
|
// Fetch user
|
|
const user: IUser = await User
|
|
.findOne({ token });
|
|
|
|
if (user === null) {
|
|
return reject('user not found');
|
|
}
|
|
|
|
resolve([user, null]);
|
|
} else {
|
|
const accessToken = await AccessToken.findOne({
|
|
hash: token.toLowerCase()
|
|
});
|
|
|
|
if (accessToken === null) {
|
|
return reject('invalid signature');
|
|
}
|
|
|
|
const app = await App
|
|
.findOne({ _id: accessToken.appId });
|
|
|
|
const user = await User
|
|
.findOne({ _id: accessToken.userId });
|
|
|
|
resolve([user, app]);
|
|
}
|
|
});
|