node package manager

koa-passport

Passport middleware for Koa

koa-passport

Passport middleware for Koa

koa-passport version koa version branch npm tag
1.x 1.x v1.x latest
2.x 2.x v2.x next
3.x 2.x master
  • change ctx.passport.* to ctx.state.* (e.g. ctx.passport.user to ctx.state.user)
  • don't call passport methods on ctx.req (e.g. use ctx.login instead of ctx.req.login)
  • update custom authentication callback arguments to err, user, info, status (e.g. passport.authenticate('local', function(err, user, info, status) { ... })(ctx, next))
// body parser 
const bodyParser = require('koa-bodyparser')
app.use(bodyParser())
 
// Sessions 
const convert = require('koa-convert') // necessary until koa-generic-session has been updated to support koa@2 
const session = require('koa-generic-session')
app.keys = ['secret']
app.use(convert(session()))
 
const passport = require('koa-passport')
app.use(passport.initialize())
app.use(passport.session())

Example Application

Passport's values and methods are exposed as follows:

app.use(async ctx => {
  ctx.isAuthenticated()
  ctx.isUnauthenticated()
  ctx.login()
  ctx.logout()
  ctx.state.user
})

MIT