rkusa/koa-passport-example

Not able to login using local strategy.

Closed this issue · 21 comments

I copied the koa-passport-example folder as it is. Npm installed. I also added koa-logger for logging. POST /login is returning 302. What could be the reason?

<-- POST /login
--> POST /login 302 88ms -
<-- GET /app

The 302 Found header is the correct response. It performs the redirect to '/app'. Or do you get an error when opening /app after the login?

I am getting redirected to '/' instead of '/app'. Is this normal behavior?

  <-- POST /login
  --> POST /login 302 88ms -
  <-- GET /app
  --> GET /app 302 25ms -
  <-- GET /
  --> GET / 200 470ms -

No, this is not normal. I'll try to reproduce your issue

Hmm, I think in your case, if (this.req.isAuthenticated()) in server.js#L78 fails, despite the fact that you are successfully logged in.

However, I was not able to reproduce your issue. I made a fresh copy, installed npm packages, started the server and everything worked just fine.

So lets try to isolate the issue, what is your Node version?

0.11.12 is my node version. I will try with 0.11.11 and let you know.

I also tried it with 0.11.12, it works for me.

Maybe there is a problem with the session. Please try adding a console.log(this.session) as follows:

// Require authentication for now
app.use(function*(next) {
  console.log(this.session)
  if (this.req.isAuthenticated()) {
    yield next
  } else {
    this.redirect('/')
  }
})

What is the output look like?

console displays this

{ cookie: 
   { httpOnly: true,
     path: '/',
     overwrite: true,
     signed: true,
     maxage: undefined },
  passport: {} }

Since you have copied koa-passport-example as it is, the output should look like:

{ cookie: 
   { httpOnly: true,
     path: '/',
     overwrite: true,
     signed: true,
     maxage: undefined },
  passport: { user: 1 } }

But I've no idea why it does not add the user id to your session.

Are you using, Mac, Linux or Windows?

I am on ubuntu 13.04

I am on Mac, so I think it should work on Linux just fine ... I've no remaining idea, for now. I'll think a little bit more about it ...

I also get this error when I try to debug using webstorm. But I am getting this on some other codes also which are otherwise working fine. I am guessing it has nothing to do with koa-passport.

[TypeError: Cannot call method 'indexOf' of undefined]

If you need anything from my side, any details or just anything, let me know.

Your output of npm list --depth 0?

koa-passport-example@0.1.0 /home/server/koa-passport-example-master
├─┬ handlebars@1.3.0
│ ├─┬ optimist@0.3.7
│ │ └── wordwrap@0.0.2
│ └─┬ uglify-js@2.3.6
│ ├── async@0.2.10
│ └─┬ source-map@0.1.33
│ └── amdefine@0.1.0
├─┬ koa@0.3.0
│ ├─┬ accepts@1.0.1
│ │ └── negotiator@0.4.2
│ ├── co@3.0.5
│ ├── cookies@0.3.8
│ ├── debug@0.7.4
│ ├── delegates@0.0.3
│ ├── finished@1.1.1
│ ├── fresh@0.2.2
│ ├── keygrip@1.0.0
│ ├── koa-compose@2.1.0
│ ├── mime@1.2.11
│ └── type-is@1.0.0
├─┬ koa-formidable@0.1.0
│ └── formidable@1.0.14
├─┬ koa-logger@1.2.0
│ ├── bytes@0.2.1
│ ├── humanize-number@0.0.2
│ └── passthrough-counter@0.0.1
├─┬ koa-passport@0.2.0
│ └─┬ passport@0.2.0
│ ├── passport-strategy@1.0.0
│ └── pause@0.0.1
├─┬ koa-render@0.2.1
│ ├─┬ co-views@0.2.0
│ │ └─┬ co-render@0.0.1
│ │ └── consolidate@0.9.1
│ ├── debug@0.7.4
│ └── merge@1.0.1 (git://github.com/yields/merge#2f357cb501cd06f1a86394b7817e5df73c53f644)
├─┬ koa-router@3.0.2
│ ├── debug@0.7.4
│ ├── koa-compose@2.2.0
│ ├── methods@0.1.0
│ └── path-to-regexp@0.0.2
├─┬ koa-sess@0.0.8
│ ├── buffer-crc32@0.2.1
│ ├── debug@0.7.4
│ └── uid2@0.0.3
├─┬ passport-facebook@1.0.3
│ └─┬ passport-oauth2@1.1.2
│ ├── oauth@0.9.11
│ ├── passport-strategy@1.0.0
│ └── uid2@0.0.3
├─┬ passport-google@0.3.0
│ ├─┬ passport-openid@0.3.1
│ │ ├── openid@0.5.5
│ │ └─┬ passport@0.1.18
│ │ └── pause@0.0.1
│ └── pkginfo@0.2.3
├─┬ passport-local@0.1.6
│ ├─┬ passport@0.1.18
│ │ └── pause@0.0.1
│ └── pkginfo@0.2.3
└─┬ passport-twitter@1.0.2
├─┬ passport-oauth1@1.0.1
│ ├── oauth@0.9.11
│ ├── passport-strategy@1.0.0
│ └── utils-merge@1.0.0
└─┬ xtraverse@0.1.0
└── xmldom@0.1.19

https://gist.github.com/watsgoing/0ce7a2771f84478f6ef0
I have uploaded the files as I am using them.

Everything looks fine...
Could you try if passport-local with express instead of Koa is working for you? E.g.: passport-local/tree/master/examples/express3

I think passport-local with express is not working. When I login by going to /login and entering username = "bob" and password = "secret" I get redirected to '/'. And when I try to go to '/account' (which is a secure page) I am redirected to /login.
What could be wrong? Should I discuss this issue on passport?

I've tried the express example, too. It's working for me. I've no idea what could be wrong. Maybe the guys at passport-local have an idea?

yes. Sorry for wasting your time like this. Thanks a lot for your help.

No problem! Sorry for not being able to help you ;-)

Atleast we were able to separate the problem. :)