When using a composable which uses useRoute
inside middleware I receive this warn: ‘[nuxt] Calling useRoute within middleware may lead to misleading results. Instead, use the (to, from) arguments passed to the middleware to access the new and old routes.’
I tried the solutions from here: https://github.com/nuxt-modules/i18n/issues/2064 and here Nuxt i18n Calling useRoute within middleware may lead to misleading results, but they didn’t work for me. I’m trying to understand whether this is a bug that I should wait to be fixed or if there’s an actual solution. I’m using useRoute
inside useStoreAuth()
, but not inside checkSession
. Since I added useStoreAuth
to the middleware, it says I can’t use useRoute
. Creating a separate composable for the signIn
function where I use useRoute
won’t work, because I need to access the signedIn
ref from useStoreAuth
in both checkSession
and signIn
functions. Besides, I need to use checkSession
within signIn
, which means if I create a separate composable called useStoreSession
and add it to the middleware, it will also contain useStoreAuth
with useRoute
.
export default defineNuxtRouteMiddleware(async (to, _from) => {
const { signedIn, checkSession } = useStoreAuth();
await checkSession();
if (!signedIn.value && to.path.startsWith('/profile')) {
return navigateTo(`/sign-in?redirect=${encodeURIComponent(to.path)}`);
}
if (signedIn.value && to.path === '/sign-in') {
return navigateTo('/');
}
});
Looking for a possible solution.