diff --git a/auth/local/auth/composables/auth.ts b/auth/local/auth/composables/auth.ts index 6f8ea56a..d25b6eaa 100644 --- a/auth/local/auth/composables/auth.ts +++ b/auth/local/auth/composables/auth.ts @@ -8,9 +8,9 @@ export const authLogin = async (email: string, password: string) => { password: password, }, }); - useAuth().redirectTo.value = null; + const { redirectTo } = useRoute().query; await useAuth().updateSession(); - await navigateTo(useAuth().redirectTo.value || "/"); + await navigateTo(String(redirectTo) || "/"); }; export const authRegister = async (email: string, password: string) => { diff --git a/auth/local/auth/nuxt.config.ts b/auth/local/auth/nuxt.config.ts index e42656af..ca8768bd 100644 --- a/auth/local/auth/nuxt.config.ts +++ b/auth/local/auth/nuxt.config.ts @@ -15,7 +15,7 @@ export default defineNuxtConfig({ }, nitro: { storage: { - ".data:auth": { driver: "fs", base: "./.data/auth" }, + "db:auth": { driver: "fs", base: "./.data/auth" }, }, }, }); diff --git a/auth/local/auth/plugins/0.auth.ts b/auth/local/auth/plugins/0.auth.ts index f7a662e1..aaa2bb07 100644 --- a/auth/local/auth/plugins/0.auth.ts +++ b/auth/local/auth/plugins/0.auth.ts @@ -11,24 +11,21 @@ export default defineNuxtPlugin(async (nuxtApp) => { const loggedIn: any = computed(() => !!session.value?.email); - // Create a ref to know where to redirect the user when logged in - const redirectTo = useState("authRedirect") - /** * Add global route middleware to protect pages using: - * + * * definePageMeta({ * auth: true * }) */ - // + // addRouteMiddleware( "auth", - (to) => { + (to, from) => { if (to.meta.auth && !loggedIn.value) { - redirectTo.value = to.path - return "/login"; + if (process.server && to.path === from.path) return; + return `/login?redirectTo=${to.path}`; } }, { global: true } @@ -39,14 +36,14 @@ export default defineNuxtPlugin(async (nuxtApp) => { if (process.client) { watch(loggedIn, async (loggedIn) => { if (!loggedIn && currentRoute.meta.auth) { - redirectTo.value = currentRoute.path - await navigateTo("/login"); + await navigateTo(`/login?redirectTo=${currentRoute.path}`); } }); } if (loggedIn.value && currentRoute.path === "/login") { - await navigateTo(redirectTo.value || "/"); + const to = String(currentRoute.query.redirectTo) || '/'; + await navigateTo(to); } return { @@ -54,7 +51,6 @@ export default defineNuxtPlugin(async (nuxtApp) => { auth: { loggedIn, session, - redirectTo, updateSession, }, }, diff --git a/auth/local/pages/login.vue b/auth/local/pages/login.vue index ffed7115..4def8fab 100644 --- a/auth/local/pages/login.vue +++ b/auth/local/pages/login.vue @@ -10,7 +10,7 @@ const isValid = computed(() => { return email.value && password.value; }); -const redirectTo = useAuth().redirectTo.value +const { redirectTo } = useRoute().query; const alert = ref( `Please login or register ${redirectTo ? `to access ${redirectTo}` : ""}` ); diff --git a/auth/local/pages/profile.vue b/auth/local/pages/profile.vue index f4471b1d..05bba177 100644 --- a/auth/local/pages/profile.vue +++ b/auth/local/pages/profile.vue @@ -7,7 +7,7 @@ definePageMeta({ auth: true, }); -const { data: session } = await useFetch("/api/auth/session", { headers: useRequestHeaders(['cookie'])}); +const { data: session } = await useFetch("/api/auth/session");