From 1cea2fe9f8a94ce5a78e16692445b38a2cdb7712 Mon Sep 17 00:00:00 2001 From: Pascal Martineau Date: Thu, 18 Sep 2025 12:47:13 -0400 Subject: [PATCH] refactor: Use AuthUser fragment for login / swithcTo --- wp-content/themes/ccat/app/graphql/login.gql | 3 +-- .../themes/ccat/app/graphql/switchTo.gql | 3 +-- .../ccat/server/graphql/AuthUser.fragment.gql | 9 ++++++++ wp-content/themes/ccat/server/utils/auth.ts | 21 +++++++++++-------- wp-content/themes/ccat/shared/types/auth.d.ts | 1 + 5 files changed, 24 insertions(+), 13 deletions(-) create mode 100644 wp-content/themes/ccat/server/graphql/AuthUser.fragment.gql diff --git a/wp-content/themes/ccat/app/graphql/login.gql b/wp-content/themes/ccat/app/graphql/login.gql index 9c650e9..1263ad6 100644 --- a/wp-content/themes/ccat/app/graphql/login.gql +++ b/wp-content/themes/ccat/app/graphql/login.gql @@ -3,8 +3,7 @@ mutation login($email: String!, $password: String!) { authToken refreshToken user { - id - email + ...AuthUser } } } \ No newline at end of file diff --git a/wp-content/themes/ccat/app/graphql/switchTo.gql b/wp-content/themes/ccat/app/graphql/switchTo.gql index b4e4295..653282b 100644 --- a/wp-content/themes/ccat/app/graphql/switchTo.gql +++ b/wp-content/themes/ccat/app/graphql/switchTo.gql @@ -3,8 +3,7 @@ mutation switchTo($userId: ID!) { authToken refreshToken user { - id - email + ...AuthUser } } } \ No newline at end of file diff --git a/wp-content/themes/ccat/server/graphql/AuthUser.fragment.gql b/wp-content/themes/ccat/server/graphql/AuthUser.fragment.gql new file mode 100644 index 0000000..8784fb3 --- /dev/null +++ b/wp-content/themes/ccat/server/graphql/AuthUser.fragment.gql @@ -0,0 +1,9 @@ +fragment AuthUser on User { + id + email + roles { + nodes { + name + } + } +} \ No newline at end of file diff --git a/wp-content/themes/ccat/server/utils/auth.ts b/wp-content/themes/ccat/server/utils/auth.ts index fad34df..d862848 100644 --- a/wp-content/themes/ccat/server/utils/auth.ts +++ b/wp-content/themes/ccat/server/utils/auth.ts @@ -1,6 +1,7 @@ -import type { LoginRootMutation, SwitchToRootMutation } from "#graphql-operations"; +import type { LoginRootMutation, SwitchToRootMutation, AuthUserFragment } from "#graphql-operations"; import type { H3Event } from "h3"; import { pick } from "es-toolkit/compat"; +import type { User } from "#auth-utils"; export async function handleLogin(event: H3Event, loginData?: LoginRootMutation) { if (!loginData?.login?.user) { @@ -8,10 +9,7 @@ export async function handleLogin(event: H3Event, loginData?: LoginRootMutation) } const { authToken, refreshToken, user } = loginData.login; await setUserSession(event, { - user: { - id: user.id, - email: user.email, - }, + user: getAuthUser(user), secure: { authToken, refreshToken, @@ -32,10 +30,7 @@ export async function handleSwitchTo(event: H3Event, data?: SwitchToRootMutation const session = await getUserSession(event); const { authToken, refreshToken, user } = data.switchTo; await setUserSession(event, { - user: { - id: user.id, - email: user.email, - }, + user: getAuthUser(user), secure: { authToken, refreshToken, @@ -63,3 +58,11 @@ export async function handleSwitchBack(event: H3Event) { isSwitchedTo: false, }); } + +function getAuthUser(user: AuthUserFragment): User { + return { + id: Number(user.id), + email: user.email!, + roles: user.roles?.nodes.map(({ name }) => name!) || [], + }; +} diff --git a/wp-content/themes/ccat/shared/types/auth.d.ts b/wp-content/themes/ccat/shared/types/auth.d.ts index 7fadddd..1c629b7 100644 --- a/wp-content/themes/ccat/shared/types/auth.d.ts +++ b/wp-content/themes/ccat/shared/types/auth.d.ts @@ -3,6 +3,7 @@ declare module "#auth-utils" { interface User { id: number; email: string; + roles: string[]; } interface UserSession {