adonisjs / auth

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

Possible wrong type when calling .guard('api').loginAs(someUser) #211

Closed enxoco closed 1 year ago

enxoco commented 1 year ago

Package version

@adonisjs/auth 8.2.1

Node.js and npm version

v16.13.1 and 8.1.2

Sample Code (to reproduce the issue)

test('api token can be used to access authenticated routes', async ({ client }) => {

  const unauthorizedRequest = await client.get('/api/users')
  unauthorizedRequest.assertStatus(401)
  const admin = await User.findByOrFail("username", "admin");

  // Delete all tokens to actually test this.
  await Database.rawQuery(`DELETE FROM api_tokens WHERE user_id = ${admin.id}`)
  const authorizedRequest = await client.get('/api/users').guard('api').loginAs(admin)
  authorizedRequest.assertStatus(200)

})

Issue

Maybe I am doing this wrong but I am trying to write a test to verify that api token authentication is working properly. My first issue is that I get the following type error with the above code:

image

My second issue is that is seems that even if I delete all of the api tokens before attempted to make the request I am still receiving a success response. I would expect that this would fail. Am I not doing this right?

RomainLanz commented 1 year ago

Hey @enxoco! 👋🏻

Please check https://github.com/adonisjs/auth/issues/200

enxoco commented 1 year ago

@RomainLanz Thanks for the quick reply and I apologize for not doing a thorough search before posting this. I am assuming that I can simply upgrade the auth package to the latest version to resolve this issue? Giving that a shot now.

RomainLanz commented 1 year ago

Yes, and copy the PR content inside your contracts/auth.ts file where needed.

thetutlage commented 1 year ago

Closing since not actionable