N
Nuxt3mo ago
kiri

500[nuxt] instance unavailable Hello so I am having middlewae with following code

export default defineNuxtRouteMiddleware((to, from) => {
const authStore = useAuthStore();

console.log(`Navigating from ${from.path} to ${to.path}`);

const publicPaths = ['/'];

if (publicPaths.includes(to.path)) {
console.log('Public path, no redirection needed');
return;
}

if (!authStore.isAuthenticated) {
if (to.path !== LOCAL_ENDPOINTS.login) {
console.log('User is not authenticated, redirecting to login');
return navigateTo(LOCAL_ENDPOINTS.login);
}
}
else if (to.path === LOCAL_ENDPOINTS.login) {
console.log('User is authenticated, redirecting to dashboard');
return navigateTo(LOCAL_ENDPOINTS.dashboard);
}

return;
});
export default defineNuxtRouteMiddleware((to, from) => {
const authStore = useAuthStore();

console.log(`Navigating from ${from.path} to ${to.path}`);

const publicPaths = ['/'];

if (publicPaths.includes(to.path)) {
console.log('Public path, no redirection needed');
return;
}

if (!authStore.isAuthenticated) {
if (to.path !== LOCAL_ENDPOINTS.login) {
console.log('User is not authenticated, redirecting to login');
return navigateTo(LOCAL_ENDPOINTS.login);
}
}
else if (to.path === LOCAL_ENDPOINTS.login) {
console.log('User is authenticated, redirecting to dashboard');
return navigateTo(LOCAL_ENDPOINTS.dashboard);
}

return;
});
and also a store with the followind
export const useAuthStore = defineStore('auth', () => {
const isAuthenticated = ref(false);
const accessToken = ref<string | null>(null);
const isLoggedIn = computed(() => isAuthenticated.value);
const login = (token: string) => {
isAuthenticated.value = true;
accessToken.value = token;
};

const logout = () => {
isAuthenticated.value = false;
accessToken.value = null;
};

const setToken = (token: string) => {
accessToken.value = token;
};

return {
isAuthenticated,
accessToken,
isLoggedIn,
login,
logout,
setToken,
};
}, {
persist: true,
});
export const useAuthStore = defineStore('auth', () => {
const isAuthenticated = ref(false);
const accessToken = ref<string | null>(null);
const isLoggedIn = computed(() => isAuthenticated.value);
const login = (token: string) => {
isAuthenticated.value = true;
accessToken.value = token;
};

const logout = () => {
isAuthenticated.value = false;
accessToken.value = null;
};

const setToken = (token: string) => {
accessToken.value = token;
};

return {
isAuthenticated,
accessToken,
isLoggedIn,
login,
logout,
setToken,
};
}, {
persist: true,
});
but I am getting the following error >> 500 [nuxt] 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. Find out more at https://nuxt.com/docs/guide/concepts/auto-imports#vue-and-nuxt-composables.
1 Reply
kiri
kiriOP3mo ago
at useRouter (C:\Users\kirub\OneDrive\Desktop\nexeor\ERP\ERP_FrontEnd_Shadcn\nodemodules.pnpm\[email protected]@[email protected]_@[email protected][email protected][email protected][email protected]_o57s3csgdh2lnswaprw3ptgo3e\node_modules\nuxt\dist\app\composables\router.js:14:32) at Module.navigateTo (C:\Users\kirub\OneDrive\Desktop\nexeor\ERP\ERP_FrontEnd_Shadcn\nodemodules.pnpm\[email protected]@[email protected]_@[email protected][email protected][email protected][email protected]_o57s3csgdh2lnswaprw3ptgo3e\node_modules\nuxt\dist\app\composables\router.js:98:18) at C:\Users\kirub\OneDrive\Desktop\nexeor\ERP\ERP_FrontEnd_Shadcn\middleware\auth.global.ts:18:36 at C:\Users\kirub\OneDrive\Desktop\nexeor\ERP\ERP_FrontEnd_Shadcn\nodemodules.pnpm\[email protected]@[email protected]_@[email protected][email protected][email protected][email protected]_o57s3csgdh2lnswaprw3ptgo3e\node_modules\nuxt\dist\pages\runtime\plugins\router.js:188:61 at fn (C:\Users\kirub\OneDrive\Desktop\nexeor\ERP\ERP_FrontEnd_Shadcn\nodemodules.pnpm\[email protected]@[email protected]_@[email protected][email protected][email protected][email protected]_o57s3csgdh2lnswaprw3ptgo3e\node_modules\nuxt\dist\app\nuxt.js:223:44) at Object.callAsync (/C:/Users/kirub/OneDrive/Desktop/nexeor/ERP/ERP_FrontEnd_Shadcn/node_modules/.pnpm/[email protected]/node_modules/unctx/dist/index.mjs:68:55) at C:\Users\kirub\OneDrive\Desktop\nexeor\ERP\ERP_FrontEnd_Shadcn\nodemodules.pnpm\[email protected]@[email protected]_@[email protected][email protected][email protected][email protected]_o57s3csgdh2lnswaprw3ptgo3e\node_modules\nuxt\dist\app\nuxt.js:226:56 at Object.runWithContext (C:\Users\kirub\OneDrive\Desktop\nexeor\ERP\ERP_FrontEnd_Shadcn\node_modules.pnpm@[email protected]\node_modules@vue\runtime-core\dist\runtime-core.cjs.js:3894:18) at callWithNuxt (C:\Users\kirub\OneDrive\Desktop\nexeor\ERP\ERP_FrontEnd_Shadcn\node_modules.pnpm\nu what could be done???
Want results from more Discord servers?
Add your server