From 9d99770b384d06713258d5d516712c80a421bcfd Mon Sep 17 00:00:00 2001 From: Pascal Martineau Date: Tue, 20 Jan 2026 10:37:29 -0500 Subject: [PATCH] refactor: /api/login route --- .../app/composables/useAuthConnexion.ts | 18 ++++-------------- .../themes/moonshine/app/pages/connexion.vue | 5 ++++- .../themes/moonshine/server/api/login.post.ts | 17 +++++++++++++++++ .../themes/moonshine/server/utils/auth.ts | 6 ++++-- .../moonshine/shared/utils/auth-login-form.ts | 8 ++++++++ 5 files changed, 37 insertions(+), 17 deletions(-) create mode 100644 wp-content/themes/moonshine/server/api/login.post.ts create mode 100644 wp-content/themes/moonshine/shared/utils/auth-login-form.ts diff --git a/wp-content/themes/moonshine/app/composables/useAuthConnexion.ts b/wp-content/themes/moonshine/app/composables/useAuthConnexion.ts index dbd07b1..3d0a313 100644 --- a/wp-content/themes/moonshine/app/composables/useAuthConnexion.ts +++ b/wp-content/themes/moonshine/app/composables/useAuthConnexion.ts @@ -1,13 +1,5 @@ -import z from "zod"; import type { FormSubmitEvent } from "@nuxt/ui"; -export const authLoginFormSchema = z.object({ - username: z.email("Courriel invalide"), - password: z.string("Veuillez saisir votre mot de passe"), -}); - -export type AuthLoginForm = z.infer; - const isRedirecting = ref(false); export function useAuthConnexion() { @@ -20,16 +12,14 @@ export function useAuthConnexion() { await delay(1000); await refreshUserSession(); await navigateTo(to || routeRedirect || "/"); - isRedirecting.value = false; } // Login - const { mutate: loginMutate } = useGraphQLMutation("AuthLogin"); - async function login({ data: variables }: FormSubmitEvent, redirect?: string) { + async function login({ data: body }: FormSubmitEvent, redirect?: string) { try { - const { data } = await loginMutate(variables); - if (!data?.login) { - throw new Error(`Échec de la connexion par mot de passe.`); + const { success, message } = await $fetch("/api/login", { method: "POST", body }); + if (!success) { + throw new Error(message); } await redirectTo(redirect); } diff --git a/wp-content/themes/moonshine/app/pages/connexion.vue b/wp-content/themes/moonshine/app/pages/connexion.vue index 54076cd..beaf42b 100644 --- a/wp-content/themes/moonshine/app/pages/connexion.vue +++ b/wp-content/themes/moonshine/app/pages/connexion.vue @@ -1,5 +1,8 @@