Files
headless-2026-03/wp-content/themes/headless/server/api/login.post.ts

27 lines
945 B
TypeScript

export default defineEventHandler(async (event) => {
try {
// Validate the request body against the schema
const variables = authLoginFormSchema.parse(await readBody(event));
// Execute the GraphQL operation to authenticate the user
const { data } = await executeSchemaOperation(event, {
operationName: "AuthLogin",
variables,
});
// Validate the response data
if (!data?.login) throw new Error("Identifiants invalides. Veuillez réessayer.");
// Handle the login process by setting the session data
if (!(await handleLogin(event, data.login))) {
throw new Error("Une erreur est survenue lors de la connexion.");
}
return { success: true, message: "Vous avez été connecté avec succès." };
} catch (error) {
const message =
error instanceof Error ? error.message : "Une erreur est survenue lors de la connexion.";
return { success: false, message };
}
});