Node.js PassportJS在保存时不重定向
我试图重定向一个用户,一旦他注册,或者如果用户名已经存在于数据库中,该功能工作正常,但我不能重定向,我完全不知道下一步该怎么做 这是我的护照档案Node.js PassportJS在保存时不重定向,node.js,express,passport.js,passport-local,Node.js,Express,Passport.js,Passport Local,我试图重定向一个用户,一旦他注册,或者如果用户名已经存在于数据库中,该功能工作正常,但我不能重定向,我完全不知道下一步该怎么做 这是我的护照档案 var LocalStrategy = require('passport-local').Strategy; var User = require('./models/user'); module.exports = function(passport) { passport.serializeUser(func
var LocalStrategy = require('passport-local').Strategy;
var User = require('./models/user');
module.exports = function(passport) {
passport.serializeUser(function(user, done) {
done(null, user);
});
// used to deserialize the user
passport.deserializeUser(function(user, done) {
done(null, user);
});
passport.use('local-signup', 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() {
// find a user whose email is the same as the forms email
// we are checking to see if the user trying to login already exists
User.findOne({ 'email' : email }, function(err, user) {
// if there are any errors, return the error
if (err) {
return done(err);
}
if(user) {
if(user.validPassword(password)) {
return done(null, user);
} else {
return done(null, true, req.flash('signupMessage', 'That email is already in DB.'));
}
} else {
var newUser = new User();
newUser.email = email;
newUser.password = newUser.generateHash(password);
newUser.save(function(err) {
if (err)
return done(err);
return done(null, newUser);
});
}
});
});
}));
}
我的路线呢
app.post('/signup',passport.authenticate('local-signup', {
successRedirect : '/timeslot', // redirect to the secure profile section
failureRedirect : '/', // redirect back to the signup page if there is an error
failureFlash : true // allow flash messages
}));
我也试过这样做
app.post('/signup',function(req, res, next) {
passport.authenticate('local-signup', function(err, user, info) {
if (user) {
res.redirect('/timeslot');
}
else res.redirect('/');
})(req, res, next);
});
但它根本不起作用。我非常想解决这个问题,但不知道如何解决。我在passport重定向方面遇到了同样的问题。因此,我的代码也开始工作了
app.get("/auth/facebook/callback", passport.authenticate("facebook", { failureRedirect: config.loginPage }), function(req,res) {
res.redirect(config.redirectAfterLogin);
});