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.
// 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')),
};
})
),
}))
),
};

View file

@ -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<typeof jwtCustomizerUserContextGuard>;

View file

@ -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<typeof userInfoGuard>;