adonisjs / auth

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

Lucid Provider Gives Error "The guard must called "setRememberMeToken" before calling "updateRememberMeToken" on the Lucid provider" #219

Closed kylanhurt closed 1 year ago

kylanhurt commented 1 year ago

Package version

8.2.3

Node.js and npm version

16.19.0 and 8.19.3

BONUS (a sample repo to reproduce the issue)

https://github.com/telos-crew/adonis-lucid-auth/tree/649927491c79635b80332ba66dcba2c95f008917

Sample Code (to reproduce the issue)

This specifically occurs when doing the await auth.use('web').login(user, true) line in AuthController.ts

AuthController.ts

  public async login({ auth, request, response }: HttpContextContract) {
    const account_name = 'kylan'
    const user = await User.query().where({ account_name }).firstOrFail()
    try {
      await auth.use('web').login(user, true)
    } catch (err) {
      return response.status(400).json({ error: err.message })
    }
  }

config/auth.ts

const authConfig: AuthConfig = {
  guard: 'web',
  guards: {
    web: {
      driver: 'session',
      provider: {
        driver: 'lucid',
        identifierKey: 'id',
        uids: ['id'],
      },
    },
  },
}

contracts/auth.ts

declare module '@ioc:Adonis/Addons/Auth' {
  interface ProvidersList {
    user: {
      implementation: LucidProviderContract<typeof User>
      config: LucidProviderConfig<typeof User>
    }
  }

  interface GuardsList {
    web: {
      implementation: SessionGuardContract<'user', 'web'>
      config: SessionGuardConfig<'user'>
      client: SessionClientContract<'user'>
    }
  }
}
thetutlage commented 1 year ago

Your User model does not have a rememberMeToken column and neither the database table has this column. Agree the error message can be improved, but it is not a bug.

Please update your model and table and then give it a try. Feel free to re-open the issue if the problem persists

kylanhurt commented 1 year ago

@thetutlage Could you elaborate on this? Is it as simple as removing the references to remember_me_token from the DB migration file? Does this mean that the "rememberMe" functionality is not possible to use with Lucid? Because if I remove it from the DB (it was not present anywhere on the User model), I still get the same error.

thetutlage commented 1 year ago

It means, if you want to use rememberMe feature, then your model and table should have this column

kylanhurt commented 1 year ago

Update: looks like this latest issue after adding the column to the User model was that I used snake case (remember_me_token) instead of camel case (rememberMeToken)

@thetutlage Sorry, I know this is probably a very tedious process for you. I misread your earlier comment. I have adjusted my repo accordingly but am still getting the same issue. In fact, when I log the providerUser in the updateRememberMeToken method it seems that the providerUser.user.$dirty property is missing. This is what providerUser.user logs out:

User {
  modelOptions: {
    connection: 'pg',
    profiler: Profiler {
      appRoot: '/Users/kylan/Documents/adonis/auth-attempt',
      logger: [Logger],
      config: [Object]
    }
  },
  modelTrx: undefined,
  transactionListener: [Function: bound listener],
  fillInvoked: false,
  cachedGetters: {},
  forceUpdate: false,
  '$columns': {},
  '$attributes': { id: 1, account_name: 'kylan', remember_me_token: null },
  '$original': { id: 1, account_name: 'kylan', remember_me_token: null },
  '$preloaded': {},
  '$extras': {},
  '$sideloaded': {},
  '$isPersisted': true,
  '$isDeleted': false,
  '$isLocal': false,
  rememberMeToken: 'vxXL0TDXoQf2iYFXU1VN'
}

As you can see the remember_me_token attribute appears to be included, although the $columns object seems to be empty. Is there something wrong with my project's configuration or something?

Here is the updated commit: https://github.com/telos-crew/adonis-lucid-auth/commit/66485fbcf0f908ccb1e5601e21ed7a27aabb8c2d

thetutlage commented 1 year ago

There is no remember me column in this model https://github.com/telos-crew/adonis-lucid-auth/blob/649927491c79635b80332ba66dcba2c95f008917/app/Models/User.ts.

Please, read a little of about how remember me works with login and sessions and then try to update your code accordingly.