refactor: /api/login route
This commit is contained in:
@@ -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<typeof authLoginFormSchema>;
|
||||
|
||||
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<AuthLoginForm>, redirect?: string) {
|
||||
async function login({ data: body }: FormSubmitEvent<AuthLoginForm>, 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);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
<script setup lang="ts">
|
||||
|
||||
const { isRedirecting } = useAuthConnexion();
|
||||
onBeforeMount(() => {
|
||||
isRedirecting.value = false;
|
||||
});
|
||||
</script>
|
||||
|
||||
<template>
|
||||
|
||||
Reference in New Issue
Block a user