-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathauthenticate.js
33 lines (28 loc) · 975 Bytes
/
authenticate.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
// required modules
const LocalStrategy = require('passport-local').Strategy;
const User = require('./Schemas/user');
// module exports
module.exports = (passport) => {
passport.use('login', new LocalStrategy((username, password, done) => {
// verify credentials
User.findOne({username: username, password: password}).then(data => {
// if not credentials verified
if (!data) {
return done(null, false, {message: "Incorrect Username or Password"});
} else { // if credentials verified
return done(null, {
username: data.username
});
}
}).catch(err=> {
return done(err);
});
}));
passport.serializeUser((user, done) => {
done(null, user.username);
console.log(user);
});
passport.deserializeUser((username, done) => {
done(null, {username: username});
});
}