passport.deserializeuser not called,and ctx.isAuthenticated() always equals false
See original GitHub issue-
The following is my key package “koa”: “^2.2.0”, “koa-passport”: “^3.0.0”, “koa-router”: “^7.0.1”, “koa-session”: “^5.0.0”
-
My problem is when I use “ctx.isAuthenticated()” to check if user login before, ctx.isAuthenticated() always equal false even user has login by passport.authenticate
3.The following is my code:
app.js
const session = require('koa-session')
const passport = require(__dirname + '/src/auth/passport_config.js')
const xauth = require('./src/auth/router_auth.js')
const app = new Koa()
app.proxy = true
app.keys = ['your-session-secret']
app.use(session(app))
app.use(bodyParser())
app.use(passport.initialize())
app.use(passport.session())
app.use(mount('/',xauth.routes()))
passport_config.js
const passport = require('koa-passport')
const LocalStrategy = require('passport-local')
const UserModel = require(__dirname + '/../../src/model/UserModel')
passport.use(new LocalStrategy(
function(username, password, done) {
UserModel.findOne({ username: username }).then(function(result) {
if (result != null) {
if (result.password == password) {
return done(null, result);
} else {
log.error('密码错误');
return done(null, false, { message: '密码错误' })
}
} else {
log.error('用户不存在');
return done(null, false, { message: '用户不存在' })
}
}).catch(function(err) {
log.error(err.message);
return done(null, false, { message: err.message })
});
}
));
passport.serializeUser(function(user, done) {
done(null, user);
});
passport.deserializeUser(function(user, done) {
console.info(user)
return done(null, user);
});
router_auth.js
const Router = require('koa-router')
const log = require('tracer').colorConsole({ level: require('config').get('log').level })
const router = new Router()
const passport = require(__dirname + '/passport_config.js')
router.post('/login', function(ctx, next) {
return passport.authenticate('local', function(err, user, info, status) {
if (user === false) {
ctx.body = { success: false }
ctx.throw(401)
} else {
ctx.body = { success: true }
return ctx.login(user)
}
})(ctx, next)
})
router.post('/test', function(ctx, next) {
if (ctx.isAuthenticated()) {
console.info('认证通过')
return next()
} else {
ctx.body = '非法访问'
}
})
module.exports = router
I try http://localhost/login,it work But I try http://localhost/test,it doesn’t work,it is always false,and passport.deserializeUser has never been called
I am very looking forward to your help,and sorry for my pool english
Issue Analytics
- State:
- Created 6 years ago
- Comments:8 (4 by maintainers)
Top Results From Across the Web
passport's req.isAuthenticated always returning false, even ...
I was using findOne() in findById() and then I replaced it with find() and now req.isAuthenticated() is working fine. My app wasn't saving...
Read more >Trouble authenticating with passport + Node.js + Heroku
deserializeUser() are never being called. In my views, req.user contains only {“isAuthenticated”:false,“matchingUsername”:null,“ ...
Read more >Documentation: Sessions - Passport.js
When the session is authenticated, Passport will call the deserializeUser function, which in the above example is yielding the previously stored user ID, ......
Read more >A brand new website interface for an even better experience!
passport.deserializeuser not called,and ctx.isAuthenticated() always equals false.
Read more >Passport.js | isAuthenticated() always return false - JavaScript
js. While using Local strategy, I have trouble with isAuthenticated(). It always returns false even after login successfully. Below is my code, ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

I’m pretty sure that in the end I resolved it by changing the order in which packages were initialised.
This is the order that worked for me. At one time I had something different that worked with earlier versions but not the latest.
app.use(session(sessionOptions, app)); app.use(passport.initialize()) app.use(passport.session())
Hope it might help.
Steve
Got the same problem, however latest solution doesn’t work for me, I already have such order, but it still doesn’t call deserialize method. However ctx.session is populated with correct session.
It seems this module is absolutely incompatible with koa-session when external session storage is used.