Setup passport + jwt strategy

This commit is contained in:
rijkvanzanten
2020-06-23 14:25:55 -04:00
parent e1af1e4499
commit de53aa01a7
2 changed files with 29 additions and 0 deletions

6
src/auth/passport.ts Normal file
View File

@@ -0,0 +1,6 @@
import passport from 'passport';
import JWTStrategy from './strategies/jwt';
passport.use(JWTStrategy);
export default passport;

View File

@@ -0,0 +1,23 @@
import { Strategy, ExtractJwt } from 'passport-jwt';
import database from '../../database';
import APIError, { ErrorCode } from '../../error';
const JWTStrategy = new Strategy(
{
secretOrKey: process.env.SECRET,
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
},
async (payload, done) => {
// This is just an extra verification to make sure the user actually exists when you're trying to
// use it
const users = await database.select('id').from('directus_users').where({ id: payload.id });
if (users && users[0]) {
return done(null, users[0]);
}
return done(new APIError(ErrorCode.USER_NOT_FOUND, 'User not found.'));
}
);
export default JWTStrategy;