mirror of
https://iceshrimp.dev/iceshrimp/iceshrimp
synced 2024-12-24 19:48:08 +09:00
8d94a6a8df
* wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip
39 lines
846 B
TypeScript
39 lines
846 B
TypeScript
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 'user not found';
|
|
}
|
|
|
|
return [user, null];
|
|
} else {
|
|
const accessToken = await AccessTokens.findOne({
|
|
hash: token.toLowerCase()
|
|
});
|
|
|
|
if (accessToken == null) {
|
|
throw 'invalid signature';
|
|
}
|
|
|
|
const app = await Apps
|
|
.findOne(accessToken.appId);
|
|
|
|
const user = await Users
|
|
.findOne(accessToken.userId);
|
|
|
|
return [user, app];
|
|
}
|
|
};
|