2017-10-08 142 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 }))并开始工作。

相关问题