简体   繁体   English

passport.authenticate() 失败

[英]passport.authenticate() failing

So I have a login form which makes a POST request to app.js:所以我有一个登录表单,它向 app.js 发出 POST 请求:

//app.js file
const express = require("express");
const ejs = require("ejs");
const bodyParser = require("body-parser");
const mongoose = require("mongoose");
const { check } = require('express-validator');
const passport = require("passport");
const session = require("express-session");

const UserLogin = require("./middlwares/user-login");
const User = require("./database/Users");

const app = express();

//setting ejs
app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static("public"));

//setting sessions and passport
app.use(session({
    secret: "Our little Secret for Sessions.",
    resave: false,
    saveUninitialized: false
}));

app.use(passport.initialize());
app.use(passport.session());

//Serializing and deserializing user for checking login status in cookie
passport.use(User.createStrategy());

passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());

app.post("/login", UserLogin);

and then the authentication happens in the user-login.js, which has Passport.authenticate().然后身份验证发生在具有 Passport.authenticate() 的 user-login.js 中。 This is working in another tutorial example where I am doing authentication in the main app.js itself.这在另一个教程示例中起作用,我在主 app.js 本身中进行身份验证。 But here, the authenticate function is not executing.但是在这里,验证 function 没有执行。 I don't understand why at all.我完全不明白为什么。 I don't know how to investigate what happens when passport.authenticate() is executed.我不知道如何调查执行 passport.authenticate() 时会发生什么。 I tried going through the documentation but can't figure out why it's failing.我尝试浏览文档,但无法弄清楚它失败的原因。 Just trying to implement what I have learnt in my own project.只是试图实施我在自己的项目中学到的东西。

//user-login.js
const mongoose = require("mongoose");
const passport = require("passport");
const User = require("../database/Users");


module.exports = async (req, res) => {
    const user = new User({
        username: req.body.loginEmail,
        password: req.body.loginPassword
    });

    console.log(user.username + " and " + user.password);
    req.login(user, function(err){
        if (err) {
            console.log("user log-in triggered but error: " + err);
        } else {
            console.log("before authenticate");
            passport.authenticate("local")(req, res, function(){
            console.log("after authenticate");
                res.render("user-logged-in", {});
            });
            
        }
    });

Any help?有什么帮助吗? Just started using stackOverflow, so apologies if the code dump is a bad way to ask a question.刚开始使用stackOverflow,如果代码转储是一个不好的提问方式,我们深表歉意。 I also don't understand this syntax - passport.authenticate('local')(req, res, function(){});我也不明白这种语法 - passport.authenticate('local')(req, res, function(){});

Ok, I just realized the form making the POST request needs its fields to be named as username and password for the passport.authenticate() to work?好的,我刚刚意识到发出 POST 请求的表单需要将其字段命名为用户名和密码才能让 passport.authenticate() 工作? That seems an odd sort of compulsion.这似乎是一种奇怪的强迫。 In my html form I had it named as "loginEmail" because that's also the label that I was using for end users and that's what was causing passport.authenticate() to fail.在我的 html 表单中,我将其命名为“loginEmail”,因为这也是我用于最终用户的 label,这就是导致 passport.authenticate() 失败的原因。

It was not the file structure or some such obscure reason!这不是文件结构或一些如此晦涩的原因!

See Himanshu's reply.见Himanshu的回复。 You need your form fields to be called username and password .您需要将表单字段称为usernamepassword It solved my issue.它解决了我的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM