From 056901e7e78fa4dbdff1c65b71a759bbbca241d1 Mon Sep 17 00:00:00 2001 From: Pascal Martineau Date: Thu, 18 Sep 2025 14:14:57 -0400 Subject: [PATCH] feat: redirect param to useAuthAction --- .../ccat/app/components/auth/LogoutForm.vue | 2 +- .../themes/ccat/app/composables/useAuth.ts | 25 +++++++++++++------ 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/wp-content/themes/ccat/app/components/auth/LogoutForm.vue b/wp-content/themes/ccat/app/components/auth/LogoutForm.vue index a928b09..7c453f0 100644 --- a/wp-content/themes/ccat/app/components/auth/LogoutForm.vue +++ b/wp-content/themes/ccat/app/components/auth/LogoutForm.vue @@ -22,7 +22,7 @@ if (!isLoggedIn.value) { loading-auto to="#" label="Déconnexion" - @click="logout" + @click="logout()" /> diff --git a/wp-content/themes/ccat/app/composables/useAuth.ts b/wp-content/themes/ccat/app/composables/useAuth.ts index b2e0cb9..e6760f5 100644 --- a/wp-content/themes/ccat/app/composables/useAuth.ts +++ b/wp-content/themes/ccat/app/composables/useAuth.ts @@ -13,7 +13,7 @@ export function useAuth() { export function useAuthActions() { const { fetch: refreshUserSession } = useUserSession(); - const redirect = useRoute().query.redirect as string || "/"; + const routeRedirect = useRoute().query.redirect as string || undefined; const router = useRouter(); const toast = useToast(); @@ -33,15 +33,16 @@ export function useAuthActions() { required: true, }, ]; - async function login({ data: args }: FormSubmitEvent) { + async function login({ data: args }: FormSubmitEvent, redirect?: string) { try { + const redirectTo = redirect || routeRedirect || "/"; const { data, errors } = await useGraphqlMutation("login", args); if (errors.length || !data.login) { console.error(errors); throw new Error("Une erreur est survenue."); } - await router.push(redirect); await refreshUserSession(); + await router.push(redirectTo); } catch (error) { const message = error instanceof Error ? error.message : "Une erreur est survenue."; @@ -50,14 +51,16 @@ export function useAuthActions() { } // Logout - async function logout() { + async function logout(redirect?: string) { try { + const redirectTo = redirect || routeRedirect || "/"; + console.log(redirectTo); const result = await $fetch("/api/logout", { method: "POST" }); if (!result.success) { throw new Error("Une erreur est survenue."); } - await router.push(redirect); await refreshUserSession(); + await router.push(redirectTo); } catch (error) { const message = error instanceof Error ? error.message : "Une erreur est survenue."; @@ -66,13 +69,17 @@ export function useAuthActions() { } // Switch to - async function switchTo(userId: string | number) { + async function switchTo(userId: number, redirect?: string) { try { + const redirectTo = redirect || routeRedirect; const { data, errors } = await useGraphqlMutation("switchTo", { userId }); if (errors.length || !data.switchTo) { throw new Error("Une erreur est survenue"); } await refreshUserSession(); + if (redirectTo) { + await router.push(redirectTo); + } } catch (error) { const message = error instanceof Error ? error.message : "Une erreur est survenue"; @@ -81,13 +88,17 @@ export function useAuthActions() { } // Switch back - async function switchBack() { + async function switchBack(redirect?: string) { try { + const redirectTo = redirect || routeRedirect; const result = await $fetch("/api/switch-back", { method: "POST" }); if (!result.success) { throw new Error("Une erreur est survenue."); } await refreshUserSession(); + if (redirectTo) { + await router.push(redirectTo); + } } catch (error) { const message = error instanceof Error ? error.message : "Une erreur est survenue";