-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpassportConfig.js
37 lines (34 loc) · 1.31 KB
/
passportConfig.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
const localStartegy = require('passport-local').Strategy;
const userSchema = require('./modal/userDetails');
const bcrypt = require('bcrypt');
function passportIntialise(passport) {
const authenticateUser = async (email, psw, done) => {
userSchema.find({ emailId: email }, (err, user) => {
if (err) throw err;
if (user.length == 0) {
return done(null, false, { message: 'No user with that email' })
}
try {
bcrypt.compare(psw, user[0].password, function (err, isMatch) {
if (err) throw err;
if (isMatch) {
return done(null, user);
// res.redirect('/');
} else {
return done(null, false, { message: 'Invalid Password' });
}
});
} catch (e) {
return done(e)
}
});
}
passport.use(new localStartegy({ usernameField: 'email', passwordField: 'psw' }, authenticateUser));
passport.serializeUser((user, done) => done(null, user[0].id))
passport.deserializeUser((id, done) => {
userSchema.findById(id, function (err, user) {
return done(err, user);
});
})
}
module.exports = passportIntialise;