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 };
|
||||
}
|
||||
});
|
||||
@@ -17146,6 +17146,18 @@ type RootMutation {
|
||||
"""Input for the updateUser mutation"""
|
||||
input: UpdateUserInput!
|
||||
): UpdateUserPayload
|
||||
|
||||
"""The userSwitchBack mutation"""
|
||||
userSwitchBack(
|
||||
"""Input for the userSwitchBack mutation"""
|
||||
input: UserSwitchBackInput!
|
||||
): UserSwitchBackPayload
|
||||
|
||||
"""The userSwitchTo mutation"""
|
||||
userSwitchTo(
|
||||
"""Input for the userSwitchTo mutation"""
|
||||
input: UserSwitchToInput!
|
||||
): UserSwitchToPayload
|
||||
}
|
||||
|
||||
"""The root entry point into the Graph"""
|
||||
@@ -24625,6 +24637,53 @@ enum UserRoleEnum {
|
||||
SUBSCRIBER
|
||||
}
|
||||
|
||||
"""Input for the userSwitchBack mutation."""
|
||||
input UserSwitchBackInput {
|
||||
"""
|
||||
This is an ID that can be passed to a mutation by the client to track the progress of mutations and catch possible duplicate mutation submissions.
|
||||
"""
|
||||
clientMutationId: String
|
||||
}
|
||||
|
||||
"""The payload for the userSwitchBack mutation."""
|
||||
type UserSwitchBackPayload {
|
||||
"""
|
||||
If a 'clientMutationId' input is provided to the mutation, it will be returned as output on the mutation. This ID can be used by the client to track the progress of mutations and catch possible duplicate mutation submissions.
|
||||
"""
|
||||
clientMutationId: String
|
||||
|
||||
"""Whether switching back was successful"""
|
||||
success: Boolean
|
||||
}
|
||||
|
||||
"""Input for the userSwitchTo mutation."""
|
||||
input UserSwitchToInput {
|
||||
"""
|
||||
This is an ID that can be passed to a mutation by the client to track the progress of mutations and catch possible duplicate mutation submissions.
|
||||
"""
|
||||
clientMutationId: String
|
||||
|
||||
"""The ID of the user to switch to"""
|
||||
userId: ID
|
||||
}
|
||||
|
||||
"""The payload for the userSwitchTo mutation."""
|
||||
type UserSwitchToPayload {
|
||||
"""JWT Token for the target user"""
|
||||
authToken: String
|
||||
|
||||
"""
|
||||
If a 'clientMutationId' input is provided to the mutation, it will be returned as output on the mutation. This ID can be used by the client to track the progress of mutations and catch possible duplicate mutation submissions.
|
||||
"""
|
||||
clientMutationId: String
|
||||
|
||||
"""JWT Refresh Token for the target user"""
|
||||
refreshToken: String
|
||||
|
||||
"""The target user object"""
|
||||
user: User
|
||||
}
|
||||
|
||||
"""Connection between the User type and the Comment type"""
|
||||
type UserToCommentConnection implements CommentConnection & Connection {
|
||||
"""Edges for the UserToCommentConnection connection"""
|
||||
|
||||
5
wp-content/themes/ccat/server/graphql/userSwitchBack.gql
Normal file
5
wp-content/themes/ccat/server/graphql/userSwitchBack.gql
Normal file
@@ -0,0 +1,5 @@
|
||||
mutation userSwitchBack {
|
||||
userSwitchBack(input: {}) {
|
||||
success
|
||||
}
|
||||
}
|
||||
17
wp-content/themes/ccat/server/graphql/userSwitchTo.gql
Normal file
17
wp-content/themes/ccat/server/graphql/userSwitchTo.gql
Normal file
@@ -0,0 +1,17 @@
|
||||
mutation userSwitchTo($userId: ID!) {
|
||||
userSwitchTo(input: { userId: $userId }) {
|
||||
authToken
|
||||
refreshToken
|
||||
user {
|
||||
id
|
||||
databaseId
|
||||
username
|
||||
email
|
||||
firstName
|
||||
lastName
|
||||
avatar {
|
||||
url
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user