如果用户选择了“否”单选按钮,我如何重定向到/signup?
路线
// app/routes/users.js
app.post('/users/session', passport.authenticate('local', {
failureRedirect: '/signin',
failureFlash: true
}), users.session);
查看
<form action="/users/session" method="post">
<input ng-change="change()" ng-model="NotSureWhatToPutHere" type="radio" name="optionsRadios" id="optionsRadios1" value="option1" checked>
<label for="optionsRadios1">
No, I am a <b>new customer</b>.
</label>
<input type="radio" ng-model="NotSureWhatToPutHere" ng-change="change()" name="optionsRadios" id="optionsRadios2" value="option2">
<label class="control-label" for="optionsRadios2">
Yes, I have a password:
</label>
<button type="submit">Sign in</button>
</form>
Controller
// controllers/index.js
'use strict';
angular.module('mean.system').controller('IndexController', ['$scope', 'Global', function ($scope, Global) {
$scope.global = Global;
}]);
最佳答案
这并不是 Node/Passport 问题,而是一个 Angular 问题。
我猜你是用 Angular 提交的(例如 $http.post('/users/session',...)
或者你有一个 <form action='/users/session' ...>
。
您需要告诉 Angular 监听单选按钮(例如 ng-change 或类似的东西)。当用户在无密码和密码之间切换时,您应该将 url 更改为/signup。
所以,根本不需要访问服务器。如果您确实想继续,您可以在其中添加另一个中间件吗?像这样的东西:
app.post('/users/session', function(req, res, next) {
if (req.body.youroptionselected) return res.redirect('/somewhere');
next();
}, .... other handler here)
.
这不会重定向到 POST/signup - 重定向永远不会。
您的 Angular Controller (客户端)示例将如下所示:
angular.service('loginService', function($http){
var service = {
signin: function(email, password){
$http.post('/users/session', {email: email, password: password})
.success(function(response){
// You're logged in here, you could do something like a redirect.
})
.catch(function(err){
console.log('Something wen\'t wrong, show this message or something.');
});
}
, signup: function(email) {
$http.post('/signup', {email: email})
.success(function(response){
// You've handled this in your backend, now on the client you're in.
})
.catch(function(err){
console.log('Something wen\'t wrong with signup., show this message or something.');
});
}
}
})
angular.controller('loginController', function(loginService){
// get a hold of the button here
$scope.buttonClickHandler = function(){
// if new user, use loginService.signup($scope.email)...
// else use loginService.signin($scope.email, $scope.password) ...
}
})
关于javascript - Passport 单选按钮重定向到注册页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22137768/