-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
86 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
const passport = require("passport"); | ||
|
||
const LocalStrategy = require("passport-local").Strategy; | ||
const User = require('../models/user'); | ||
|
||
//authentication using passport: | ||
passport.use(new LocalStrategy({ | ||
usernameField: 'email' | ||
}, | ||
|
||
|
||
//here done is a callback function. | ||
function(email,password,done){ | ||
//find user and establish identity: | ||
User.findOne({email:email}, function(err,user){ | ||
if(err){ | ||
console.log("Error in finding user --> Passport"); | ||
return done(err); | ||
} | ||
|
||
//if user does not exist or password incorrect: | ||
if(!user || user.password!= password){ | ||
console.log("Invalid Credentials!!"); | ||
return done(err,false); //done takes 2 arguments. | ||
} | ||
|
||
return done(null,user); //if user is found then return done(). | ||
}); | ||
} | ||
|
||
|
||
)); | ||
|
||
//serializing user to decide which key is to be kept in the cookies | ||
|
||
passport.serializeUser(function(user,done){ | ||
done(null,user.id); //this encrypts the id. | ||
}); | ||
|
||
//deserializing user from thr key in the cookies | ||
|
||
passport.deserializeUser(function(id,done){ | ||
User.findById(id, function(err,user){ | ||
if(err){ | ||
console.log("Error in finding user --> Passport"); | ||
return done(err); | ||
} | ||
|
||
return done(null,user); | ||
}) | ||
}) |