lib/framework/koa.js
function initialize(passport) {
  const middleware = promisify(_initialize(passport))
  return function passportInitialize(ctx, next) {
    // koa <-> connect compatibility:
    const userProperty = passport._userProperty || 'user'
    // check ctx.req has the userProperty
    if (!ctx.req.hasOwnProperty(userProperty)) {
      Object.defineProperty(ctx.req, userProperty, {
        enumerable: true,
        get: function() {
          return ctx.state[userProperty]
        },
        set: function(val) {
          ctx.state[userProperty] = val
        }
      })
    }

    // create mock object for express' req object
    const req = createReqMock(ctx, userProperty)


    // add Promise-based login method
    const login  = req.login
    ctx.login = ctx.logIn = function(user, options) {
      return new Promise((resolve, reject) => {
        login.call(req, user, options, err => {
          if (err) reject(err)
          else resolve()
        })
      })
    }

    // add aliases for passport's request extensions to Koa's context
    ctx.logout = ctx.logOut = req.logout.bind(req)
    ctx.isAuthenticated     = req.isAuthenticated.bind(req)
    ctx.isUnauthenticated   = req.isUnauthenticated.bind(req)

    return middleware(req, ctx).then(function() {
      return next()
    })
  }
}
function initialize(passport) {
  const middleware = promisify(_initialize(passport))
  return function passportInitialize(ctx, next) {
    // koa <-> connect compatibility:
    const userProperty = passport._userProperty || 'user'
    // check ctx.req has the userProperty
    if (!ctx.req.hasOwnProperty(userProperty)) {
      Object.defineProperty(ctx.req, userProperty, {
        enumerable: true,
        get: function() {
          return ctx.state[userProperty]
        },
        set: function(val) {
          ctx.state[userProperty] = val
        }
      })
    }

    // create mock object for express' req object
    const req = createReqMock(ctx, userProperty)


    // add Promise-based login method
    const login  = req.login
    ctx.login = ctx.logIn = function(user, options) {
      return new Promise((resolve, reject) => {
        login.call(req, user, options, err => {
          if (err) reject(err)
          else resolve()
        })
      })
    }

    // add aliases for passport's request extensions to Koa's context
    ctx.logout = ctx.logOut = req.logout.bind(req)
    ctx.isAuthenticated     = req.isAuthenticated.bind(req)
    ctx.isUnauthenticated   = req.isUnauthenticated.bind(req)

    return middleware(req, ctx).then(function() {
      return next()
    })
  }
}
function initialize ( passport ) { const middleware = promisify ( _initialize ( passport ) ) return function passportInitialize ( ctx , next ) { // koa <-> connect compatibility: const userProperty = passport . _userProperty || 'user' // check ctx.req has the userProperty if ( ! ctx . req . hasOwnProperty ( userProperty ) ) { Object . defineProperty ( ctx . req , userProperty , { enumerable : true , get : function ( ) { return ctx . state [ userProperty ] } , set : function ( val ) { ctx . state [ userProperty ] = val } } ) } // create mock object for express' req object const req = createReqMock ( ctx , userProperty ) // add Promise-based login method const login = req . login ctx . login = ctx . logIn = function ( user , options ) { return new Promise ( ( resolve , reject ) => { login . call ( req , user , options , err => { if ( err ) reject ( err ) else resolve ( ) } ) } ) } // add aliases for passport's request extensions to Koa's context ctx . logout = ctx . logOut = req . logout . bind ( req ) ctx . isAuthenticated = req . isAuthenticated . bind ( req ) ctx . isUnauthenticated = req . isUnauthenticated . bind ( req ) return middleware ( req , ctx ) . then ( function ( ) { return next ( ) } ) } }
Passport's initialization middleware for Koa.

@return {GeneratorFunction}
@api private
Passport s initialization middleware for Koa .