generated from pascalmartineau/wp-skeleton
feat: Initial user switching mutations
This commit is contained in:
38
wp-content/themes/ccat/app/composables/useUserSwitching.ts
Normal file
38
wp-content/themes/ccat/app/composables/useUserSwitching.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
export const useUserSwitching = () => {
|
||||
const session = useUserSession();
|
||||
|
||||
const isSwitched = computed(() => Boolean(session.data.value?.switchedBy));
|
||||
|
||||
const switchTo = async (userId: string | number) => {
|
||||
const response = await $fetch("/api/switch-to", {
|
||||
method: "POST",
|
||||
body: { userId },
|
||||
});
|
||||
|
||||
if (response.success) {
|
||||
await session.fetch();
|
||||
return response;
|
||||
}
|
||||
|
||||
throw new Error(response.message || "Switch failed");
|
||||
};
|
||||
|
||||
const switchBack = async () => {
|
||||
const response = await $fetch("/api/switch-back", {
|
||||
method: "POST",
|
||||
});
|
||||
|
||||
if (response.success) {
|
||||
await session.fetch();
|
||||
return response;
|
||||
}
|
||||
|
||||
throw new Error(response.message || "Switch back failed");
|
||||
};
|
||||
|
||||
return {
|
||||
isSwitched,
|
||||
switchTo,
|
||||
switchBack,
|
||||
};
|
||||
};
|
||||
Reference in New Issue
Block a user