trandaison / nuxt-3-auth

A simple authentication module for Nuxt 3
https://nuxt-3-auth.github.io
42 stars 4 forks source link

Value is missing on SSR #19

Open paulvonber opened 6 days ago

paulvonber commented 6 days ago

Can you please clarify why I can't get loggedIn.value on SSR is this intended behaviour?

here is how my code looks like:

  const { $auth } = useNuxtApp()
  const { user, loggedIn } = $auth

  if (import.meta.server) {
    console.log(`🆘 ~ SERVER loggedIn:`, loggedIn)
    console.log(`🆘 ~ SERVER loggedIn:`, loggedIn.value)
  }

  if (import.meta.client) {
    console.log(`🆘 ~ CLIENT loggedIn:`, loggedIn)
    console.log(`🆘 ~ CLIENT loggedIn:`, loggedIn.value)
  }

And here is the result: SSR:

image

CLIENT:

image
trandaison commented 5 days ago

@paulvonber Can you create a reproduce repo on codesandbox or stackblitz? It seem something's wrong with your code, I'm testing on local but the SSR works just fine.

The component

Screenshot 2024-06-24 at 08 39 11

The debug log on server side

Screenshot 2024-06-24 at 08 38 51

Screenshot 2024-06-24 at 08 38 35

The SSR content

Screenshot 2024-06-24 at 08 38 15

paulvonber commented 5 days ago

@trandaison I'll try to find a time to create repro as this is a bit of things to do. But would you try out to get the value within the middleware as I have problem there in the page/component everything works correctly for me as well.

trandaison commented 4 days ago

@paulvonber The same result.

Screenshot 2024-06-24 at 21 04 52 Screenshot 2024-06-24 at 21 03 20 Screenshot 2024-06-24 at 21 02 20

Did you set auth.middleware.global = false in the nuxt.config.ts file?

paulvonber commented 4 days ago

I do have auth.middleware.global = true not sure if that anyhow clashes, but I also try to use it in middleware_name.global.js so globally as well.

trandaison commented 1 day ago

I got it. Your middleware is a global one, and it has a higher order than the auth middleware, so it executes before the auth middleware where I put the logic to determine the login session.

I will fix in the next release. Thanks for the support!