$http POST &Passport.Js 重定向

问题描述:

我有一个用于注册的多步 Angular.js 表单,可将​​所有用户输入保存到:

I have a multi-step Angular.js form for signup that saves all the users input into:

$scope.user = {};

然后我像这样使用 ng-submit :

Then I use ng-submit like so:

<form class="css-form" name="myForm" ng-submit="signup()" novalidate>

然后,Signup 函数在 /signup 路由上向我的 Node.JS API 发出 POST 请求,如下所示:

The Signup function then makes a POST request to my Node.JS API on the /signup route like so:

$scope.signup = function(){

  return $http.post('/signup',$scope.user).
    success(function(data, status, headers, config) {
        // this callback will be called asynchronously
        // when the response is available
        console.log("this is the response data " + data);
    }).
    error(function(data, status, headers, config) {
        // called asynchronously if an error occurs
        // or server returns response with an error status.
        console.log("this is the error " + data);
    });
};

注意:以上成功注册了一个新用户并将数据保存在MongoDB中.

Note: The above successfully registers a new user and saves the data within MongoDB.

问题:

我使用 Passport.Js 服务器端来验证 &注册用户.我正在使用本地注册策略.

I am using Passport.Js server side to authenticate & register users. I am using the local-signup strategy.

问题是,如果用户注册成功,他们应该被自动重定向到特定页面,如果他们不成功也一样.以下是我的服务器端代码:

The problem is that if the user is successfully registered they should be automatically redirected to a specific page and the same if they are unsuccessful. Below is my server side code:

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

问题是在 BOTH 上成功注册 &如果不成功,它不会根据提供的路线重定向用户.它在响应数据中发回实际的重定向页面代码.因此,在成功注册后,它应该将用户发送到配置文件 &如果不成功,则转到主页

我似乎无法解决这个问题,也许我的整个技术都是错误的.任何帮助将不胜感激.

I can't seem to solve this, maybe my whole technique is wrong. Any help will be appreciated.

不是 shure,但可能会有所帮助.

Not shure, but it might help.

在角度:

$scope.signup = function(){
  return $http.post('/signup',$scope.user)
    .then(function(data) {
        location.replace(data.redirectUrl); // use appropriate function to update route
    });
};

在 nodejs 中:

In nodejs:

   app.post('/signup',
      passport.authenticate('local-signup', { failWithError: true }),
      function(req, res, next) {
        // success
        return res.json({ redirectUrl: '/profile' });
      },
      function(err, req, res, next) {
        // error
        return res.json({ redirectUrl: '/' });
      }
   );