2016-03-01 89 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