-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.js
More file actions
29 lines (28 loc) · 891 Bytes
/
auth.js
File metadata and controls
29 lines (28 loc) · 891 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
const passport = require('passport');
const {Strategy, ExtractJwt} = require('passport-jwt');
const config = require('./config');
module.exports = app => {
const Users = app.models.users;
const { jwt } = config;
const params = {
secretOrKey: jwt.secret,
jwtFromRequest: ExtractJwt.fromHeader("authorization")
};
const strategy = new Strategy(params, (payload, done) => {
Users.findByPk(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", jwt.options)
};
}