-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathauth.js
34 lines (27 loc) · 828 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
31
32
33
34
import passport from 'passport';
import { Strategy, ExtractJwt } from 'passport-jwt';
module.exports = app => {
const User = app.db.models.User;
const config = app.config;
const params = {
secretOrKey: config.secret,
jwtFromRequest: ExtractJwt.fromAuthHeader(),
};
const strategy = new Strategy(params, (payload, done) => {
User.findById(payload.id)
.then(user => {
if (user) {
return done(null, user);
}
return done(null, false);
})
.catch(err => done(err, null));
});
passport.use(strategy);
passport.serializeUser((user, done) => done(null, user));
passport.deserializeUser((user, done) => done(null, user));
return {
init: () => passport.initialize(),
authenticate: () => passport.authenticate('jwt', config.secret),
};
};