2016-06-28 24 views
0
router.post('/register', function(req, res, next){ 
    var name   = req.body.name; 
    var email   = req.body.email; 
    var username  = req.body.username; 
    var password  = req.body.password; 
    var password2  = req.body.password2; 

    req.checkBody('name', 'Name field is required').notEmpty(); 
    req.checkBody('email', 'Email field is required').notEmpty(); 
    req.checkBody('email', 'Email must be a valid email address').isEmail(); 
    req.checkBody('username', 'Username field is required').notEmpty(); 
    req.checkBody('password', 'Password field is required').notEmpty(); 
    req.checkBody('password2', 'Passwords do not match').equals(req.body.password); 

    var errors = req.validationErrors(); 

    if(errors){ 
     res.render('register'); 

    } else { 

     passport.authenticate('local-register',{ 
      successRedirect: '/dashboard', 
      failureRedirect: '/register' 
     })(req, res, next) 
    } 
}); 

DBに関連するデータをポンピングするなど、ユーザー登録後にもう少し操作したいです。しかし、私はパスポートで成功のコールバックがどこにあるのか分かりません。パスポート登録成功コールバック

答えて

0

passport documentationを見ると、自分で応答をリダイレクトすることでこれを達成できるはずです。

例:

app.post('/login', 
    passport.authenticate('local'), 
    function(req, res) { 
    // If this function gets called, authentication was successful. 
    // `req.user` contains the authenticated user. 
    res.redirect('/users/' + req.user.username); 
    }); 

だからあなたのコードは、潜在的に

router.post('/register', function(req, res, next){ 
    var name   = req.body.name; 
    var email   = req.body.email; 
    var username  = req.body.username; 
    var password  = req.body.password; 
    var password2  = req.body.password2; 

    req.checkBody('name', 'Name field is required').notEmpty(); 
    req.checkBody('email', 'Email field is required').notEmpty(); 
    req.checkBody('email', 'Email must be a valid email address').isEmail(); 
    req.checkBody('username', 'Username field is required').notEmpty(); 
    req.checkBody('password', 'Password field is required').notEmpty(); 
    req.checkBody('password2', 'Passwords do not match').equals(req.body.password); 

    var errors = req.validationErrors(); 

    if(errors){ 
     res.render('register'); 
    } else { 
     passport.authenticate('local-register',{ })(req, res, function(returnCode) { 
      // Yay successfully registered user 
      // Do more stuff 
      console.log(returnCode);  // Check what this value is and redirect accordingly. 
      res.redirect('/dashboard/'); // How redirect can potentially be done 
     }) 
    } 
}); 

私は returnCode値がその意志ので、どうなるかわからない...このようなものに変更することができます戦略 local-registerの実装に依存します。

とにかく、returnCodeは、登録が成功したかどうかをチェックし、その値に応じてユーザーを適切にリダイレクトするために使用される可能性があります。

+0

returnCode = 'undefined' –

+0

@AliciaBrandon解決策で述べたように、' returnCode'の値は戦略が返すものによって決まります。 –

関連する問題