feat: Initial login / logout API endpoints
This commit is contained in:
27
wp-content/themes/headless/server/api/login.post.ts
Normal file
27
wp-content/themes/headless/server/api/login.post.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
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, error } = await executeSchemaOperation(event, {
|
||||
operationName: "AuthLogin",
|
||||
variables,
|
||||
});
|
||||
|
||||
// Handle errors and validate the response data
|
||||
if (error) throw error;
|
||||
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: "Connexion réussie" };
|
||||
} catch (error) {
|
||||
const message =
|
||||
error instanceof Error ? error.message : "Une erreur est survenue lors de la connexion.";
|
||||
return { success: false, message };
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user