minor: rename onLoginSubmit / onLoginClick
All checks were successful
Deploy WordPress and Nuxt / deploy (push) Successful in 1m2s

This commit is contained in:
2025-09-18 12:28:29 -04:00
parent 0e8dd8f01e
commit 48c8454f2a
4 changed files with 43 additions and 46 deletions

View File

@@ -1,5 +1,5 @@
<script setup lang="ts">
const { loginFields, onLoginSubmit } = useAuth();
const { loginFields, login } = useAuth();
</script>
<template>
@@ -10,6 +10,6 @@ const { loginFields, onLoginSubmit } = useAuth();
description="Veuillez vous identifier."
icon="i-lucide-user"
loading-auto
@submit="onLoginSubmit"
@submit="login"
/>
</template>

View File

@@ -1,5 +1,5 @@
<script setup lang="ts">
const { onLogoutClick } = useAuth();
const { logout } = useAuth();
</script>
<template>
@@ -18,7 +18,7 @@ const { onLogoutClick } = useAuth();
loading-auto
to="/auth"
label="Déconnexion"
@click="onLogoutClick"
@click="logout"
/>
</div>
</template>

View File

@@ -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<LoginOutput>) {
async function login({ data: args }: FormSubmitEvent<LoginOutput>) {
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 };
}

View File

@@ -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 };
}