#How can fetch be done with authorization coming from localStorage?

1 messages · Page 1 of 1 (latest)

leaden trout
#

I have a problem related to localStorage during ssr.
Let me explain in more detail.

At the address http://localhost:3000/test I have a useFetch with in the headers an authorization that has as value a token that is in the localStorage.
The problem is that the localStorage is only accessible client-side so the call does not work.

I'm trying them all but I can't solve it. Can anyone who can give me some advice?

solid yarrow
#

Hi 👋 localStorage is only available clientside

#

so you should use cookies to trasnfer client auth tokens to the SSR

leaden trout
#

Hi @solid yarrow and thanks for the suggestion. Unfortunately I suspected that with cookies was the only solution. Unfortunately I followed a long tutorial on implementing jwt through the use of springboot, vue.js and localStorage. Now I don't know if I will be able to repurpose the whole tutorial with the cookie system. Do you happen to have any good tutorials to recommend?

solid yarrow
#

usually auth-guarded routes does not really need SSR. I don't have any recommendations as i'm always using google to find anything.
You can always handle manually your manage tokens in SSR with cookies (with secure and httponly)
Maybe can you try https://github.com/sidebase/nuxt-auth which would save you a lot of time.

GitHub

🔐 Nuxt user authentication and sessions via NextAuth.js. nuxt-auth wraps NextAuth.js to offer the reliability & convenience of a 12k star library to the nuxt 3 ecosystem with a native devel...