-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.js
30 lines (26 loc) · 798 Bytes
/
auth.js
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
import passport from 'passport';
import { Strategy, ExtractJwt } from 'passport-jwt';
export default (app) => {
const Users = app.datasource.models.Users;
const opts = {};
opts.secretOrKey = app.config.jwtSecret;
opts.jwtFromRequest = ExtractJwt.fromAuthHeader();
const strategy = new Strategy(opts, (payload, done) => {
Users.findById(payload.id)
.then((user) => {
if (user) {
return done(null, {
id: user.id,
email: user.email,
});
}
return done(null, false);
})
.catch(error => done(error, null));
});
passport.use(strategy);
return {
initialize: () => passport.initialize(),
authenticate: () => passport.authenticate('jwt', app.config.jwtSession),
};
};