diff --git a/src/composables/useAuthUtils.ts b/src/composables/useAuthUtils.ts index 202ecfb..75e1df8 100644 --- a/src/composables/useAuthUtils.ts +++ b/src/composables/useAuthUtils.ts @@ -31,7 +31,6 @@ export function getSafeRedirectUrl(input: string | null, baseUrl: string, allowe export function useAuthUtils() { const authStore = useAuthStore(); - const route = useRoute(); const config = useRuntimeConfig(); const allowedRedirectHosts = computed(() => config.public.redirectHosts.split(' ').map(v => v.trim()).filter(v => v.length > 0)); @@ -46,11 +45,12 @@ export function useAuthUtils() { external: redirectUrl.external }); }, - async redirectToLogin() { - await navigateTo({ + redirectToLogin(to?: string | undefined) { + const target = to ?? useRoute().fullPath; + return navigateTo({ path: '/account/login', query: { - redirect: route.fullPath + redirect: target } }); }, diff --git a/src/middleware/2.enforce.global.ts b/src/middleware/2.enforce.global.ts index 7c64a1d..9977f35 100644 --- a/src/middleware/2.enforce.global.ts +++ b/src/middleware/2.enforce.global.ts @@ -1,5 +1,8 @@ -function notAllowed() { - return navigateTo('/'); +import type { RouteLocationNormalizedGeneric } from 'vue-router'; + +function notAllowed(to: RouteLocationNormalizedGeneric) { + const authUtils = useAuthUtils(); + return authUtils.redirectToLogin(to.fullPath); } export default defineNuxtRouteMiddleware(async (to) => { @@ -10,7 +13,7 @@ export default defineNuxtRouteMiddleware(async (to) => { if (to.meta.needsAuth) { if (!meStore.user) { - return notAllowed(); + return notAllowed(to); } } });