forked from jainaman1398/SastaBazaar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
passport.js
75 lines (69 loc) · 2.11 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
const passport=require('passport');
var User=require('./models/signin');
const LocalStrategy=require('passport-local').Strategy;
console.log("passport");
var mongoose=require('mongoose');
mongoose.connect('mongodb://localhost:27017/myapp');
passport.serializeUser(function(user, done) {
done(null, user.id);
});
passport.deserializeUser(function(id, done) {
User.findById(id, function(err, user) {
done(err, user);
});
});
passport.use('local.signup',new LocalStrategy({
usernameField:'Email',
passwordField:'password',
passReqToCallback:true
},function (req,Email, password, done) {
/* req.checkBody('Email', 'Invalid Email').notEmpty().isEmail();
req.checkBody('password', 'Invalid password').notEmpty().isLength({min:6});
var errors=req.validationErrors();
if(errors)
{
var messages=[];
errors.forEach(function (err,docs) {
messages.push(docs.msg);
});
return(done,null,req.flash('error',messages));
}*/
User.findOne({'Email':Email},function (err,user) {
if(user){
return done(null,false,{message:"user is registered already"}); //msg
}
var newUser=new User();
newUser.Email=Email;
newUser.password=password;
newUser.save(function (err,result) {
if(err){
return done(err);}
return done(null,newUser);
});
});
})) ;
passport.use('local.signin',new LocalStrategy({
usernameField:'Email',
passwordField:'password',
passReqToCallback:true
},function (req,Email, password, done) {
User.findOne({'Email': Email}, function (err, user) {
if (!user) {
return done(null, false, {message: "user doesn't Exist"}); //msg
}
if(user.password!=password){
return done(null,false,{message:"Password is Incorrect"});
}
return done(null,user);
});
}));
/*
User.find(function (err,docs) {
console.log(docs);
})
let y='[email protected]';
let t=User.findOne({'Email':y},function (err,user) {
console.log(user.password);
})
*/
module.exports=passport;