【问题标题】:Passport.js local strategy gives 401 error when trying to authenticatePassport.js 本地策略在尝试进行身份验证时给出 401 错误
【发布时间】:2020-04-02 09:16:57
【问题描述】:

身份验证过程给出 401 bad request。尽管新用户使用散列密码保存到 db,但我注意到它已保存到 MongoDB Atlas。我正在使用本地策略。如果我为注册尝试“本地注册”,我会到达秘密路由,但对于登录路由,我仍然会收到 401 错误请求。 或者 mongodb atlas 尝试访问凭据是否存在身份验证问题??

app.post("/register", function (req, res, next) {
var newUser = new User({
username: req.body.username
});
User.register(newUser, req.body.password, function (err, user, info) {
 console.log(user);
 if (err) {
 return res.render("register");
} else {

// go to the next middleware
next();
 }
res.status(401).send(info);

});
}, passport.authenticate('local', { 
    successRedirect: '/secret',
    failureRedirect: '/login' 
})); 

app.post(
"/login",
passport.authenticate("local", {
successRedirect: "/secret",
failureRedirect: "/login"
}),
function (request, response) {}
);

【问题讨论】:

    标签: mongodb express passport.js http-status-code-401


    【解决方案1】:

    发现错误。我的错误

    passport.use(new localStrategy(User.authenticate));

    坚持

    passport.use(new localStrategy(User.authenticate()));

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-11
      • 2014-02-22
      • 1970-01-01
      • 2017-10-23
      相关资源
      最近更新 更多