-
-
Notifications
You must be signed in to change notification settings - Fork 423
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(schemas): add verification record table
- Loading branch information
Showing
6 changed files
with
104 additions
and
15 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
packages/schemas/alterations/next-1725971571-add-verification-record.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
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, | ||
consumed_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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
packages/schemas/src/foundations/jsonb-types/verification-records.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { socialUserInfoGuard } from '@logto/connector-kit'; | ||
import { z } from 'zod'; | ||
|
||
export const defaultVerificationRecordDataGuard = z.object({}); | ||
|
||
export const emailVerificationRecordDataGuard = z.object({ | ||
email: z.string(), | ||
}); | ||
|
||
export type EmailVerificationRecordData = z.infer<typeof emailVerificationRecordDataGuard>; | ||
|
||
export const phoneVerificationRecordDataGuard = z.object({ | ||
phone: z.string(), | ||
}); | ||
|
||
export type PhoneVerificationRecordData = z.infer<typeof phoneVerificationRecordDataGuard>; | ||
|
||
export const socialVerificationRecordDataGuard = z.object({ | ||
connectorId: z.string(), | ||
socialUserInfo: socialUserInfoGuard.optional(), | ||
}); | ||
|
||
export type SocialVerificationRecordData = z.infer<typeof socialVerificationRecordDataGuard>; | ||
|
||
export const verificationRecordDataGuard = z.union([ | ||
defaultVerificationRecordDataGuard, | ||
emailVerificationRecordDataGuard, | ||
phoneVerificationRecordDataGuard, | ||
socialVerificationRecordDataGuard, | ||
]); | ||
|
||
export type VerificationRecordData = z.infer<typeof verificationRecordDataGuard>; | ||
|
||
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_at timestamptz, | ||
consumed_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); |