同时将护照集成到我的代码的登录表单中。一切正常,直到我打电话给password.authenticate在请求中,返回了400 Bad Request。我做错了什么?

战略

passport.use('local.login',new LocalStrategy({
    usernameField: 'Email',
    passwordField: 'Password',
},function (email,done) {
    Schema.Users.findOne({'Email': email},function (err,user) {
        if(err) {
            return done(err);
        }
        if(user==null) {
            return done(null,false,{ message: 'Incorrect username.' })
        }
        if(user.Password!==Password) {
            //console.log('Wrong password');
            return done(null,false,{ message: 'Wrong password' })
        }
        return done(null,user);
    })
}));

passport.serializeUser(function (user,done) {
    done(null,user.id);
});

passport.deserializeUser(function (id,done) {
    Schema.Users.findById(id,function (err,user) {
        done(err,user);
    })
});


内部login.js

router.post('/x',passport.authenticate('local.signup',{
    successRedirect: '/success',
    failureFlash: '/failure'
}));


app.js

let login = require('./login.js');
app.use('/login',login);


的HTML

<form action="http://localhost:8080/login/x" method="post">
    <div class="row">
        <div class="col s6">
            <label for = "email"></label>
            <input id = "Email" type="email" placeholder="Email" name="Email">
        </div>
        <div class="col s6">
            <label for = "Password"></label>
            <input id = "Password" type="Password" placeholder="Password" name="Password">
        </div>
    </div>
    <button class="waves-effect waves-light btn" type="submit">Log In</button
</form>

最佳答案

哦,我现在知道了,但我可能错了。您已经检查了来自db的用户密码是否与从前端发送的密码匹配,但是您忘记在此函数内传递该密码,那里只有一封电子邮件,因此只需尝试添加password作为参数:

function (email, password, done) {
Schema.Users.findOne({'Email': email},function (err,user) {
    if(err) {
        return done(err);
    }
    if(user==null) {
        return done(null,false,{ message: 'Incorrect username.' })
    }
    if(user.Password!==password) {
        //console.log('Wrong password');
        return done(null,false,{ message: 'Wrong password' })
    }
    return done(null,user);
})

关于node.js - 特快使用 Passport 时提出错误要求,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48902246/

10-09 18:22