Вопрос

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?

Это было полезно?

Решение

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.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top