-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvalidation.js
51 lines (44 loc) · 1.38 KB
/
validation.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
// const express = require('express');
// const router = express.Router();
const passport = require("passport");
const LocalStrategy = require("passport-local").Strategy;
const User = require("./models/user");
// Telling passport we want to use a Local Strategy. In other words, we want login with a username/email and password
passport.use(new LocalStrategy(
// Our user will sign in using an email, rather than a "username"
{
usernameField: "email",
passwordField: "password",
passReqToCallback:true
},
function(req,email, password, done) {
console.log(email)
// When a user tries to sign in this code runs
User.findOne({email:email})
.then(function(user) {
// If there's no user with the given email
if (!user) {
return done(null, false, {
message: "Incorrect email."
});
}
// If there is a user with the given email, but the password the user gives us is incorrect
else if (!user.checkPassword(password)) {
return done(null, false, {
message: "Incorrect password."
});
}
// If none of the above, return the user
return done(null, user);
});
}
));
passport.serializeUser(function(user, done) {
done(null, user.id);
});
passport.deserializeUser(function(user,done) {
User.findById(user.id, function(err,user){
done(err, user);
});
});
module.exports = passport;