Skip to content

Commit

Permalink
feat(schemas): add verification record table
Browse files Browse the repository at this point in the history
  • Loading branch information
wangsijie committed Sep 12, 2024
1 parent b639249 commit f1ea3d5
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { sql } from '@silverhand/slonik';

import type { AlterationScript } from '../lib/types/alteration.js';

import { applyTableRls, dropTableRls } from './utils/1704934999-tables.js';

const alteration: AlterationScript = {
up: async (pool) => {
await pool.query(sql`
create table verification_records (
tenant_id varchar(21) not null
references tenants (id) on update cascade on delete cascade,
id varchar(21) not null,
user_id varchar(21)
references users (id) on update cascade on delete cascade,
created_at timestamptz not null default(now()),
type varchar(255) /* @use VerificationType */ not null,
verified_at timestamptz,
expires_at timestamptz not null,
data jsonb /* @use VerificationRecordData */ not null default '{}'::jsonb,
primary key (id)
);
create index verification_records__id
on verification_records (tenant_id, id);
`);
await applyTableRls(pool, 'verification_records');
},
down: async (pool) => {
await dropTableRls(pool, 'verification_records');
await pool.query(sql`
drop table verification_records;
`);
},
};

export default alteration;
1 change: 1 addition & 0 deletions packages/schemas/src/foundations/jsonb-types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export * from './sentinel.js';
export * from './users.js';
export * from './sso-connector.js';
export * from './applications.js';
export * from './verification-records.js';

export {
configurableConnectorMetadataGuard,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { z } from 'zod';

export enum VerificationType {
Password = 'Password',
EmailVerificationCode = 'EmailVerificationCode',
PhoneVerificationCode = 'PhoneVerificationCode',
Social = 'Social',
EnterpriseSso = 'EnterpriseSso',
TOTP = 'Totp',
WebAuthn = 'WebAuthn',
BackupCode = 'BackupCode',
NewPasswordIdentity = 'NewPasswordIdentity',
}

export const verificationTypeGuard = z.nativeEnum(VerificationType);
13 changes: 0 additions & 13 deletions packages/schemas/src/types/interactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,6 @@ export const verificationCodeIdentifierGuard = z.object({
value: z.string(),
}) satisfies ToZodObject<VerificationCodeIdentifier>;

/** Logto supported interaction verification types. */
export enum VerificationType {
Password = 'Password',
EmailVerificationCode = 'EmailVerificationCode',
PhoneVerificationCode = 'PhoneVerificationCode',
Social = 'Social',
EnterpriseSso = 'EnterpriseSso',
TOTP = 'Totp',
WebAuthn = 'WebAuthn',
BackupCode = 'BackupCode',
NewPasswordIdentity = 'NewPasswordIdentity',
}

// REMARK: API payload guard

/** Payload type for `POST /api/experience/verification/{social|sso}/:connectorId/authorization-uri`. */
Expand Down
4 changes: 2 additions & 2 deletions packages/schemas/src/types/log/interaction.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { type MfaFactor } from '../../foundations/index.js';
import type { InteractionEvent, VerificationType } from '../interactions.js';
import { type VerificationType, type MfaFactor } from '../../foundations/index.js';
import type { InteractionEvent } from '../interactions.js';

export type Prefix = 'Interaction';

Expand Down
17 changes: 17 additions & 0 deletions packages/schemas/tables/verification_records.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
create table verification_records (
tenant_id varchar(21) not null
references tenants (id) on update cascade on delete cascade,
id varchar(21) not null,
user_id varchar(21)
references users (id) on update cascade on delete cascade,
created_at timestamptz not null default(now()),
type varchar(255) /* @use VerificationType */ not null,
verified boolean not null default false,
expires_at timestamptz not null,
/** Use JsonObject here to avoid complex typing, the type will be validated in verification classes. */
data jsonb /* @use JsonObject */ not null default '{}'::jsonb,
primary key (id)
);

create index verification_records__id
on verification_records (tenant_id, id);

0 comments on commit f1ea3d5

Please sign in to comment.