generated from pascalmartineau/wp-skeleton
24 lines
737 B
TypeScript
24 lines
737 B
TypeScript
export function useLogout() {
|
|
const toast = useToast();
|
|
const redirect = useRoute().query.redirect as string || "/";
|
|
const router = useRouter();
|
|
const { fetch: refreshUserSession } = useUserSession();
|
|
|
|
async function onLogoutClick() {
|
|
try {
|
|
const result = await $fetch("/api/logout", { method: "POST" });
|
|
if (!result.success) {
|
|
throw new Error("Une erreur est survenue.");
|
|
}
|
|
await router.push(redirect);
|
|
await refreshUserSession();
|
|
}
|
|
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" });
|
|
}
|
|
}
|
|
|
|
return { onLogoutClick };
|
|
}
|