Nuxt Plugin using `useCookie` composable error.
Hi, I have a custom Auth fetch nuxt Plugin for fetching data behind a authentication. But I keep getting errors about using Nuxt composables.
I am using the useCookie composable. How should I run this function?:
Error:
My custom fetch plugin:
example page that retrieves the data using the authFetch.
Does somebody know how I can fix this? This might be a SRR issue?
4 Replies
Most likely it’s propagating from
useCookie
which accesses the Nuxt instance on the server. I would move it to the top level out of the callback.Thanks for having a look. Sadly putting the useCookie to the top level like this did not fix the problem:
Some context: I am using a jwt auth endpoint on a custom nodeJS server, so I am not using the nuxt auth package or SSR functionality (server folder).
When setting the
ssr
to false in the nuxt.config.ts
this works perfectly fine. But of course I don't want to lose out on SSR in some pages/components.SSR does not require the use of the server directory. It works with SSR disabled because of the very issue I outlined. Lastly, I don’t see, in your revised code, useCookie moved to the top. It should be within the scope of the plugin, where you’ve placed your config and store.
Thanks a lot, I did manage to fix the composable problem (example below). Now I need to find out why the authToken.value is empty after a refreshCookie() 😂 probably again a SSR or browser thing, since the refreshToken is for the browser.
Well of course I can fix this with just returning the authToken when calling the
refreshTokenAction
function. But I am just curious how the flow is working now in Nuxt, this is an aspect I'm having difficulty wrapping my head around.