generated from pascalmartineau/wp-skeleton
feat: redirect param to useAuthAction
All checks were successful
Deploy WordPress and Nuxt / deploy (push) Successful in 1m3s
All checks were successful
Deploy WordPress and Nuxt / deploy (push) Successful in 1m3s
This commit is contained in:
@@ -22,7 +22,7 @@ if (!isLoggedIn.value) {
|
||||
loading-auto
|
||||
to="#"
|
||||
label="Déconnexion"
|
||||
@click="logout"
|
||||
@click="logout()"
|
||||
/>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
@@ -13,7 +13,7 @@ export function useAuth() {
|
||||
|
||||
export function useAuthActions() {
|
||||
const { fetch: refreshUserSession } = useUserSession();
|
||||
const redirect = useRoute().query.redirect as string || "/";
|
||||
const routeRedirect = useRoute().query.redirect as string || undefined;
|
||||
const router = useRouter();
|
||||
const toast = useToast();
|
||||
|
||||
@@ -33,15 +33,16 @@ export function useAuthActions() {
|
||||
required: true,
|
||||
},
|
||||
];
|
||||
async function login({ data: args }: FormSubmitEvent<LoginOutput>) {
|
||||
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 router.push(redirect);
|
||||
await refreshUserSession();
|
||||
await router.push(redirectTo);
|
||||
}
|
||||
catch (error) {
|
||||
const message = error instanceof Error ? error.message : "Une erreur est survenue.";
|
||||
@@ -50,14 +51,16 @@ export function useAuthActions() {
|
||||
}
|
||||
|
||||
// Logout
|
||||
async function 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 router.push(redirect);
|
||||
await refreshUserSession();
|
||||
await router.push(redirectTo);
|
||||
}
|
||||
catch (error) {
|
||||
const message = error instanceof Error ? error.message : "Une erreur est survenue.";
|
||||
@@ -66,13 +69,17 @@ export function useAuthActions() {
|
||||
}
|
||||
|
||||
// Switch to
|
||||
async function switchTo(userId: string | number) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
catch (error) {
|
||||
const message = error instanceof Error ? error.message : "Une erreur est survenue";
|
||||
@@ -81,13 +88,17 @@ export function useAuthActions() {
|
||||
}
|
||||
|
||||
// Switch back
|
||||
async function switchBack() {
|
||||
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);
|
||||
}
|
||||
}
|
||||
catch (error) {
|
||||
const message = error instanceof Error ? error.message : "Une erreur est survenue";
|
||||
|
||||
Reference in New Issue
Block a user