generated from pascalmartineau/wp-skeleton
feat: isRedirecting auth
All checks were successful
Deploy WordPress and Nuxt / deploy (push) Successful in 1m1s
All checks were successful
Deploy WordPress and Nuxt / deploy (push) Successful in 1m1s
This commit is contained in:
@@ -11,12 +11,22 @@ export function useAuth() {
|
||||
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 router = useRouter();
|
||||
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 = [
|
||||
{
|
||||
@@ -35,14 +45,12 @@ export function useAuthActions() {
|
||||
];
|
||||
async function login({ data: args }: FormSubmitEvent<LoginOutput>, redirect?: string) {
|
||||
try {
|
||||
const redirectTo = redirect || routeRedirect || "/";
|
||||
const { data, errors } = await useGraphqlMutation("login", args);
|
||||
if (errors.length || !data.login) {
|
||||
console.error(errors);
|
||||
throw new Error("Une erreur est survenue.");
|
||||
}
|
||||
await refreshUserSession();
|
||||
await router.push(redirectTo);
|
||||
await redirectTo(redirect);
|
||||
}
|
||||
catch (error) {
|
||||
const message = error instanceof Error ? error.message : "Une erreur est survenue.";
|
||||
@@ -53,14 +61,11 @@ export function useAuthActions() {
|
||||
// Logout
|
||||
async function logout(redirect?: string) {
|
||||
try {
|
||||
const redirectTo = redirect || routeRedirect || "/";
|
||||
console.log(redirectTo);
|
||||
const result = await $fetch("/api/logout", { method: "POST" });
|
||||
if (!result.success) {
|
||||
throw new Error("Une erreur est survenue.");
|
||||
}
|
||||
await refreshUserSession();
|
||||
await router.push(redirectTo);
|
||||
await redirectTo(redirect);
|
||||
}
|
||||
catch (error) {
|
||||
const message = error instanceof Error ? error.message : "Une erreur est survenue.";
|
||||
@@ -71,15 +76,11 @@ export function useAuthActions() {
|
||||
// Switch to
|
||||
async function switchTo(userId: number, redirect?: string) {
|
||||
try {
|
||||
const redirectTo = redirect || routeRedirect;
|
||||
const { data, errors } = await useGraphqlMutation("switchTo", { userId });
|
||||
if (errors.length || !data.switchTo) {
|
||||
throw new Error("Une erreur est survenue");
|
||||
}
|
||||
await refreshUserSession();
|
||||
if (redirectTo) {
|
||||
await router.push(redirectTo);
|
||||
}
|
||||
await redirectTo(redirect);
|
||||
}
|
||||
catch (error) {
|
||||
const message = error instanceof Error ? error.message : "Une erreur est survenue";
|
||||
@@ -90,15 +91,11 @@ export function useAuthActions() {
|
||||
// Switch back
|
||||
async function switchBack(redirect?: string) {
|
||||
try {
|
||||
const redirectTo = redirect || routeRedirect;
|
||||
const result = await $fetch("/api/switch-back", { method: "POST" });
|
||||
if (!result.success) {
|
||||
throw new Error("Une erreur est survenue.");
|
||||
}
|
||||
await refreshUserSession();
|
||||
if (redirectTo) {
|
||||
await router.push(redirectTo);
|
||||
}
|
||||
await redirectTo(redirect);
|
||||
}
|
||||
catch (error) {
|
||||
const message = error instanceof Error ? error.message : "Une erreur est survenue";
|
||||
@@ -106,5 +103,5 @@ export function useAuthActions() {
|
||||
}
|
||||
}
|
||||
|
||||
return { loginFields, login, logout, switchTo, switchBack };
|
||||
return { isRedirecting, loginFields, login, logout, switchTo, switchBack };
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user