rkusa / koa-passport-example

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

help with passport-twitter #13

Closed made-by-chris closed 8 years ago

made-by-chris commented 8 years ago

Hi there, im attempting to transplant your twitter example ( which works for me in the example ) into a pre-existent project. when I hit the initial /auth/twitter, i get a 500 and this error (the route is being arrived at, I can log messages when it is hit )

  TypeError: Object prototype may only be an Object or null: undefined
      at Function.create (native)
      at createReqMock (/Users/basiclaser/work/databird/node_modules/koa-passport/lib/framework/koa.js:187:22)
      at /Users/basiclaser/work/databird/node_modules/koa-passport/lib/framework/koa.js:108:19
      at new Promise (/Users/basiclaser/work/databird/node_modules/core-js/modules/es6.promise.js:197:7)
      at Object.passportAuthenticate (/Users/basiclaser/work/databird/node_modules/koa-passport/lib/framework/koa.js:106:15)
      at Object.dispatch (/Users/basiclaser/work/databird/node_modules/koa-router/lib/router.js:324:52)
      at [object Generator].next (native)
      at Object.<anonymous> (/Users/basiclaser/work/databird/node_modules/koa-compose/index.js:29:5)
      at [object Generator].next (native)
      at onFulfilled (/Users/basiclaser/work/databird/node_modules/co/index.js:65:19)
      at /Users/basiclaser/work/databird/node_modules/co/index.js:54:5
      at new Promise (/Users/basiclaser/work/databird/node_modules/core-js/modules/es6.promise.js:197:7)
      at Object.co (/Users/basiclaser/work/databird/node_modules/co/index.js:50:10)
      at Object.createPromise (/Users/basiclaser/work/databird/node_modules/co/index.js:30:15)
      at Server.<anonymous> (/Users/basiclaser/work/databird/node_modules/koa/lib/application.js:132:8)
      at emitTwo (events.js:100:13)

and here's my current router/auth file

const appDir = require('path').dirname(require.main.filename);
import App from 'koa';
const app = App();
import views from 'co-views';
import publicRoute from 'koa-static'
import {initialTweets} from './initialTweets';
import {searchTweets} from './search';

import Router from 'koa-router';
const router = Router();
import Passport from 'koa-passport';
const passport = Passport;

export function setUpRoutes(){
  var TwitterStrategy = require('passport-twitter').Strategy
  var user = { id: 1, username: 'test' }
  passport.serializeUser(function(user, done) {
    done(null, user.id)
  })
  passport.deserializeUser(function(id, done) {
    done(null, user)
  })
  passport.use(new TwitterStrategy({
      consumerKey: 'sYwaHTFjXlPSUqic9',
      consumerSecret: 'Ka9Ds05LO88SFPf8f7zJwROMasgN6ixzAhlPBKSl7kV',
      callbackURL: 'http://127.0.0.1:4000/auth/twitter/callback'
    },
    function(token, tokenSecret, profile, done) {
      console.log("authentication occured");
      done(null, user)
    }
  ))

  const render = views(appDir + '/views', { ext: 'ejs' });
  app
    .use(router.routes())
    .use(router.allowedMethods())
    .use(publicRoute(appDir + '/public'))
  router
    // new & unauthenticated users
    .get('/', function *(next) {
      this.body = yield render('landing');
    })
    // authenticate users with Twitter
    .get('/auth/twitter',
      passport.authenticate('twitter')
    )
    // redirect users after attempting to authenticate with Twitter
    .get('/auth/twitter/callback',
      passport.authenticate('twitter', {
        successRedirect: '/app',
        failureRedirect: '/'
    }))
    // logged in users
    .get('/app', function *(next) {
      this.body = yield render('index');
    })

    app.listen(4000);
}

If you can shed any light on why you think it might be happening, i would really appreciate it. Thanks!

rkusa commented 8 years ago

Which version of koa and which version of koa-passport are you using?

made-by-chris commented 8 years ago

thanks I've only just become aware of all the 1.x - 2.x issues. i was using koa1 and koa-passport2 . Can you recommend any tutorials for koa2 and koa-router2, or koa2 and koa-passport2? Thank you

rkusa commented 8 years ago

I am only aware of these two:

made-by-chris commented 8 years ago

thanks a lot