diff --git a/wp-content/themes/ccat/app/components/auth/LoginForm.vue b/wp-content/themes/ccat/app/components/auth/LoginForm.vue index d9bf6a1..451461f 100644 --- a/wp-content/themes/ccat/app/components/auth/LoginForm.vue +++ b/wp-content/themes/ccat/app/components/auth/LoginForm.vue @@ -1,5 +1,5 @@ diff --git a/wp-content/themes/ccat/app/components/auth/LogoutForm.vue b/wp-content/themes/ccat/app/components/auth/LogoutForm.vue index 1e571d4..e6ec18b 100644 --- a/wp-content/themes/ccat/app/components/auth/LogoutForm.vue +++ b/wp-content/themes/ccat/app/components/auth/LogoutForm.vue @@ -1,5 +1,5 @@ diff --git a/wp-content/themes/ccat/app/composables/useAuth.ts b/wp-content/themes/ccat/app/composables/useAuth.ts index fceaab6..59cb04a 100644 --- a/wp-content/themes/ccat/app/composables/useAuth.ts +++ b/wp-content/themes/ccat/app/composables/useAuth.ts @@ -1,12 +1,16 @@ import type { FormSubmitEvent } from "@nuxt/ui"; export function useAuth() { + const { loggedIn, session } = useUserSession(); const toast = useToast(); const redirect = useRoute().query.redirect as string || "/"; const router = useRouter(); const { fetch: refreshUserSession } = useUserSession(); - // Login form + const isLoggedIn = loggedIn; + const isSwitchedTo = computed(() => Boolean(session.value?.isSwitchedTo)); + + // Login const loginFields = [ { name: "email", @@ -22,7 +26,7 @@ export function useAuth() { required: true, }, ]; - async function onLoginSubmit({ data: args }: FormSubmitEvent) { + async function login({ data: args }: FormSubmitEvent) { try { const { data, errors } = await useGraphqlMutation("login", args); if (errors.length || !data.login) { @@ -38,8 +42,8 @@ export function useAuth() { } } - // Logout action - async function onLogoutClick() { + // Logout + async function logout() { try { const result = await $fetch("/api/logout", { method: "POST" }); if (!result.success) { @@ -54,5 +58,35 @@ export function useAuth() { } } - return { loginFields, onLoginSubmit, onLogoutClick }; + // Switch to + async function switchTo(userId: string | number) { + try { + const { data, errors } = await useGraphqlMutation("switchTo", { userId }); + if (errors.length || !data.switchTo) { + throw new Error("Une erreur est survenue"); + } + await refreshUserSession(); + } + catch (error) { + const message = error instanceof Error ? error.message : "Une erreur est survenue"; + toast.add({ title: "Échec du changement d'utilisateur", description: message, color: "error" }); + } + } + + // Switch back + async function switchBack() { + try { + const result = await $fetch("/api/switch-back", { method: "POST" }); + if (!result.success) { + throw new Error("Une erreur est survenue."); + } + await refreshUserSession(); + } + catch (error) { + const message = error instanceof Error ? error.message : "Une erreur est survenue"; + toast.add({ title: "Échec du changement d'utilisateur", description: message, color: "error" }); + } + } + + return { isLoggedIn, isSwitchedTo, loginFields, login, logout, switchTo, switchBack }; } diff --git a/wp-content/themes/ccat/app/composables/useUserSwitching.ts b/wp-content/themes/ccat/app/composables/useUserSwitching.ts deleted file mode 100644 index fdc9ed7..0000000 --- a/wp-content/themes/ccat/app/composables/useUserSwitching.ts +++ /dev/null @@ -1,37 +0,0 @@ -export function useUserSwitching() { - const toast = useToast(); - const { fetch: refreshUserSession } = useUserSession(); - const { session } = useUserSession(); - - const isUserSwitched = computed(() => Boolean(session.value?.isSwitchedTo)); - - async function userSwitchTo(userId: string | number) { - try { - const { data, errors } = await useGraphqlMutation("switchTo", { userId }); - if (errors.length || !data.switchTo) { - throw new Error("Une erreur est survenue"); - } - await refreshUserSession(); - } - catch (error) { - const message = error instanceof Error ? error.message : "Une erreur est survenue"; - toast.add({ title: "Échec du changement d'utilisateur", description: message, color: "error" }); - } - } - - async function userSwitchBack() { - try { - const result = await $fetch("/api/switch-back", { method: "POST" }); - if (!result.success) { - throw new Error("Une erreur est survenue."); - } - await refreshUserSession(); - } - catch (error) { - const message = error instanceof Error ? error.message : "Une erreur est survenue"; - toast.add({ title: "Échec du changement d'utilisateur", description: message, color: "error" }); - } - } - - return { isUserSwitched, userSwitchTo, userSwitchBack }; -}