auth icon indicating copy to clipboard operation
auth copied to clipboard

Typing for auth model relationships break when adding token guard

Open jarle opened this issue 1 year ago • 1 comments

Package version

9.2.3

Describe the bug

When adding the token guard to my project I got typescript errors in existing code when trying to load relationships on the auth model.

router.get('/foo', async ({ auth }) => {
  // here the signature of the load method is of type _any_
  await auth.user?.load('post')
  // this gives type error (Argument of type '"post"' is not assignable to parameter of type 'undefined'.ts(2345))
  await auth.user?.related('post').query().first()
  return null
})

Reproduction repo

https://github.com/jarle/adonis-auth-issue

jarle avatar Jul 28 '24 17:07 jarle

Yeah, that is indeed an issue with the types. The immediate way to fix it to use a specific guard before accessing the user property. For example:

auth.use('api').user?.related('posts')

thetutlage avatar Jul 30 '24 04:07 thetutlage

This issue has been marked as stale because it has been inactive for more than 21 days. Please reopen if you still need help on this issue

github-actions[bot] avatar Jan 18 '25 01:01 github-actions[bot]

This issue has been marked as stale because it has been inactive for more than 21 days. Please reopen if you still need help on this issue

github-actions[bot] avatar Feb 08 '25 01:02 github-actions[bot]

This issue has been automatically closed because it has been inactive for more than 4 weeks. Please reopen if you still need help on this issue

github-actions[bot] avatar Feb 13 '25 01:02 github-actions[bot]