generated from pascalmartineau/wp-skeleton
refactor: use onServerResponse for auth instead of server api
This commit is contained in:
@@ -22,11 +22,12 @@ export function useAuth() {
|
||||
required: true,
|
||||
},
|
||||
];
|
||||
async function onLoginSubmit({ data }: FormSubmitEvent<LoginOutput>) {
|
||||
async function onLoginSubmit({ data: args }: FormSubmitEvent<LoginOutput>) {
|
||||
try {
|
||||
const result = await $fetch<{ success: boolean; message?: string }>("/api/login", { method: "POST", body: data });
|
||||
if (!result.success) {
|
||||
throw new Error(result.message || "Une erreur est survenue.");
|
||||
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();
|
||||
|
||||
@@ -7,40 +7,29 @@ export function useUserSwitching() {
|
||||
|
||||
async function userSwitchTo(userId: string | number) {
|
||||
try {
|
||||
const result = await $fetch<{ success: boolean; message?: string }>("/api/switch-to", {
|
||||
method: "POST",
|
||||
body: { userId },
|
||||
});
|
||||
|
||||
if (!result.success) {
|
||||
throw new Error(result.message || "Switch failed");
|
||||
const { data, errors } = await useGraphqlMutation("userSwitchTo", { userId });
|
||||
if (errors.length || !data.userSwitchTo) {
|
||||
throw new Error("Une erreur est survenue");
|
||||
}
|
||||
|
||||
await refreshUserSession();
|
||||
return result;
|
||||
}
|
||||
catch (error) {
|
||||
const message = error instanceof Error ? error.message : "Switch failed";
|
||||
const message = error instanceof Error ? error.message : "Une erreur est survenue";
|
||||
toast.add({ title: "Échec du changement d'utilisateur", description: message, color: "error" });
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async function userSwitchBack() {
|
||||
try {
|
||||
const result = await $fetch<{ success: boolean; message?: string }>("/api/switch-back", {
|
||||
method: "POST",
|
||||
});
|
||||
const result = await $fetch("/api/switch-back", { method: "POST" });
|
||||
if (!result.success) {
|
||||
throw new Error(result.message || "Échec du retour à l'utilisateur précédent");
|
||||
throw new Error("Une erreur est survenue.");
|
||||
}
|
||||
await refreshUserSession();
|
||||
return result;
|
||||
}
|
||||
catch (error) {
|
||||
const message = error instanceof Error ? error.message : "Échec du retour à l'utilisateur précédent";
|
||||
const message = error instanceof Error ? error.message : "Une erreur est survenue";
|
||||
toast.add({ title: "Échec du changement d'utilisateur", description: message, color: "error" });
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
10
wp-content/themes/ccat/app/graphql/login.gql
Normal file
10
wp-content/themes/ccat/app/graphql/login.gql
Normal file
@@ -0,0 +1,10 @@
|
||||
mutation login($email: String!, $password: String!) {
|
||||
login(input: { username: $email, password: $password }) {
|
||||
authToken
|
||||
refreshToken
|
||||
user {
|
||||
id
|
||||
email
|
||||
}
|
||||
}
|
||||
}
|
||||
10
wp-content/themes/ccat/app/graphql/userSwitchTo.gql
Normal file
10
wp-content/themes/ccat/app/graphql/userSwitchTo.gql
Normal file
@@ -0,0 +1,10 @@
|
||||
mutation userSwitchTo($userId: ID!) {
|
||||
userSwitchTo(input: { userId: $userId }) {
|
||||
authToken
|
||||
refreshToken
|
||||
user {
|
||||
id
|
||||
email
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user