Hi,
I am trying to upgrade my Nuxt App + external REST API to OAuth. I tried a few Nuxt OAuth options listed on the Nuxt Modules Page, but all of them seem to be designed for the built-in Nuxt API. I then decided to use nuxt-auth-utils. I found no way to access the OAuth Accesstoken on client side, so because I needed the OAuth Accesstoken for my API I also added an Nuxt-API endpoint which returns the accessToken. The client can call that, gets identified via its nuxt-auth-utils session, and gets the Accesstoken, which is currently cached in localStorage to not strain the Nuxt-API on every useFetch request which handles getting the Accesstoken. I also added Accesstoken refreshing via an Nuxt-API endpoint, but that works without problem so I will not go deeper into that.
Problem: It of course has no access to the localStorage in SSR, and because of that the custom useApiFetch composable (which handles getting the AccessToken and adds it to the Authorization headers) tries to call the "get Accesstoken from Nuxt-API" endpoint first, and after that it injects the accessToken into the header options and finally the custom useFetch composable returns a new return useFetch(url, options) so that the vue components can work with the returned data. This unfortunately gives me the following error: A composable that requires access to the Nuxt instance was called outside of a plugin, Nuxt hook, Nuxt middleware, or Vue setup function. This is probably not a Nuxt bug. I tracked it down to be caused by multiple useFetch usages within the composable: 1) useFetch to fetch the accesstoken endpoint to get the accesstoken because there is no localStorage, and 2) the return statement which has a useFetch. When not fetching the Accesstoken endpoint or alternately return null instead of useFetch, this error doesn't occur.
This crashes only on backend, and not on client side.
I don't quite understand why this is happening. Does anybody have an idea about the error?
Thanks!