护照本地策略在邮递员工作但不在浏览器

时间:2017-10-08 06:11:58

标签: node.js express passport.js passport-local

我尝试使用电子邮件和密码设置身份验证。这是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的完整代码的链接。

我遇到的问题是,当我在邮件中使用邮件和密码向邮递员发送邮件请求时,结果很好,我已成功重定向到个人资料路由。但是,当我尝试通过在我的页面上填写表单来登录时,我会被重定向回到&#39; /注册&#39;路线。有人可以帮忙解决这个问题吗?

1 个答案:

答案 0 :(得分:1)

我找到了答案。原因是表单没有将电子邮件和密码值传递给req.body。我将app.use(bodyParser.json())更改为app.use(bodyParser.urlencoded({ extended: true }))并开始工作。