-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.js
39 lines (34 loc) · 1 KB
/
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
35
36
37
38
39
const passport = require('passport');
const { Strategy, ExtractJwt } = require('passport-jwt')
module.exports = app => {
const Users = app.db.models.Users;
const config = app.libs.config;
const params = {
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
secretOrKey: config.jwtSecret
};
const strategy = new Strategy(params, async (payload, done) => {
try {
const user = await Users.findById(payload.id);
if (user) {
return done(null, {
id: user.id,
email: user.email
});
} else {
return done(null, false);
}
} catch (error) {
return done(error, null);
}
});
passport.use(strategy);
return {
initialize: () => {
return passport.initialize();
},
authenticate: () => {
return passport.authenticate('jwt', config.jwtSession);
}
}
};