rkusa / koa-passport-example

koa-passport usage example
https://github.com/rkusa/koa-passport
262 stars 74 forks source link

yield* passport.authenticate throws TypeError #8

Closed punmechanic closed 9 years ago

punmechanic commented 9 years ago

Hi,

Throwing together a quick sample of this application -

'use strict';
const koa = require('koa')
const passport = require('koa-passport')
const LocalStrategy = require('passport-local').Strategy
const Router = require('koa-router')

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

passport.deserializeUser(function(id, done) {
  done(null, { id: id })
})

passport.use('local', new LocalStrategy(function(username, password, done) {
  done(null, {})
}))

const app = koa()
app.use(passport.initialize())

const router = new Router()
router.get('/protected', function* protectedPage() {
  yield* passport.authenticate('local', function* passportCallback(error, user) { 
    this.status = 200
  })
})
app.use(router.routes())

app.listen(3000)

The call to yield* passport.authenticate throws an exception -

  TypeError: undefined is not a function
      at Object.protectedPage (c:\projects\dash\index.js:25:19)
      at GeneratorFunctionPrototype.next (native)
      at Object.dispatch (c:\projects\dash\node_modules\koa-router\lib\router.js:297:14)
      at GeneratorFunctionPrototype.next (native)
      at onFulfilled (c:\projects\dash\node_modules\koa\node_modules\co\index.js:64:19)
      at c:\projects\dash\node_modules\koa\node_modules\co\index.js:53:5
      at Object.co (c:\projects\dash\node_modules\koa\node_modules\co\index.js:49:10)
      at Object.toPromise (c:\projects\dash\node_modules\koa\node_modules\co\index.js:117:63)
      at next (c:\projects\dash\node_modules\koa\node_modules\co\index.js:98:29)
      at onFulfilled (c:\projects\dash\node_modules\koa\node_modules\co\index.js:68:7)

altering this yield* call to a simple yield allows the application to work as intended.

This code is running on iojs v1.8.1, with latest versions of koa-passport, koa-router, koa and passport-local as of writing.

I'm only mentioning this because the sample application in this repo uses yield* as opposed to yield.

rkusa commented 9 years ago

I had no issue running this example in iojs 2.2. I have still replaced yield* with yield. I am not sure about my initial reason for using yield*, but I don't see a reason for using it, now.

Thanks for reporting!