-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.ts
More file actions
31 lines (28 loc) · 836 Bytes
/
auth.ts
File metadata and controls
31 lines (28 loc) · 836 Bytes
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
import * as passport from 'passport';
import {User} from './modules/User/service';
import { Strategy, ExtractJwt } from 'passport-jwt';
const config = require('./config/env/config')();
export default function AuthConfig () {
const UserService = new User();
let opts = {
secretOrKey: config.secret,
jwtFromRequest: ExtractJwt.fromAuthHeader()
};
passport.use(new Strategy(opts, (jwtPayload, done) => {
UserService.getById(jwtPayload.id)
.then(user => {
if(user) {
return done(null, {
id: user.id,
email: user.email
});
}
return done(null, false);
})
.catch(error => done(error, null));
}));
return {
initialize: () => passport.initialize(),
authenticate: () => passport.authenticate('jwt', {session: false}),
};
}