Nuxt Authorization does not follow nuxt-auth-utils' type declaration file
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)
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.