Skip to content

Commit

Permalink
refactor(core,schemas): relocate the profile type defs
Browse files Browse the repository at this point in the history
relocate the profile type defs
  • Loading branch information
simeng-li committed Jul 15, 2024
1 parent e52b0b3 commit 3d2f450
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { type WithLogContext } from '#src/middleware/koa-audit-log.js';
import type TenantContext from '#src/tenants/TenantContext.js';
import assertThat from '#src/utils/assert-that.js';

import type { Interaction } from '../types.js';
import { interactionProfileGuard, type Interaction, type InteractionProfile } from '../types.js';

import { getNewUserProfileFromVerificationRecord, toUserSocialIdentityData } from './utils.js';
import { ProfileValidator } from './validators/profile-validator.js';
Expand All @@ -24,14 +24,14 @@ import {
type InteractionStorage = {
interactionEvent?: InteractionEvent;
userId?: string;
profile?: Record<string, unknown>;
profile?: InteractionProfile;
verificationRecords?: VerificationRecordData[];
};

const interactionStorageGuard = z.object({
interactionEvent: z.nativeEnum(InteractionEvent).optional(),
userId: z.string().optional(),
profile: z.object({}).optional(),
profile: interactionProfileGuard.optional(),
verificationRecords: verificationRecordDataGuard.array().optional(),
}) satisfies ToZodObject<InteractionStorage>;

Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/routes/experience/classes/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ import {
SignInIdentifier,
VerificationType,
type InteractionIdentifier,
type InteractionProfile,
type User,
} from '@logto/schemas';

import RequestError from '#src/errors/RequestError/index.js';
import type Queries from '#src/tenants/Queries.js';

import type { InteractionProfile } from '../types.js';

import { type VerificationRecord } from './verifications/index.js';

export const findUserByIdentifier = async (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type { InteractionProfile } from '@logto/schemas';

import RequestError from '#src/errors/RequestError/index.js';
import type Libraries from '#src/tenants/Libraries.js';
import type Queries from '#src/tenants/Queries.js';
import assertThat from '#src/utils/assert-that.js';

import type { InteractionProfile } from '../../types.js';

export class ProfileValidator {
constructor(
private readonly libraries: Libraries,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { socialUserInfoGuard, type SocialUserInfo, type ToZodObject } from '@log
import {
VerificationType,
type CreateUserSsoIdentity,
type InteractionProfile,
type JsonObject,
type SocialAuthorizationUrlPayload,
type SupportedSsoConnector,
Expand All @@ -23,6 +22,8 @@ import type Queries from '#src/tenants/Queries.js';
import type TenantContext from '#src/tenants/TenantContext.js';
import assertThat from '#src/utils/assert-that.js';

import type { InteractionProfile } from '../../types.js';

import { type IdentifierVerificationRecord } from './verification-record.js';

/** The JSON data type for the EnterpriseSsoVerification record stored in the interaction storage */
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { socialUserInfoGuard, type SocialUserInfo, type ToZodObject } from '@logto/connector-kit';
import {
VerificationType,
type InteractionProfile,
type JsonObject,
type SocialAuthorizationUrlPayload,
type User,
Expand All @@ -21,6 +20,8 @@ import type TenantContext from '#src/tenants/TenantContext.js';
import assertThat from '#src/utils/assert-that.js';
import { type LogtoConnector } from '#src/utils/connectors/types.js';

import type { InteractionProfile } from '../../types.js';

import { type IdentifierVerificationRecord } from './verification-record.js';

/** The JSON data type for the SocialVerification record stored in the interaction storage */
Expand Down
50 changes: 50 additions & 0 deletions packages/core/src/routes/experience/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,53 @@
import { type SocialUserInfo, socialUserInfoGuard, type ToZodObject } from '@logto/connector-kit';
import { type CreateUser, Users, UserSsoIdentities, type UserSsoIdentity } from '@logto/schemas';
import type Provider from 'oidc-provider';
import { z } from 'zod';

export type Interaction = Awaited<ReturnType<Provider['interactionDetails']>>;

export type InteractionProfile = {
socialIdentity?: {
target: string;
userInfo: SocialUserInfo;
};
enterpriseSsoIdentity?: Pick<
UserSsoIdentity,
'identityId' | 'ssoConnectorId' | 'issuer' | 'detail'
>;
} & Pick<
CreateUser,
| 'avatar'
| 'name'
| 'username'
| 'primaryEmail'
| 'primaryPhone'
| 'passwordEncrypted'
| 'passwordEncryptionMethod'
>;

export const interactionProfileGuard = Users.createGuard
.pick({
avatar: true,
name: true,
username: true,
primaryEmail: true,
primaryPhone: true,
passwordEncrypted: true,
passwordEncryptionMethod: true,
})
.extend({
socialIdentity: z
.object({
target: z.string(),
userInfo: socialUserInfoGuard,
})
.optional(),
enterpriseSsoIdentity: UserSsoIdentities.guard
.pick({
identityId: true,
ssoConnectorId: true,
issuer: true,
detail: true,
})
.optional(),
}) satisfies ToZodObject<InteractionProfile>;
52 changes: 0 additions & 52 deletions packages/schemas/src/types/interactions.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
/* eslint-disable max-lines */
import { socialUserInfoGuard, type SocialUserInfo } from '@logto/connector-kit';
import { emailRegEx, phoneRegEx, usernameRegEx } from '@logto/core-kit';
import { z } from 'zod';

import { UserSsoIdentities, type UserSsoIdentity } from '../db-entries/user-sso-identity.js';
import { Users, type CreateUser } from '../db-entries/user.js';
import {
MfaFactor,
SignInIdentifier,
Expand Down Expand Up @@ -67,53 +63,6 @@ export enum VerificationType {
BackupCode = 'BackupCode',
}

export type InteractionProfile = {
socialIdentity?: {
target: string;
userInfo: SocialUserInfo;
};
enterpriseSsoIdentity?: Pick<
UserSsoIdentity,
'identityId' | 'ssoConnectorId' | 'issuer' | 'detail'
>;
} & Pick<
CreateUser,
| 'avatar'
| 'name'
| 'username'
| 'primaryEmail'
| 'primaryPhone'
| 'passwordEncrypted'
| 'passwordEncryptionMethod'
>;

export const interactionProfileGuard = Users.createGuard
.pick({
avatar: true,
name: true,
username: true,
primaryEmail: true,
primaryPhone: true,
passwordEncrypted: true,
passwordEncryptionMethod: true,
})
.extend({
socialIdentity: z
.object({
target: z.string(),
userInfo: socialUserInfoGuard,
})
.optional(),
enterpriseSsoIdentity: UserSsoIdentities.guard
.pick({
identityId: true,
ssoConnectorId: true,
issuer: true,
detail: true,
})
.optional(),
}) satisfies ToZodObject<InteractionProfile>;

// REMARK: API payload guard

/** Payload type for `POST /api/experience/verification/{social|sso}/:connectorId/authorization-uri`. */
Expand Down Expand Up @@ -430,4 +379,3 @@ export const verifyMfaResultGuard = z.object({
});

export type VerifyMfaResult = z.infer<typeof verifyMfaResultGuard>;
/* eslint-enable max-lines */

0 comments on commit 3d2f450

Please sign in to comment.