2016-03-01 120 views
0

我有注册一个多步骤Angular.js形式保存所有用户输入:

$scope.user = {}; 

然后我用ng-submit像这样:

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

的注册功能,然后进行POST请求我的Node.js API的/signup航线上,像这样:

$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中。

的问题:

我使用Passport.Js服务器端&注册用户进行身份验证。我正在使用本地注册策略。

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

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

问题是,在这两个成功注册&不成功不重定向根据所提供的路由用户。它将响应数据中的实际重定向页面代码发回。所以在成功注册时,如果不成功,它应该将用户发送到&主页面

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

回答

0

不舒服,但它可能有帮助。

在角:

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

在的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: '/' }); 
     } 
    ); 
+0

非常感谢你的回答。这工作完美。你知道这种方法涉及的任何风险还是安全的? – Skywalker

+0

没有任何风险:这是通过xhr请求认证的标准方式 –

+0

感谢您的帮助。已经坚持了几天! – Skywalker