generated from pascalmartineau/wp-skeleton
All checks were successful
Deploy WordPress and Nuxt / deploy (push) Successful in 1m1s
108 lines
3.4 KiB
TypeScript
108 lines
3.4 KiB
TypeScript
import type { FormSubmitEvent } from "@nuxt/ui";
|
|
|
|
export function useAuth() {
|
|
const { loggedIn, session } = useUserSession();
|
|
|
|
const isLoggedIn = loggedIn;
|
|
const isSwitchedTo = computed(() => Boolean(session.value?.isSwitchedTo));
|
|
const hasRole = (role: string) => session.value?.user?.roles?.includes(role) || false;
|
|
const isAdmin = computed(() => hasRole("administrator"));
|
|
|
|
return { isLoggedIn, isSwitchedTo, hasRole, isAdmin };
|
|
}
|
|
|
|
const isRedirecting = ref(false);
|
|
|
|
export function useAuthActions() {
|
|
const { fetch: refreshUserSession } = useUserSession();
|
|
const routeRedirect = useRoute().query.redirect as string || undefined;
|
|
const toast = useToast();
|
|
|
|
async function redirectTo(to: string | undefined) {
|
|
isRedirecting.value = true;
|
|
await delay(1000);
|
|
await refreshUserSession();
|
|
await navigateTo(to || routeRedirect || "/");
|
|
await nextTick();
|
|
isRedirecting.value = false;
|
|
}
|
|
|
|
// Login
|
|
const loginFields = [
|
|
{
|
|
name: "email",
|
|
type: "text" as const,
|
|
label: "Courriel",
|
|
placeholder: "Entrez votre courriel",
|
|
required: true,
|
|
}, {
|
|
name: "password",
|
|
label: "Mot de passe",
|
|
type: "password" as const,
|
|
placeholder: "Entrez votre mot de passe",
|
|
required: true,
|
|
},
|
|
];
|
|
async function login({ data: args }: FormSubmitEvent<LoginOutput>, redirect?: string) {
|
|
try {
|
|
const { data, errors } = await useGraphqlMutation("login", args);
|
|
if (errors.length || !data.login) {
|
|
console.error(errors);
|
|
throw new Error("Une erreur est survenue.");
|
|
}
|
|
await redirectTo(redirect);
|
|
}
|
|
catch (error) {
|
|
const message = error instanceof Error ? error.message : "Une erreur est survenue.";
|
|
toast.add({ title: "Échec de la connexion", description: message, color: "error" });
|
|
}
|
|
}
|
|
|
|
// Logout
|
|
async function logout(redirect?: string) {
|
|
try {
|
|
const result = await $fetch("/api/logout", { method: "POST" });
|
|
if (!result.success) {
|
|
throw new Error("Une erreur est survenue.");
|
|
}
|
|
await redirectTo(redirect);
|
|
}
|
|
catch (error) {
|
|
const message = error instanceof Error ? error.message : "Une erreur est survenue.";
|
|
toast.add({ title: "Échec de la déconnexion", description: message, color: "error" });
|
|
}
|
|
}
|
|
|
|
// Switch to
|
|
async function switchTo(userId: number, redirect?: string) {
|
|
try {
|
|
const { data, errors } = await useGraphqlMutation("switchTo", { userId });
|
|
if (errors.length || !data.switchTo) {
|
|
throw new Error("Une erreur est survenue");
|
|
}
|
|
await redirectTo(redirect);
|
|
}
|
|
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(redirect?: string) {
|
|
try {
|
|
const result = await $fetch("/api/switch-back", { method: "POST" });
|
|
if (!result.success) {
|
|
throw new Error("Une erreur est survenue.");
|
|
}
|
|
await redirectTo(redirect);
|
|
}
|
|
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 { isRedirecting, loginFields, login, logout, switchTo, switchBack };
|
|
}
|