blob: 519ed773889b2d7f62e4d38447e93224a629a26e (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
import isNativeToken from './common/is-native-token';
import { User } from '../../models/entities/user';
import { App } from '../../models/entities/app';
import { Users, AccessTokens, Apps } from '../../models';
export default async (token: string): Promise<[User | null | undefined, App | null | undefined]> => {
if (token == null) {
return [null, null];
}
if (isNativeToken(token)) {
// Fetch user
const user = await Users
.findOne({ token });
if (user == null) {
throw new Error('user not found');
}
return [user, null];
} else {
const accessToken = await AccessTokens.findOne({
hash: token.toLowerCase()
});
if (accessToken == null) {
throw new Error('invalid signature');
}
const app = await Apps
.findOne(accessToken.appId);
const user = await Users
.findOne({
id: accessToken.userId // findOne(accessToken.userId) のように書かないのは後方互換性のため
});
return [user, app];
}
};
|