generated from pascalmartineau/wp-skeleton
39 lines
815 B
TypeScript
39 lines
815 B
TypeScript
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,
|
|
};
|
|
};
|