护照的req.isAuthenticated总是返回false,即使我硬编码完成(null,true)
我试图让我的护照本地策略工作。
我有这个中间件设置:
passport.use(new LocalStrategy(function(username, password, done) { //return done(null, user); if (username=='ben' && password=='benny'){ console.log("Password correct"); return done(null, true); } else return done(null, false, {message: "Incorrect Login"}); }));
但在这里
app.use('/admin', adminIsLoggedIn, admin); function adminIsLoggedIn(req, res, next) { // if user is authenticated in the session, carry on if (req.isAuthenticated()) return next(); // if they aren't redirect them to the home page res.redirect('/'); }
它总是失败并redirect到主页。
我无法弄清楚为什么会发生这种情况? 为什么不authentication?
在我的控制台中,我可以看到Password Correct
是打印。 为什么不行?
我有一个类似的问题。 可能是由于护照所需的快速会话中间件。 通过使用中间件按以下顺序进行修复:(Express 4)
var session = require('express-session'); // required for passport session app.use(session({ secret: 'secrettexthere', saveUninitialized: true, resave: true, // using store session on MongoDB using express-session + connect store: new MongoStore({ url: config.urlMongo, collection: 'sessions' }) })); // Init passport authentication app.use(passport.initialize()); // persistent login sessions app.use(passport.session());
这也可能是您的客户的POST / GET调用的问题。 我有这个完全相同的问题,但事实certificate,我不得不提供fetch
(这是我使用的)选项credentials:'include'
像这样:
fetch('/...', { method: 'POST', headers: myHeaders, credentials: 'include', body: ... ...})
原因是因为提取不支持传递cookie,这在这种情况下是必要的。
忘记添加我也有同样的问题
request.login()
上
app.post('/login', function(request, response, next) { console.log(request.session) passport.authenticate('login', function(err, user, info) { if(!user){ response.send(info.message);} else{ request.login(user, function(error) { if (error) return next(error); console.log("Request Login supossedly successful."); return response.send('Login successful'); }); //response.send('Login successful'); } })(request, response, next); } );
希望这可能有助于其他人在这里结束与我一样的原因。
为NEWBIES
我正面临类似的问题,我的isAuthenticated()函数将返回false.I失去了很多时间,希望这个答案保存你的。
需要注意的一些常见问题,
- 中间件设置顺序(express-session> pass.initialize> pass.session)(并且当你这样做的时候检查你的语法)。
- Serialize和Deserialize方法需要传递用户请求(更多信息,我已经发布了这个链接的答案.. Passport基础知识Session(expressjs) – 为什么我们需要序列化和反序列化? )如果没有用户请求然后isAuthenticated将返回false ….并redirect到PATH定义……当错误…和…再次一次…..检查您的语法。
- 在模型(user.js)中定义的getUserById或findById函数需要有一个User.findById(而不是User.findOne,再次检查你的等待IT..SYNTAX)函数的定义。请求在每个会议)
我通过修复我的passport.deserializeUser来解决这个问题。 我使用的是mongo native,因为大部分的例子都使用了Mongoose,所以我再次陷入了_id陷阱。
所以记得在读取deserializeUser中的用户时使_id是mongo ObjectID
passport.deserializeUser(function(user, done) { const collection = db.get().collection('users') const userId = new mongo.ObjectID(user); collection.findOne({_id : userId}, function(err, user) { if (err) done(err, null); done(null, user); }); });
我的查询没有find用户,因为我没有使id为ObjectID,并且没有任何地方显示错误。
- passport.initialize()中间件不用于Express 4.10自定义callback
- 护照本地策略 – 不通过callback
- ReactJs前端和Passport NodeJs后端
- Mongoose schematype.castForQuery错误。 用户身份validation失败
- 如何允许+护照本地策略的用户字符?
- Passport本地策略完成callback不会传递错误json消息
- 如何在使用护照-http Basic +护照 – 本地组合时防止www-authenticate头
- 如何在Node.js中重置密码后使用passport.jslogin?
- 尝试发出POST请求注册新用户时出现404错误