Passport无线电选项按钮重定向到注册页面

时间:2014-03-03 02:41:44

标签: javascript angularjs node.js passport.js mean-stack

如果用户选择了“否”单选按钮,如何重定向到/注册?

ROUTE

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

1 个答案:

答案 0 :(得分:0)

这不是节点/护照问题,而是一个有角度的问题。

我猜您提交的内容有角度(例如,$http.post('/users/session',...),或者您有<form action='/users/session' ...>

你需要告诉angular听取单选按钮(例如,ng-change或类似的东西)。当用户在无密码和密码之间切换时,您应该将您的网址更改为/ signup。

所以,根本不需要打服务器。如果你想继续,你可以在那里添加另一个中间件?类似的东西:

app.post('/users/session', function(req, res, next) { if (req.body.youroptionselected) return res.redirect('/somewhere'); next(); }, .... other handler here)

这不会重定向到POST /注册 - 重定向永远不会。

你的Angular控制器(客户端)示例是这样的:

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