adonisjs / auth

Official Authentication package for AdonisJS
https://docs.adonisjs.com/guides/auth/introduction
MIT License
196 stars 63 forks source link

Refresh tokens issue in scheme jwt with custom foreign keys #82

Closed vinicius-batista closed 6 years ago

vinicius-batista commented 6 years ago

So, when I try to refresh an user token show a query exception: error: column tokens.userid does not exist Method try to access userid (all in lowercase) in tokens table, but the correct is userId. This error show only in generateForRefreshToken.

Code example bellow.

//Model definition
class User extends Model {
    static boot () {
      super.boot()
      this.addHook('beforeCreate', 'User.hashPassword')
   }
    tokens () {
      return this.hasMany('App/Models/Token', 'id', 'userId')
    }
  }

  //controller
  async refreshToken ({ request, auth }) {
      const refreshToken = request.header('x-refresh-token')
      return await auth.generateForRefreshToken(refreshToken) 
  }
thetutlage commented 6 years ago

Did u modified the default foreign key?

vinicius-batista commented 6 years ago

In token scheme? Yes. I use auth method for attempt with refresh token and work correctly.

This error was throw when executing this method on Lucid Serializer. https://github.com/adonisjs/adonis-auth/blob/develop/src/Serializers/Lucid.js#L158

thetutlage commented 6 years ago

Can u share the database you are using?

vinicius-batista commented 6 years ago

I'm using PostgreSQL

thetutlage commented 6 years ago

Okay, so that seems to be the issue, PostgreSQL converts keys to lowercase when not in double quotes ".

Lemme see what best I can do in this case

thetutlage commented 6 years ago

@vinicius-batista Fixed in https://github.com/adonisjs/adonis-lucid/commit/c7df2007a9294b4279187d4de293f47de65b845f