diff --git a/wp-content/themes/ccat/app/composables/useUserSwitching.ts b/wp-content/themes/ccat/app/composables/useUserSwitching.ts index ab2164c..fdc9ed7 100644 --- a/wp-content/themes/ccat/app/composables/useUserSwitching.ts +++ b/wp-content/themes/ccat/app/composables/useUserSwitching.ts @@ -7,8 +7,8 @@ export function useUserSwitching() { async function userSwitchTo(userId: string | number) { try { - const { data, errors } = await useGraphqlMutation("userSwitchTo", { userId }); - if (errors.length || !data.userSwitchTo) { + const { data, errors } = await useGraphqlMutation("switchTo", { userId }); + if (errors.length || !data.switchTo) { throw new Error("Une erreur est survenue"); } await refreshUserSession(); diff --git a/wp-content/themes/ccat/app/graphql/switchTo.gql b/wp-content/themes/ccat/app/graphql/switchTo.gql new file mode 100644 index 0000000..b4e4295 --- /dev/null +++ b/wp-content/themes/ccat/app/graphql/switchTo.gql @@ -0,0 +1,10 @@ +mutation switchTo($userId: ID!) { + switchTo(input: { userId: $userId }) { + authToken + refreshToken + user { + id + email + } + } +} \ No newline at end of file diff --git a/wp-content/themes/ccat/app/graphql/userSwitchTo.gql b/wp-content/themes/ccat/app/graphql/userSwitchTo.gql deleted file mode 100644 index 9eac404..0000000 --- a/wp-content/themes/ccat/app/graphql/userSwitchTo.gql +++ /dev/null @@ -1,10 +0,0 @@ -mutation userSwitchTo($userId: ID!) { - userSwitchTo(input: { userId: $userId }) { - authToken - refreshToken - user { - id - email - } - } -} \ No newline at end of file diff --git a/wp-content/themes/ccat/functions.php b/wp-content/themes/ccat/functions.php index 8d487e5..a4722dd 100644 --- a/wp-content/themes/ccat/functions.php +++ b/wp-content/themes/ccat/functions.php @@ -23,9 +23,9 @@ require_once __DIR__ . '/includes/taxonomies/resource-category.php'; // Forms // WPGraphQL +require_once __DIR__ . '/includes/graphql/auth.php'; require_once __DIR__ . '/includes/graphql/address.php'; require_once __DIR__ . '/includes/graphql/breadcrumbs.php'; -require_once __DIR__ . '/includes/graphql/user-switching.php'; // Roles diff --git a/wp-content/themes/ccat/includes/graphql/user-switching.php b/wp-content/themes/ccat/includes/graphql/auth.php similarity index 70% rename from wp-content/themes/ccat/includes/graphql/user-switching.php rename to wp-content/themes/ccat/includes/graphql/auth.php index 298ac86..d93d2ec 100644 --- a/wp-content/themes/ccat/includes/graphql/user-switching.php +++ b/wp-content/themes/ccat/includes/graphql/auth.php @@ -1,29 +1,29 @@ array( 'userId' => array( 'type' => 'ID', - 'description' => esc_html__( 'The ID of the user to switch to', 'ccat' ), + 'description' => 'The ID of the user to switch to', ), ), 'outputFields' => array( 'authToken' => array( 'type' => 'String', - 'description' => esc_html__( 'JWT Token for the target user', 'ccat' ), + 'description' => 'JWT Token for the target user', ), 'refreshToken' => array( 'type' => 'String', - 'description' => esc_html__( 'JWT Refresh Token for the target user', 'ccat' ), + 'description' => 'JWT Refresh Token for the target user', ), 'user' => array( 'type' => 'User', - 'description' => esc_html__( 'The target user object', 'ccat' ), + 'description' => 'The target user object', ), ), 'mutateAndGetPayload' => 'ccat_graphql_switch_to_mutation', @@ -31,19 +31,19 @@ function ccat_graphql_register_user_switch_to() { ); } -// Callback for userSwitchTo mutation +// Handle switchTo mutation function ccat_graphql_switch_to_mutation( $input ) { if ( ! is_user_logged_in() || ! current_user_can( 'manage_options' ) ) { - throw new \GraphQL\Error\UserError( esc_html__( 'Insufficient permissions', 'ccat' ) ); + throw new \GraphQL\Error\UserError( "Permissions insuffisantes pour se connecter en tant qu'un autre utilisateur." ); } $user_id = absint( $input['userId'] ); $current_user_id = get_current_user_id(); if ( $user_id === $current_user_id ) { - throw new \GraphQL\Error\UserError( esc_html__( 'Cannot switch to yourself', 'ccat' ) ); + throw new \GraphQL\Error\UserError( 'Impossible de se connecter en tant que son propre utilisateur.' ); } $target_user = get_user_by( 'ID', $user_id ); if ( ! $target_user ) { - throw new \GraphQL\Error\UserError( esc_html__( 'User not found', 'ccat' ) ); + throw new \GraphQL\Error\UserError( 'Utilisateur introuvable.' ); } $secret_key = defined( 'GRAPHQL_JWT_AUTH_SECRET_KEY' ) ? GRAPHQL_JWT_AUTH_SECRET_KEY : wp_salt(); $issued_at = time(); diff --git a/wp-content/themes/ccat/server/api/logout.post.ts b/wp-content/themes/ccat/server/api/logout.post.ts index 5fbc2a7..7d784a9 100644 --- a/wp-content/themes/ccat/server/api/logout.post.ts +++ b/wp-content/themes/ccat/server/api/logout.post.ts @@ -1,6 +1,12 @@ import { defineEventHandler } from "h3"; export default defineEventHandler(async (event) => { - await clearUserSession(event); - return { success: true }; + try { + await handleLogout(event); + return { success: true, message: "Déconnexion réussie" }; + } + catch (error) { + const message = error instanceof Error ? error.message : "Une erreur est survenue."; + return { success: false, message }; + } }); diff --git a/wp-content/themes/ccat/server/graphqlMiddleware.serverOptions.ts b/wp-content/themes/ccat/server/graphqlMiddleware.serverOptions.ts index c24893b..09d9406 100644 --- a/wp-content/themes/ccat/server/graphqlMiddleware.serverOptions.ts +++ b/wp-content/themes/ccat/server/graphqlMiddleware.serverOptions.ts @@ -1,6 +1,6 @@ import { defineGraphqlServerOptions } from "nuxt-graphql-middleware/server-options"; import { jwtDecode } from "jwt-decode"; -import type { LoginRootMutation, UserSwitchToRootMutation } from "#graphql-operations"; +import type { LoginRootMutation, SwitchToRootMutation } from "#graphql-operations"; interface DecodedToken { exp: number; @@ -37,8 +37,8 @@ export default defineGraphqlServerOptions({ case "login": await handleLogin(event, response._data!.data as LoginRootMutation); break; - case "userSwitchTo": - await handleSwitchTo(event, response._data!.data as UserSwitchToRootMutation); + case "switchTo": + await handleSwitchTo(event, response._data!.data as SwitchToRootMutation); break; } return response._data!; diff --git a/wp-content/themes/ccat/server/schema.graphql b/wp-content/themes/ccat/server/schema.graphql index cc4f2a2..3ab8b94 100644 --- a/wp-content/themes/ccat/server/schema.graphql +++ b/wp-content/themes/ccat/server/schema.graphql @@ -17463,6 +17463,12 @@ type RootMutation { input: SendPasswordResetEmailInput! ): SendPasswordResetEmailPayload + """The switchTo mutation""" + switchTo( + """Input for the switchTo mutation""" + input: SwitchToInput! + ): SwitchToPayload + """The updateCategory mutation""" updateCategory( """Input for the updateCategory mutation""" @@ -17582,12 +17588,6 @@ type RootMutation { """Input for the updateUser mutation""" input: UpdateUserInput! ): UpdateUserPayload - - """The userSwitchTo mutation""" - userSwitchTo( - """Input for the userSwitchTo mutation""" - input: UserSwitchToInput! - ): UserSwitchToPayload } """The root entry point into the Graph""" @@ -21777,6 +21777,34 @@ type SiteOptions implements AcfOptionsPage & Node & WithAcfGroupCcat { parentId: String } +"""Input for the switchTo mutation.""" +input SwitchToInput { + """ + 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 switchTo mutation.""" +type SwitchToPayload { + """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 +} + """ A taxonomy term used to organize and classify content. Tags do not have a hierarchy and are generally used for more specific classifications. """ @@ -24947,34 +24975,6 @@ enum UserRoleEnum { TRANSLATOR } -"""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""" diff --git a/wp-content/themes/ccat/server/utils/auth.ts b/wp-content/themes/ccat/server/utils/auth.ts index 9e950e5..31a9c74 100644 --- a/wp-content/themes/ccat/server/utils/auth.ts +++ b/wp-content/themes/ccat/server/utils/auth.ts @@ -1,4 +1,4 @@ -import type { LoginRootMutation, UserSwitchToRootMutation } from "#graphql-operations"; +import type { LoginRootMutation, SwitchToRootMutation } from "#graphql-operations"; import type { H3Event } from "h3"; import { pick } from "es-toolkit/compat"; @@ -25,7 +25,7 @@ export async function handleLogout(event: H3Event) { await clearUserSession(event); } -export async function handleSwitchTo(event: H3Event, data?: UserSwitchToRootMutation) { +export async function handleSwitchTo(event: H3Event, data?: SwitchToRootMutation) { if (!data?.userSwitchTo?.user) { return; }