supabase: Always getting the error "Auth session missing!" when signing in

Hello, I’ve been trying to get supabase auth working on a Nuxt3 project and always get the “Auth session missing!” message from this line: https://github.com/nuxt-community/supabase-module/blob/6d05b9383bd3c9b039e5f7ca17beba8a50812b2a/src/runtime/server/api/session.ts#L17

I was wondering if I had missed something to setup the session? This maybe a beginner issue as I’m just getting into Nuxt3, I’ve not found anything about authentication sessions on the docs.

Thanks for your time, have a great day.

EDIT: It seems I’m getting the error only when I’m coming from a sign up confirmation link not for “normal sign in”.

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 22

Most upvoted comments

I was able to reproduce the “Auth session missing!” error: when multiple tabs of the same app are open with the onAuthStateChange listener active. Only the tab with the active login has a session != null here:

  // Once Nuxt app is mounted
  nuxtApp.hooks.hook('app:mounted', () => {
    // Listen to Supabase auth changes
    client.auth.onAuthStateChange(async (event: AuthChangeEvent, session: Session | null) => {
      await setServerSession(event, session)
      user.value = client.auth.user()
    })
  })
})

The others will throw the error because the session is null, but will get the user anyways, because it has been handled by the active login tab. When no tab besides the active login tab of the same app is open, the error is not thrown.

So maybe simply return here instead of throwing an error ?

if (signEvent === 'SIGNED_IN') {
    if (!session) { throw new Error('Auth session missing!') }
    setCookie(
      event,
      `${cookieOptions.name}-access-token`,
      session.access_token,
      {
        domain: cookieOptions.domain,
        maxAge: cookieOptions.lifetime ?? 0,
        path: cookieOptions.path,
        sameSite: cookieOptions.sameSite
      }
    )
  }

The tag was available anyway. Just created the release 😀