generated from pascalmartineau/wp-skeleton
feat: Initial user switching mutations
This commit is contained in:
19
wp-content/themes/ccat/server/api/switch-back.post.ts
Normal file
19
wp-content/themes/ccat/server/api/switch-back.post.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
import { defineEventHandler } from "h3";
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
try {
|
||||
const response = await useGraphqlMutation("userSwitchBack");
|
||||
|
||||
if (response.errors?.length) {
|
||||
throw new Error(response.errors[0]?.message || "Switch back failed");
|
||||
}
|
||||
|
||||
await clearUserSession(event);
|
||||
|
||||
return { success: true };
|
||||
}
|
||||
catch (error) {
|
||||
const message = error instanceof Error ? error.message : "Switch back failed";
|
||||
return { success: false, message };
|
||||
}
|
||||
});
|
||||
33
wp-content/themes/ccat/server/api/switch-to.post.ts
Normal file
33
wp-content/themes/ccat/server/api/switch-to.post.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import { defineEventHandler, readBody } from "h3";
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const { userId } = await readBody(event);
|
||||
|
||||
try {
|
||||
const currentSession = await getUserSession(event);
|
||||
if (!currentSession?.user) {
|
||||
throw new Error("Authentication required");
|
||||
}
|
||||
|
||||
const response = await useGraphqlMutation("userSwitchTo", { userId });
|
||||
|
||||
if (response.errors?.length) {
|
||||
throw new Error(response.errors[0]?.message || "Switch failed");
|
||||
}
|
||||
|
||||
const { authToken, refreshToken, user } = response.data.userSwitchTo;
|
||||
|
||||
await setUserSession(event, {
|
||||
user,
|
||||
secure: { authToken, refreshToken },
|
||||
loggedInAt: new Date().toISOString(),
|
||||
switchedBy: currentSession.user.id,
|
||||
});
|
||||
|
||||
return { success: true };
|
||||
}
|
||||
catch (error) {
|
||||
const message = error instanceof Error ? error.message : "Switch failed";
|
||||
return { success: false, message };
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user