Passport LocaStrategy注册重定向问题

时间:2015-04-15 14:42:51

标签: javascript express mongoose passport.js mean-stack

我试图在Passport注册后重定向用户

passport.use('local-signup', new LocalStrategy({
usernameField : 'username',
passwordField : 'password',
passReqToCallback : true},
function(req, username, password, done) {

    // asynchronous
    // User.findOne wont fire unless data is sent back
    process.nextTick(function() {

    // find a user whose username is the same as the forms username
    User.findOne({ username :  username }, function(err, user) {
        // if there are any errors, return the error
        if (err)
            return done(err);
        if (user) {
            return done(null, false, req.flash('signupMessage', 'Username is already taken.'));
        } else {
            var newUser = new User();
            newUser.username = username;
            newUser.password = password;
            newUser.email = req.body.email;
            newUser.save(function(err) { // create user
                if (err) {
                    return next(err);
                } else {
                    var site = new Site({user : newUser.username, siteTitle: newUser.username});
                    site.save(function(err) { // create website
                        if (err) {
                            return next(err);
                        } else {
                        newUser.sites.push(site); // push site'id in sites field in user
                        newUser.save(); // save user after site'id has been push
                        };
                    });
                };
            res.redirect('users/' + username);
            });
            ...

我得 ReferenceError:res未定义

当我开始使用Mean dev时,任何帮助都会受到赞赏。

非常感谢

1 个答案:

答案 0 :(得分:1)

您不应该在这里处理重定向 - 策略的功能是确定用户是否经过身份验证。

然后可以在中间件中完成重定向:

app.post('/login', passport.authenticate('local', {
                     successRedirect: '/',
                     failureRedirect: '/login'
}));

http://passportjs.org/guide/authenticate/