adonisjs / auth

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

Bad loginAs method args type when chained to a guard #200

Closed Foohx closed 2 years ago

Foohx commented 2 years ago

In tests, only when chaining loginAs to guard methods, arguments of loginAs method are of type never.

(method) loginAs(...args: never): ApiRequest

Package version

@adonisjs/auth: 8.2.1

Node.js and npm version

Node.js: 16.13.0 npm: 8.10.0

Sample Code (to reproduce the issue)

Just create a new project with at least @adonisjs/auth package, then fill the existing test with the following code :

const user = await User.create({ email: 'ex@amp.le', password: '123456' })

const response = await client.get('/').loginAs(user) // Everything good

const responseApi = await client.get('/').guard('api').loginAs(user) // <-- Argument of type 'User' is not assignable to parameter of type 'never'

response.assertStatus(200)
responseApi.assertStatus(200)
thetutlage commented 2 years ago

Can you share the contracts/auth.ts file?

Foohx commented 2 years ago

Here is the file:

import User from 'App/Models/User'

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

  interface GuardsList {
    api: {
      implementation: OATGuardContract<'user', 'api'>
      config: OATGuardConfig<'user'>
    }
  }
}
thetutlage commented 2 years ago

Its a bug with the default auth contract template. Lemme push a fix

Foohx commented 2 years ago

No problem, thank you for your reactivity.

sekistner commented 2 years ago

Its a bug with the default auth contract template. Lemme push a fix

Any hint what the problem is?

thetutlage commented 2 years ago

You will have to make the changes made in this commit manually. https://github.com/adonisjs/auth/commit/946e730cdebcfd277cf69ce29d5be40c80a25ce0

The changes will be inside the contracts/auth.ts file. New projects will not have this issue

MANTENN commented 11 months ago

@Foohx was guard or loginAs undefined for you when you run the tests?

Foohx commented 11 months ago

@Foohx was guard or loginAs undefined for you when you run the tests?

Don't remember but it was fixed by updating manually the auth contract. Check replies on top :)

MANTENN commented 11 months ago

@Foohx got it, I fixed the type issue—I also had to use the ! operator for the error to go away. only issue I have is during runtime, the authjs test functions are undefined.

Foohx commented 11 months ago

@MANTENN Weird that you have to use the ! operator.

I'm using it without problem like this:

    const user = await UserFactory.apply('verified').with('roles').create()

    const mock = sinon.mock(ConversationPolicy.prototype)
    mock.expects('before').once().resolves(undefined)
    mock.expects('viewList').once().resolves(true)

    await client.get('/conversations').guard('api').loginAs(user).json({})

    mock.verify()
    mock.restore()

But I am not in the latest version for the moment. I'm going to update all deps in the next few days and I'll let you know if I encounter the same problem. :)

MANTENN commented 11 months ago

@Foohx I used the User.find(1) which either returns a user or null hence why I had to use the ! operator.

MANTENN commented 11 months ago

@Foohx turns out I had NODE_ENV set to local so the bindings never were loaded when I ran my tests. I will need to open a PR to create a warning in the CLI.

https://github.com/adonisjs/auth/issues/220

Foohx commented 11 months ago

@Foohx I used the User.find(1) which either returns a user or null hence why I had to use the ! operator.

Okay, so everything is normal.