Passportjs (after successful login)

泄露秘密 提交于 2019-12-11 22:34:18

问题


I have the following code borrowed from this excellent resource: http://scotch.io/tutorials/javascript/easy-node-authentication-setup-and-local#handling-signup/registration

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
}));

but I don't want to use the successRedirect or failureRedirect as my front end is an Angularjs app. If the signup was successful I want to tell my Angularjs front end but how can I do this?


回答1:


Instead of doing:

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
}));

You can do something like:

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);
});

For more details go to http://passportjs.org/guide/authenticate/ and look at the Custom Callback section at the bottom.



来源:https://stackoverflow.com/questions/23477926/passportjs-after-successful-login

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!