Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nuxt Authorization does not follow nuxt-auth-utils' type declaration file #43

Open
devniza opened this issue Jan 20, 2025 · 1 comment
Labels
question Further information is requested

Comments

@devniza
Copy link

devniza commented Jan 20, 2025

I discovered that the Nuxt module is quite helpful for my projects. However, when I attempted to write type-safe code, I ran into an issue.

With nuxt-auth-utils, I am able to create a type declaration file labelled auth.d.ts in the root directory of my project.

// auth.d.ts
import { Role } from './shared/types/roles'

declare module '#auth-utils' {
    interface User {
        id: number;
        role: Role;
    }
}

export { }

However, whenever I create an ability in my shared/utils/abilities.ts file, I do not get any type-safety when interacting with the authorised user.

export const receiveHello = defineAbility((user) => {
    if (user.role == Role.ProcurementOfficer) {
        consola.log(user)
        return true
    }
    return false
}) 

The parameter user is type any. (I can't find a way to make it type-safe... even if I use UserSession from nuxt-auth-utils, it does not take into account the auth.d.ts file (it just gives out user and secure (which do not have any types within them)

According to my consola (a wrapper around console to make stylish outputs) terminal output:

{
  id: 1, 
  role: 'ProcurementOfficer' 
} 

TLDR;
I'd like to know how I can make the user parameter type safe by respecting auth.d.ts. I am not sure if I should make a shared/types/user.ts file for this because I don't want to update so many types (both auth.d.ts, my database schema and the new shared/types/user.ts)

@Barbapapazes
Copy link
Owner

Hey 👋,

What about adding types to the user parameter like in the example?

export const editPost = defineAbility((user: User, post: Post) => {
  return user.id === post.authorId
})

And I think you can create a shared/types/user.ts and then extend this with User in auth.d.ts. I think it should works.

@Barbapapazes Barbapapazes added the question Further information is requested label Jan 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants