0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2024-12-16 20:26:19 -05:00

refactor(core): update user context type

This commit is contained in:
Darcy Ye 2024-03-19 16:01:55 +08:00
parent d3d0f5133b
commit 348124b60e
No known key found for this signature in database
GPG key ID: B46F4C07EDEFC610
3 changed files with 37 additions and 27 deletions

View file

@ -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. organizations: await Promise.all(
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment organizationsWithRoles.map(async ({ organizationRoles, ...organization }) => ({
organizations: Object.fromEntries( id: organization.id,
await Promise.all( roles: await Promise.all(
organizationsWithRoles.map(async ({ organizationRoles, ...organization }) => [ organizationRoles.map(async ({ id, name }) => {
organization.id, const [_, fullOrganizationScopes] = await relations.rolesScopes.getEntities(
{ OrganizationScopes,
roles: await Promise.all( { organizationRoleId: id }
organizationRoles.map(async ({ id, name }) => { );
const [_, fullOrganizationScopes] = await relations.rolesScopes.getEntities( return {
OrganizationScopes, id,
{ organizationRoleId: id } name,
); scopes: fullOrganizationScopes.map(pickState('id', 'name', 'description')),
return { };
id, })
name, ),
scopes: fullOrganizationScopes.map(pickState('id', 'name', 'description')), }))
};
})
),
},
])
)
), ),
}; };

View file

@ -14,6 +14,7 @@ import { jwtCustomizerGuard } from './logto-config/index.js';
import { userInfoGuard } from './user.js'; import { userInfoGuard } from './user.js';
const organizationDetailGuard = z.object({ const organizationDetailGuard = z.object({
id: z.string(),
roles: z.array( roles: z.array(
OrganizationRoles.guard.pick({ id: true, name: true }).extend({ OrganizationRoles.guard.pick({ id: true, name: true }).extend({
scopes: z.array(OrganizationScopes.guard.pick({ id: true, name: true, description: true })), 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<typeof jwtCustomizerUserContextGuard>; export type JwtCustomizerUserContext = z.infer<typeof jwtCustomizerUserContextGuard>;

View file

@ -18,9 +18,24 @@ export const userInfoSelectFields = Object.freeze([
'isSuspended', 'isSuspended',
] as const); ] 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<typeof userInfoGuard>; export type UserInfo = z.infer<typeof userInfoGuard>;