adonisjs / auth

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

feat: option to specify user's table identifier column #152

Closed jotaajunior closed 3 years ago

jotaajunior commented 3 years ago

Proposed changes

I'm using Adonis in one of my projects and due to internal nomenclature conflicts the users table actually mean a totally different thing from the original users table.

So, I renamed the Users model to Account and updated the config, everything work out perfectlly, but since I'm using the OAT guard, I have this in my api_tokens migration:

      table
        .integer('user_id')
        .unsigned()
        .references('id')
        .inTable('users')
        .onDelete('CASCADE')

I updated to this:

      table
        .integer('acccount_id')
        .unsigned()
        .references('id')
        .inTable('accounts')
        .onDelete('CASCADE')

But since the user_id is hardcoded inside the package, this throws an error:

insert into "api_tokens" ("created_at", "expires_at", "name", "token", "type", "user_id") values ($1, $2, $3, $4, $5, $6) returning "id" - column "user_id" of relation "api_tokens" does not exist

Obivouslly I could do:

      table
        .integer('user_id')
        .unsigned()
        .references('id')
        .inTable('accounts')
        .onDelete('CASCADE')

But I think this is missleading, as User exists and is a completly different entity, in my case.

I do understand that this is probably not a very common issue, so I tried to make this a non-breaking-change.

Currently the DatabaseTokenProviderConfig looks like this:

// adonis-typings/auth.ts

    export type DatabaseTokenProviderConfig = {
        driver: 'database'
        table: string
        connection?: string
    }

I changed to this:

// adonis-typings/auth.ts
    export type DatabaseTokenProviderConfig = {
        driver: 'database'
        table: string
        foreignKey?: string
        connection?: string
    }

Notice foreignKey is optional. As most of the times the foreignKey will be user_id, I think that maybe we could automatically infer the foreignKey as being user_id.

Types of changes

What types of changes does your code introduce?

Put an x in the boxes that apply

Checklist

Put an x in the boxes that apply. You can also fill these out after creating the PR. If you're unsure about any of them, don't hesitate to ask. We're here to help! This is simply a reminder of what we are going to look for before merging your code.

Further comments

As I said I don't think this is a relevant change at all, but since is possible to change the default "users" table maybe would also be usefull to have the ability to change the foreign key.

thetutlage commented 3 years ago

I will suggest naming it as foreignKeyColumn or maybe just foreignKey?

jotaajunior commented 3 years ago

Seems like foreignKey is a better option

jotaajunior commented 3 years ago

Ok, pretty much agree with everything.

Could you verify if the tests are correct? I'm not really an expert.

Maybe I could also squash the commits?

thetutlage commented 3 years ago

Closed and re-opened for the tests to run

thetutlage commented 3 years ago

Thanks for the PR 😄