2017-10-08 145 views
0

我試圖設置與電子郵件和密碼的身份驗證。下面是signup.ejs部分碼:護照本地策略在郵遞員但在瀏覽器中工作

<% if (message.length > 0) { %> 
     <div class="alert alert-danger"><%= message %></div> 
    <% } %> 

    <!-- LOGIN FORM --> 
    <form action="/signup" method="post"> 
     <div class="form-group"> 
      <label>Email</label> 
      <input type="text" class="form-control" name="email"> 
     </div> 
     <div class="form-group"> 
      <label>Password</label> 
      <input type="password" class="form-control" name="password"> 
     </div> 

     <button type="submit" class="btn btn-warning btn-lg">Signup</button> 
    </form> 

形式員額/signup,這是我的快遞路線:

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

這是我使用的本地通行證策略:

passport.use(
     'local-signup', 
     new LocalStrategy(
      { 
       // by default, local strategy uses username and password, we will override with email 
       usernameField: 'email', 
       passwordField: 'password', 
       passReqToCallback: true // allows us to pass back the entire request to the callback 
      }, 
      function(req, email, password, done) { 
       // asynchronous 
       // User.findOne wont fire unless data is sent back 
       process.nextTick(function() { 
        // find a user whose email is the same as the forms email 
        // we are checking to see if the user trying to login already exists 
        User.findOne({ 'local.email': email }, function(err, user) { 
         // if there are any errors, return the error 
         if (err) return done(err) 

         // check to see if theres already a user with that email 
         if (user) { 
          return done(
           null, 
           false, 
           req.flash('signupMessage', 'That email is already taken.') 
          ) 
         } else { 
          // if there is no user with that email 
          // create the user 
          var newUser = new User() 

          // set the user's local credentials 
          newUser.local.email = email 
          newUser.local.password = newUser.generateHash(password) 

          // save the user 
          newUser.save(function(err) { 
           if (err) throw err 
           return done(null, newUser) 
          }) 
         } 
        }) 
       }) 
      } 
     ) 
    ) 

這是我的Github repo的完整代碼鏈接。

我遇到的問題是,當我在郵件和密碼請求中使用郵遞員發送郵件請求時,事實證明我很好,並且我已成功重定向到配置文件路由。但是,當我嘗試通過在我的頁面上填寫表單登錄時,我將重定向回「/註冊」路線。有人可以幫助解決這個問題嗎?

+0

通用提示:檢查並查看策略的驗證處理程序是否正在調用。檢查'email'和'password'是否包含期望的值。檢查並查看是否有任何錯誤發生。 – robertklep

回答

0

我找到了答案。原因是該表單未將電子郵件和密碼值傳遞給req.body。我將app.use(bodyParser.json())更改爲app.use(bodyParser.urlencoded({ extended: true }))並開始工作。

相關問題