-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpassport.js
59 lines (55 loc) · 1.6 KB
/
passport.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
const passport = require('passport');
const LocalStrategy = require('passport-local').Strategy;
const JwtStrategy = require('passport-jwt').Strategy;
const User = require('./models/User');
const bcrypt = require('bcrypt');
//here is the request for the token
const cookieExtractor = req => {
let token = null;
if(req && req.cookies){
token = req.cookies["access_token"];
}
return token;
}
//AUTHORIZATION - PROTECTING THE END POINTS
//extracting the jwt token from the request - coockieExtractor.
//use to verify to make sure it is good - secretOrKey.
//payload is the data
passport.use(new JwtStrategy({
jwtFromRequest : cookieExtractor,
secretOrKey : "to.gather"
}, (payload, done) => {
User.findById({_id : payload.sub}, (err,user) => {
if(err)
return done(err,false);
if(user)
return done(null,user);
else
//has no err & no user
return done(null,false)
});
}));
//WHEN WE ARE LOGGING IN
//here we are authinticating local strategy the user name & password
//done will be a function when we are done.
passport.use(
new LocalStrategy(
{
usernameField: 'user[email]',
passwordField: 'user[password]',
},
(email, password, done) => {
User.findOne({ email })
.then(async (user) => {
const valid = await bcrypt.compare(password, user.password);
if (!user || !valid) {
return done(null, false, {
errors: { 'email or password': 'is invalid' },
});
}
return done(null, user);
})
.catch(done);
}
)
);