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

Better typing of UserSession #38

Closed
Gerbuuun opened this issue Jan 16, 2024 · 3 comments · Fixed by #54
Closed

Better typing of UserSession #38

Gerbuuun opened this issue Jan 16, 2024 · 3 comments · Fixed by #54
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@Gerbuuun
Copy link
Contributor

Gerbuuun commented Jan 16, 2024

My problem

It has been bugging me for a while that the types of this module do not work at all.

Lets say this is your auth.d.ts:

declare module '#auth-utils' {
  interface UserSession {
    user: {
      id: number
      name: string
      email: string
    }
    loggedInAt: number
  }
}

On the client-side, all properties of the useUserSession composable are of type any (only in the playground it works)

Scherm­afbeelding 2024-01-16 om 14 00 54

On the server-side, UserSession is defined except for the user object.
Its type will always be {} | undefined regardless of what is specified in the auth.d.ts (see issue #31 )
And when checking for a session using requireUserSession the returned user object is still possibly undefined even though we just checked and required it to not be.

I have been tinkering and came up with the following:

Instead of looking for a specific property in the session data (user in this case), we check if there are any properties at all.

export async function requireUserSession(event: H3Event) {
  const userSession = await getUserSession(event)

  if (Object.keys(userSession).length === 0) {
    throw createError({
      statusCode: 401,
      message: 'Unauthorized'
    })
  }

  return userSession
}

This way you can define anything in the UserSession interface and the type works (this uses the playground's auth.d.ts)

Scherm­afbeelding 2024-01-16 om 14 22 30

On the client-side I made the following changes:

  • loggedIn is now computed based on wether there exist properties on the session object
  • There is no longer a user getter as this property is no longer defined but now the typing is at least correct.
const useSessionState = () => useState<UserSession>('nuxt-session', () => ({}))

export const useUserSession = () => {
  const sessionState = useSessionState()
  return {
    loggedIn: computed(() => Object.keys(sessionState.value).length > 0),
    session: sessionState,
    fetch,
    clear
  }
}

I would like to hear feedback from you guys

@Atinux
Copy link
Owner

Atinux commented Jan 29, 2024

Thanks for your issue @Gerbuuun

I do like having the user directly and separated from the session, I think this is just a type issue that we can fix. Sadly I am not a TS expert and the help if more than welcome!

@Atinux Atinux added the enhancement New feature or request label Jan 29, 2024
@BayBreezy
Copy link

Can a TS expert please help with this 😄 . I make the mistake when using the composable all the time because I am not sure of the type of the different properties. I remember using the library for the first time. It took me way too long to figure out that the loggedIn value was a boolean ref 😆

@Gerbuuun
Copy link
Contributor Author

Gerbuuun commented Feb 12, 2024

@Atinux I have created a PR that fixes the problem. I also just realized that someone else has submitted a PR with a fix already. (I do prefer my solution though)

Hope we can merge this soon :)

edit: doesn't seem to work anymore for me.... I'm lost. Can someone confirm?
edit2: it does work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
3 participants