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.
Files
outline/server/utils/jwt.js
Tom Moor 32f83311f6 chore: upgrade sequelize (#965)
* 0.18.0

* chore: Upgrade sequelize 4 -> 5

* fix: migrations v5 support

* fix: Majority of test failures

* fix: the rest of v5 tests
2019-06-23 15:49:45 -07:00

26 lines
575 B
JavaScript

// @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');
const user = await User.findByPk(payload.id);
try {
JWT.verify(token, user.jwtSecret);
} catch (err) {
throw new AuthenticationError('Invalid token');
}
return user;
}