node.js - Passport.js always executing "failureRedirect" -
first of all, i'm new passport.js maybe ends being naïve question. have strategy signup:
// configuring passport var passport = require('passport'); var expresssession = require('express-session'); var localstrategy = require('passport-local').strategy; var facebookstrategy = require('passport-facebook'); app.use(expresssession({secret: 'mysecretkey'})); app.use(passport.initialize()); app.use(passport.session()); app.use(flash()); //[...] passport.use('signup', new localstrategy({ name : 'name', password : 'password', email : 'email', passreqtocallback : true }, function(req, username, password, done) { findorcreateuser = function(){ // find user in mongo provided username user.findone({'name':username},function(err, user) { // in case of error return if (err){ console.log('error in signup: '+err); return done(err); } // exists if (user) { console.log('user exists'); return done(null, false, req.flash('message','user exists')); } else { // if there no user email // create user var newuser = new user(); // set user's local credentials newuser.name = name; newuser.password = createhash(password); newuser.email = req.param('email'); /*newuser.firstname = req.param('firstname'); newuser.lastname = req.param('lastname');*/ // save user newuser.save(function(err) { if (err){ console.log('error in saving user: '+err); throw err; } console.log('user registration succesful'); return done(null, newuser); }); } }); }; // delay execution of findorcreateuser , execute // method in next tick of event loop process.nexttick(findorcreateuser); }) );
and how deal post
on /register
:
/* handle registration post */ app.post('/register', passport.authenticate('signup', { successredirect: '/', failureredirect: '/failure_registration', failureflash : true }));
this always brings me failureredirect
link, instead of success. input data correct, , i'm using different user , mail register. i'm sorry if stupid question, don't understand why never goes successredirect
.
thanks.
edit: added suggestion , corrections @robertklep, still not working. i'd point out no error triggered, nor log printed.
edit2: serialization/deserialization functions:
passport.serializeuser(function(user, done) { done(null, user.id); }); passport.deserializeuser(function(id, done) { user.findbyid(id, function (err, user) { done(err, user); }); });
i had same problem, failureredirect being executed
first diagnose, used custom callback method http://passportjs.org/docs/authenticate
app.post('/sign_in', function(req, res, next) { console.log(req.url); passport.authenticate('local-login', function(err, user, info) { console.log("authenticate"); console.log(err); console.log(user); console.log(info); })(req, res, next); });
then see hidden error was
authenticate null false { message: 'missing credentials' }
which became easy me diagnoise, sending json in request rather form fields
fixed changing
app.use(bodyparser.urlencoded({ extended: true }));
to
app.use(bodyparser.json());
Comments
Post a Comment