gpt4 book ai didi

node.js - Passport.js - 错误 : failed to serialize user into session

转载 作者:IT老高 更新时间:2023-10-28 21:45:59 25 4
gpt4 key购买 nike

我遇到了 Passport.js 模块和 Express.js 的问题。

这是我的代码,我只想在第一次尝试时使用硬编码登录。

我总是收到消息:

我搜索了很多,在stackoverflow中找到了一些帖子,但我没有失败。

Error: failed to serialize user into session
at pass (c:\Development\private\aortmann\bootstrap_blog\node_modules\passport\lib\passport\index.js:275:19)

我的代码如下所示。

'use strict';

var express = require('express');
var path = require('path');
var fs = require('fs');
var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;
var nodemailer = require('nodemailer');

var app = express();

module.exports = function setupBlog(mailTransport, database){
var config = JSON.parse(fs.readFileSync('./blog.config'));

app.set('view options', {layout: false});

app.use(express.static(path.join(__dirname, '../', 'resources', 'html')));


app.use(express.bodyParser());
app.use(express.cookieParser());
app.use(express.session({ secret: 'secret' }));
app.use(passport.initialize());
app.use(passport.session());


app.get('/blog/:blogTitle', function(req, res) {
var blogTitle = req.params.blogTitle;
if(blogTitle === 'newest'){
database.getLatestBlogPost(function(post) {
res.send(post);
});
} else {
database.getBlogPostByTitle(blogTitle, function(blogPost) {
res.send(blogPost);
});
}
});

passport.use(new LocalStrategy(function(username, password, done) {
// database.login(username, password, done);
if (username === 'admin' && password === 'admin') {
console.log('in');
done(null, { username: username });
} else {
done(null, false);
}
}));

app.post('/login', passport.authenticate('local', {
successRedirect: '/accessed',
failureRedirect: '/access'
}));





app.listen(8080);
console.log('Blog is running on port 8080');

}();

谢谢。

最佳答案

您似乎没有实现 passport.serializeUserpassport.deserializeUser。尝试添加:

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

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

关于node.js - Passport.js - 错误 : failed to serialize user into session,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19948816/

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