2017-07-31 79 views
1

我試圖將谷歌的reCAPTCHA我的註冊表格的已經有Passport身份驗證就可以了代碼後端看起來是這樣的:護照身份驗證與谷歌的reCAPTCHA

app.get('/signup', function(req, res) { 
     // render the page and pass in any flash data if it exists 
     res.render('signup.ejs', { 
      message : req.flash('signupMessage') 
     }); 
    }); 

    app.post('/signup', passport.authenticate('local-signup', { 
     successRedirect : '/app', // redirect to the secure chat section 
     failureRedirect : '/signup', // redirect back to the signup page if there is an error 
     failureFlash : true // allow flash messages 
    })); 

這是我的谷歌recaptha代碼:

app.get('/signup', recaptcha.middleware.render, function(req, res) { 
     // render the page and pass in any flash data if it exists 
     res.render('signup.ejs', { 
      message : req.flash('signupMessage'), 
      captcha : req.recaptcha 
     }); 
    }); 

    app.post('/signup', recaptcha.middleware.verify, function(req, res){ 
     if (!req.recaptcha.error) { 
      //if recaptcha is correct do somthing 
     }else { 
      req.flash('signupMessage','reCAPTCHA Incorrect'); 
      res.redirect('/signup'); 
     } 
    }); 

兩個獨立工作,但是當我將它們結合起來是這樣的:

app.get('/signup', isNotLoggedIn, recaptcha.middleware.render, function(req, res) { 
     // render the page and pass in any flash data if it exists 
     res.render('signup.ejs', { 
      message : req.flash('signupMessage'), 
      captcha : req.recaptcha 
     }); 
    }); 

    app.post('/signup', recaptcha.middleware.verify, function(req, res){ 
     if (!req.recaptcha.error) { 
      passport.authenticate('local-signup', { 
       successRedirect : '/app', 
       failureRedirect : '/signup', 
       failureFlash : true 
      }); 
     }else { 
      req.flash('signupMessage','reCAPTCHA Incorrect'); 
      res.redirect('/signup'); 
     } 
    }); 

或者這樣:

app.get('/signup', isNotLoggedIn, recaptcha.middleware.render, function(req, res) { 
     // render the page and pass in any flash data if it exists 
     res.render('signup.ejs', { 
      message : req.flash('signupMessage'), 
      captcha : req.recaptcha 
     }); 
    }); 

    app.post('/signup', recaptcha.middleware.verify, function(req, res){ 
     if (req.recaptcha.error) { 
      req.flash('signupMessage','reCAPTCHA Incorrect'); 
      res.redirect('/signup'); 
     } 
    }, 
    passport.authenticate('local-signup', { 
     successRedirect : '/app', // redirect to the secure chat section 
     failureRedirect : '/signup', // redirect back to the signup page if there is an error 
     failureFlash : true // allow flash messages 
    })); 

頁面停留在加載並沒有什麼happends。

回答

1

所以我想通了!

訣竅在於使用next();如果驗證碼是正確的功能,所以代碼最終是這樣的:

app.get('/signup', isNotLoggedIn, recaptcha.middleware.render, function(req, res) { 
     // render the page and pass in any flash data if it exists 
     res.render('signup.ejs', { 
      message : req.flash('signupMessage'), 
      captcha : req.recaptcha 
     }); 
    }); 

    app.post('/signup', recaptcha.middleware.verify, captchaVerification, passport.authenticate('local-signup', { 
     successRedirect : '/app', // redirect to the secure chat section 
     failureRedirect : '/signup', // redirect back to the signup page if there is an error 
     failureFlash : true // allow flash messages 
    })); 

function captchaVerification(req, res, next) { 
    if (req.recaptcha.error) { 
     req.flash('signupMessage','reCAPTCHA Incorrect'); 
     res.redirect('/signup'); 
    } else { 
     return next(); 
    } 
} 

並且那你怎麼谷歌驗證碼集成到您的NodeJS,快遞,護照,後端