2014-05-05 110 views
0

我有下面的代码从这个优秀的资源借:http://scotch.io/tutorials/javascript/easy-node-authentication-setup-and-local#handling-signup/registrationPassportjs(成功登录后)

app.post('/signup', passport.authenticate('local-signup', { 
    successRedirect : '/profile', // redirect to the secure profile section 
    failureRedirect : '/signup', // redirect back to the signup page if there is an error 
})); 

,但我不希望使用successRedirectfailureRedirect我的前端是一个Angularjs应用。如果注册成功,我想告诉我的Angularjs前端,但我该怎么做?

回答

2

而不是做的:

app.post('/signup', passport.authenticate('local-signup', { 
    successRedirect : '/profile', // redirect to the secure profile section 
    failureRedirect : '/signup', // redirect back to the signup page if there is an error 
})); 

你可以这样做:

app.post('/signup', function(req, res, next) { 
    passport.authenticate('local-signup', function(err, user, info) { 
    if (err) { 
     return next(err); // will generate a 500 error 
    } 
    // Generate a JSON response reflecting signup 
    if (! user) { 
     return res.send({ success : false, message : 'signupfailed' }); 
    } 
    return res.send({ success : true, message : 'signup succeeded' }); 
    })(req, res, next); 
}); 

有关更多信息,请到http://passportjs.org/guide/authenticate/,并期待在底部的自定义回调部分。

+0

感谢您 - 所以您认为我应该将auth逻辑移出我单独的passport.js文件并放入我的routes.js文件中? – tommyd456

+0

不需要'本地注册'是认证过程的命名策略。每篇文章passport.use('local-signup',新的LocalStrategy({...所以当你调用passport.authenticate('local-signup')它会寻找该策略并使用该过程设置。 –

+0

一个愚蠢的问题但是为什么在Passport中有一个注册认证过程?如果我自己制作系统,那么我们在注册之前不会对用户进行认证,它们在注册时是否记录下来? – tommyd456