简体   繁体   English

Passport单选按钮重定向到注册页面

[英]Passport radio option button redirecting to signup page

If the user has the 'No' radio option button selected, how do I redirect to /signup? 如果用户选择了“否”单选按钮,则如何重定向到/ signup?

ROUTE 路线

// app/routes/users.js

app.post('/users/session', passport.authenticate('local', {
    failureRedirect: '/signin',
    failureFlash: true
}), users.session);

VIEW 视图

<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 CONTROLLER

// controllers/index.js

'use strict';

angular.module('mean.system').controller('IndexController', ['$scope', 'Global', function ($scope, Global) {
    $scope.global = Global;
}]);

This is not so much node/passport issue, it's an angular one. 这不是节点/护照问题,而是一个有角度的问题。

I guess that you submit this with either angular (like, $http.post('/users/session',...) or you have a <form action='/users/session' ...> . 我想您可以使用有角度的提交(例如$http.post('/users/session',...)或使用<form action='/users/session' ...>提交。

You need to tell angular that listens for the radio buttons (like, ng-change or something similar). 您需要告诉angular侦听单选按钮(例如ng-change或类似的东西)。 When the user switches between no password and password, you should change your url to /signup. 当用户在无密码和无密码之间切换时,应将URL更改为/ signup。

So, no need to hit the server at all there. 因此,根本不需要在那里打服务器。 If you do want to proceed, you could add another middleware in there? 如果确实要继续,可以在其中添加另一个中间件吗? Something like: 就像是:

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

This would not redirect to POST /signup though - redirects never do. 但是,这不会重定向到POST / signup-重定向从不执行。

Your Angular controller (client-side) example would be something like this: 您的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) ...
    }
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM