gpt4 book ai didi

node.js - 使用 Node/express 和 Passport.js 无法登录并且没有错误消息

转载 作者:太空宇宙 更新时间:2023-11-04 00:03:59 25 4
gpt4 key购买 nike

我注册没有问题,但是当尝试登录时,发生的所有情况是页面刷新,没有错误消息。

这是登录表单(我使用的是 ejs)

<form class="" action="/login" method="post">
<h1>Login</h1>
<div class="EmailAddress">
<input type="text" name="email" placeholder="Email">
</div>
<div class="Password">
<input type="password" name="password" placeholder="Password">
</div>
<div class="Login">
<input type="submit" value="submit">
</div>
</form>

我为两种不同类型的用户创建了两个模型,一种是运营商,一种是客户,它们几乎相同,并且注册时工作得非常好。目前我仅尝试登录客户,因此这是我的passport.js 文件:

编辑:我已经根据下面的zerosand1s所说更新了它,但不幸的是仍然无法工作。

  module.exports = function(passport) {
//LocalStrategy
passport.use(
new LocalStrategy(
{ passReqToCallback: true },
{
usernameField: "email",
passwordField: "password"
},
function(req, email, password, done) {
let query = { email: email };
// checking the name in the database against the one submitted on the form
Customer.findOne(query, function(err, customer) {
if (err) throw err;
if (!customer) {
return done(null, false, {
message: "No such password",
type: "error"
});
}
//Match password
bcrypt.compare(password, customer.password, function(err, isMatch) {
if (err) throw err;
if (isMatch) {
return done(null, customer);
} else {
return done(null, false, {
message: "No such password",
type: "error"
});
// if the password isnt an matc return wrong password
}
});
});
}
)
);
};

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

passport.deserializeUser(function(id, done) {
Customer.findById(id, function(err, customer) {
done(err, customer);
});
});

这是我用于登录的路线文件index.js

// login route
router.get("/login", function(req, res) {
res.render("login", {message: req.flash('error')});
});

// login Process
router.post("/login", function(req, res, next) {
passport.authenticate('local', {
// were using the LocalStrategy used in config/passport file
successRedirect:'../customer/dashboard',
failureRedirect:'/login',
failureFlash: true
})(req, res, next);
});

router.get("/customer/dashboard", function(req, res) {
res.render("customer/dashboard");
});

我认为可能有问题的唯一地方是我的 app.js 文件:

// Express session
app.use(session({
secret: 'keyboard cat',
resave: true,
saveUninitialized: true,
cookie: { secure: true }
}));

// Express messages middleware
app.use(require('connect-flash')());
app.use(function (req, res, next) {
res.locals.messages = require('express-messages')(req, res);
// setting a global variable called express-messages
next();
});

// Express validator middleware
app.use(expressValidator({
errorFormatter: function(param, msg, value) {
var namespace = param.split('.')
, root = namespace.shift()
, formParam = root;

while(namespace.length) {
formParam += '[' + namespace.shift() + ']';
}
return {
param : formParam,
msg : msg,
value : value
};
}
}));

app.use(function (req, res, next) {
res.locals.messages = require('express-messages')(req, res);
next();
});

// Passport Config
require('./config/passport')(passport);
// Passport Middleware
app.use(passport.initialize());
app.use(passport.session());

app.get('*', function(req, res, next) {
res.locals.customer = req.customer || null;
next();
});

app.use(cookieparser());


// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }))
// parse application/json
app.use(bodyParser.json())

app.listen(3000, function() {
console.log('server is running');
});
app.use(flash());
app.use(routes);

我花了一整天的时间来撕扯我的头发,所以非常感谢您的帮助

最佳答案

由于您使用电子邮件进行登录(而不是默认用户名),因此您需要在 Passport 本地策略中进行指定。

来自 Passport 文档,

By default, LocalStrategy expects to find credentials in parameters named username and password. If your site prefers to name these fields differently, options are available to change the defaults.

passport.use(new LocalStrategy({
passReqToCallback: true
usernameField: 'email',
passwordField: 'passwd'
},
function(username, password, done) {
// ...
}
));

关于node.js - 使用 Node/express 和 Passport.js 无法登录并且没有错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53214601/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com