-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.js
33 lines (32 loc) · 845 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
import passport from 'passport';
import { Strategy, ExtractJwt } from 'passport-jwt';
module.exports = app => {
const Users = app.db.models.Users;
const cfg = app.libs.config;
const params = {
secretOrKey: cfg.jwtSecret,
jwtFromRequest: ExtractJwt.fromAuthHeader(),
};
const strategy = new Strategy(params, (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: () => {
return passport.initialize();
},
authenticate: () => {
return passport.authenticate('jwt', cfg.jwtSession);
},
};
};