Refactoring

This commit is contained in:
syuilo 2019-01-23 19:25:36 +09:00
parent 67dacb7725
commit 2f0b75a882
No known key found for this signature in database
GPG key ID: BDC4C49D06AB9D69

View file

@ -3,10 +3,9 @@ import { default as User, IUser } from '../../models/user';
import AccessToken from '../../models/access-token'; import AccessToken from '../../models/access-token';
import isNativeToken from './common/is-native-token'; import isNativeToken from './common/is-native-token';
export default (token: string) => new Promise<[IUser, IApp]>(async (resolve, reject) => { export default async (token: string): Promise<[IUser, IApp]> => {
if (token == null) { if (token == null) {
resolve([null, null]); return [null, null];
return;
} }
if (isNativeToken(token)) { if (isNativeToken(token)) {
@ -15,17 +14,17 @@ export default (token: string) => new Promise<[IUser, IApp]>(async (resolve, rej
.findOne({ token }); .findOne({ token });
if (user === null) { if (user === null) {
return reject('user not found'); throw 'user not found';
} }
resolve([user, null]); return [user, null];
} else { } else {
const accessToken = await AccessToken.findOne({ const accessToken = await AccessToken.findOne({
hash: token.toLowerCase() hash: token.toLowerCase()
}); });
if (accessToken === null) { if (accessToken === null) {
return reject('invalid signature'); throw 'invalid signature';
} }
const app = await App const app = await App
@ -34,6 +33,6 @@ export default (token: string) => new Promise<[IUser, IApp]>(async (resolve, rej
const user = await User const user = await User
.findOne({ _id: accessToken.userId }); .findOne({ _id: accessToken.userId });
resolve([user, app]); return [user, app];
} }
}); };