1

ユーザーが [いいえ] ラジオ オプション ボタンを選択している場合、/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>

コントローラ

// controllers/index.js

'use strict';

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

1 に答える 1

0

これはノード/パスポートの問題ではなく、角度の問題です。

これをAngular(like、$http.post('/users/session',...)または<form action='/users/session' ...>.

ラジオ ボタン (ng-change など) をリッスンする angular を指定する必要があります。ユーザーがパスワードなしとパスワードを切り替えるときは、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 コントローラー (クライアント側) の例は次のようになります。

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) ...
    }
})
于 2014-03-03T02:53:14.983 に答える