-
-
Notifications
You must be signed in to change notification settings - Fork 440
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
72 additions
and
15 deletions.
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
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,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; |
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
15 changes: 15 additions & 0 deletions
15
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,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); |
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 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); |