This repository has been archived on 2022-08-14. You can view files and clone it, but cannot push or open issues or pull requests.
2019-04-18 02:11:23 +00:00
|
|
|
// @flow
|
|
|
|
import JWT from 'jsonwebtoken';
|
|
|
|
import { AuthenticationError } from '../errors';
|
|
|
|
import { User } from '../models';
|
|
|
|
|
|
|
|
export async function getUserForJWT(token: string) {
|
|
|
|
let payload;
|
|
|
|
try {
|
|
|
|
payload = JWT.decode(token);
|
|
|
|
} catch (err) {
|
|
|
|
throw new AuthenticationError('Unable to decode JWT token');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!payload) throw new AuthenticationError('Invalid token');
|
|
|
|
|
2019-06-23 22:49:45 +00:00
|
|
|
const user = await User.findByPk(payload.id);
|
2019-04-18 02:11:23 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
JWT.verify(token, user.jwtSecret);
|
|
|
|
} catch (err) {
|
|
|
|
throw new AuthenticationError('Invalid token');
|
|
|
|
}
|
|
|
|
|
|
|
|
return user;
|
|
|
|
}
|