-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.js
32 lines (28 loc) · 852 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
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"),
};
passport.use(
new Strategy(params, async (payload, done) => {
try {
const { id } = payload;
const attributes = ["id", "email"];
const options = { attributes };
const user = await Users.findByPk(id, options);
done(null, user);
} catch (err) {
done(err, null);
}
})
);
return {
initialize: () => passport.initialize(),
authenticate: () => passport.authenticate("jwt", { session: false }),
};
};