Just Can't Get Passport.js To Work
Solution 1:
You're not setting the initial parameter of passport.use and passing the req back to the callback for example - from my code:
passport.use('local-login', new LocalStrategy({
// by default, local strategy uses username and password, we will override with email
usernameField : 'email',
passwordField : 'password',
passReqToCallback : true// allows us to pass back the entire request to the callback
},
function(req, email, password, done) {
// asynchronous// User.findOne wont fire unless data is sent back
process.nextTick(function() {
db.Account.findOne( { ...
You can then authenticate using:
passport.authenticate('local-login',function(err,user,info){
In your case you are calling passport.authenticate 'local' so your first parameter in passport.use should be 'local' so that it calls the correct strategy
Solution 2:
My passport strategies are almost identical, with one exception: serializing user to its _id
property. I suspect that might be the problem, since in your deserialize method you are using the _id directly, but passing done null, user
in serialize.
passport.serializeUser (user, done) ->
if user
done null, user?._id
Solution 3:
I think its even simpler than the answers posted here. you need if error?
and if not user?
with the ?
s. Without the ?s, it does a true/false check, but with the ?s it does a null check which is what you want.
Try this code:
Account = mongoose.model 'account'
passport.use 'local-login', new LocalStrategy (_username, password, done) ->
Account.findOne {username:_username}, (error, user) ->
if error?
done error,null
elseif not user?
done null, false, {message:'Incorrect username.'}
elsedone null,user
passport.serializeUser (user, done) ->
console.log 'serialize user'if user?
done null, user._id
passport.deserializeUser (id, done) ->
Account.findOne({_id:id}).exec (error, user) ->
if user?
returndone null, user
elsereturndone null, false
Post a Comment for "Just Can't Get Passport.js To Work"