From 348124b60e67ea017ba60e83a645310ffd07ce82 Mon Sep 17 00:00:00 2001 From: Darcy Ye Date: Tue, 19 Mar 2024 16:01:55 +0800 Subject: [PATCH] refactor(core): update user context type --- packages/core/src/libraries/jwt-customizer.ts | 40 ++++++++----------- packages/schemas/src/types/jwt-customizer.ts | 3 +- packages/schemas/src/types/user.ts | 21 ++++++++-- 3 files changed, 37 insertions(+), 27 deletions(-) diff --git a/packages/core/src/libraries/jwt-customizer.ts b/packages/core/src/libraries/jwt-customizer.ts index 2fba78875..e51270a07 100644 --- a/packages/core/src/libraries/jwt-customizer.ts +++ b/packages/core/src/libraries/jwt-customizer.ts @@ -49,29 +49,23 @@ export const createJwtCustomizerLibrary = (queries: Queries, userLibrary: UserLi }; }) ), - // No need to deal with the type here, the type will be enforced by the guard when return the result. - // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment - organizations: Object.fromEntries( - await Promise.all( - organizationsWithRoles.map(async ({ organizationRoles, ...organization }) => [ - organization.id, - { - roles: await Promise.all( - organizationRoles.map(async ({ id, name }) => { - const [_, fullOrganizationScopes] = await relations.rolesScopes.getEntities( - OrganizationScopes, - { organizationRoleId: id } - ); - return { - id, - name, - scopes: fullOrganizationScopes.map(pickState('id', 'name', 'description')), - }; - }) - ), - }, - ]) - ) + organizations: await Promise.all( + organizationsWithRoles.map(async ({ organizationRoles, ...organization }) => ({ + id: organization.id, + roles: await Promise.all( + organizationRoles.map(async ({ id, name }) => { + const [_, fullOrganizationScopes] = await relations.rolesScopes.getEntities( + OrganizationScopes, + { organizationRoleId: id } + ); + return { + id, + name, + scopes: fullOrganizationScopes.map(pickState('id', 'name', 'description')), + }; + }) + ), + })) ), }; diff --git a/packages/schemas/src/types/jwt-customizer.ts b/packages/schemas/src/types/jwt-customizer.ts index ca4b5c551..6d6e0b733 100644 --- a/packages/schemas/src/types/jwt-customizer.ts +++ b/packages/schemas/src/types/jwt-customizer.ts @@ -14,6 +14,7 @@ import { jwtCustomizerGuard } from './logto-config/index.js'; import { userInfoGuard } from './user.js'; const organizationDetailGuard = z.object({ + id: z.string(), roles: z.array( OrganizationRoles.guard.pick({ id: true, name: true }).extend({ scopes: z.array(OrganizationScopes.guard.pick({ id: true, name: true, description: true })), @@ -37,7 +38,7 @@ export const jwtCustomizerUserContextGuard = userInfoGuard.extend({ ), }) ), - organizations: z.record(organizationDetailGuard), + organizations: z.array(organizationDetailGuard), }); export type JwtCustomizerUserContext = z.infer; diff --git a/packages/schemas/src/types/user.ts b/packages/schemas/src/types/user.ts index d51d16203..ea059930f 100644 --- a/packages/schemas/src/types/user.ts +++ b/packages/schemas/src/types/user.ts @@ -18,9 +18,24 @@ export const userInfoSelectFields = Object.freeze([ 'isSuspended', ] as const); -export const userInfoGuard = Users.guard.pick( - Object.fromEntries(userInfoSelectFields.map((key) => [key, true])) -); +/** + * The `pick` method of previous implementation will be overridden by `merge`/`extend` method, should explicitly specify keys in `pick` method. + * DO REMEMBER TO UPDATE THIS GUARD WHEN YOU UPDATE `userInfoSelectFields`. + */ +export const userInfoGuard = Users.guard.pick({ + id: true, + username: true, + primaryEmail: true, + primaryPhone: true, + name: true, + avatar: true, + customData: true, + identities: true, + lastSignInAt: true, + createdAt: true, + applicationId: true, + isSuspended: true, +}); export type UserInfo = z.infer;