Skip to content
This repository was archived by the owner on Sep 17, 2024. It is now read-only.

Commit 81cf4a9

Browse files
committed
feat: Stub the new modular auth module and set up flow management.
1 parent d2012c6 commit 81cf4a9

File tree

19 files changed

+342
-424
lines changed

19 files changed

+342
-424
lines changed

modules/auth/config.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
export interface Config {
2-
email?: EmailConfig;
3-
}
1+
import { Provider } from "./utils/types.ts";
42

5-
export interface EmailConfig {
6-
fromEmail: string;
7-
fromName?: string;
3+
export interface Config {
4+
providers: Provider[];
85
}

modules/auth/db/migrations/20240310214734_init/migration.sql

Lines changed: 0 additions & 48 deletions
This file was deleted.

modules/auth/db/migrations/20240312024843_init/migration.sql

Lines changed: 0 additions & 12 deletions
This file was deleted.

modules/auth/db/migrations/20240312033322_/migration.sql

Lines changed: 0 additions & 2 deletions
This file was deleted.

modules/auth/db/migrations/20240312035811_/migration.sql

Lines changed: 0 additions & 21 deletions
This file was deleted.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
-- CreateTable
2+
CREATE TABLE "IdentityEmail" (
3+
"userId" UUID NOT NULL,
4+
"email" TEXT NOT NULL,
5+
6+
CONSTRAINT "IdentityEmail_pkey" PRIMARY KEY ("email")
7+
);
8+
9+
-- CreateTable
10+
CREATE TABLE "IdentityOAuth" (
11+
"userId" UUID NOT NULL,
12+
"provider" TEXT NOT NULL,
13+
"subId" TEXT NOT NULL,
14+
15+
CONSTRAINT "IdentityOAuth_pkey" PRIMARY KEY ("provider","subId")
16+
);
17+
18+
-- CreateIndex
19+
CREATE INDEX "IdentityEmail_userId_idx" ON "IdentityEmail"("userId");
20+
21+
-- CreateIndex
22+
CREATE INDEX "IdentityOAuth_userId_idx" ON "IdentityOAuth"("userId");

modules/auth/db/schema.prisma

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,16 @@ datasource db {
33
url = env("DATABASE_URL")
44
}
55

6-
model EmailPasswordless {
7-
id String @id @default(uuid()) @db.Uuid
8-
userId String @db.Uuid @unique
9-
email String @unique
10-
createdAt DateTime @default(now()) @db.Timestamp
6+
model IdentityEmail {
7+
userId String @db.Uuid
8+
email String @id
9+
@@index([userId])
1110
}
1211

13-
model EmailPasswordlessVerification {
14-
id String @id @default(uuid()) @db.Uuid
15-
16-
// If exists, link to existing identity. If null, create new identity.
17-
userId String? @db.Uuid
18-
19-
email String
20-
21-
// Code the user has to input to verify the email
22-
code String @unique
23-
24-
attemptCount Int @default(0)
25-
maxAttemptCount Int
26-
27-
createdAt DateTime @default(now()) @db.Timestamp
28-
expireAt DateTime @db.Timestamp
29-
completedAt DateTime? @db.Timestamp
12+
model IdentityOAuth {
13+
userId String @db.Uuid
14+
provider String
15+
subId String
16+
@@id([provider, subId])
17+
@@index([userId])
3018
}

modules/auth/module.json

Lines changed: 69 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,71 @@
11
{
2-
"name": "Authentication",
3-
"description": "Authenticate users with multiple authentication methods.",
4-
"icon": "key",
5-
"tags": [
6-
"core",
7-
"auth",
8-
"user"
9-
],
10-
"authors": [
11-
"rivet-gg",
12-
"NathanFlurry"
13-
],
14-
"status": "stable",
15-
"dependencies": {
16-
"email": {},
17-
"users": {},
18-
"rate_limit": {}
19-
},
20-
"scripts": {
21-
"send_email_verification": {
22-
"name": "Send Email Verification",
23-
"description": "Send a one-time verification code to a user's email address to authenticate them.",
24-
"public": true
25-
},
26-
"complete_email_verification": {
27-
"name": "Complete Email Verification",
28-
"description": "Verify a user's email address with a one-time verification code.",
29-
"public": true
30-
}
31-
},
32-
"errors": {
33-
"provider_disabled": {
34-
"name": "Provider Disabled"
35-
},
36-
"verification_code_invalid": {
37-
"name": "Verification Code Invalid"
38-
},
39-
"verification_code_attempt_limit": {
40-
"name": "Verification Code Attempt Limit"
41-
},
42-
"verification_code_expired": {
43-
"name": "Verification Code Expired"
44-
},
45-
"verification_code_already_used": {
46-
"name": "Verification Code Already Used"
47-
},
48-
"email_already_used": {
49-
"name": "Email Already Used"
50-
}
51-
}
2+
"name": "Authentication",
3+
"description": "Authenticate users with multiple authentication methods.",
4+
"icon": "key",
5+
"tags": [
6+
"core",
7+
"auth",
8+
"user"
9+
],
10+
"authors": [
11+
"rivet-gg",
12+
"NathanFlurry"
13+
],
14+
"status": "stable",
15+
"dependencies": {
16+
"email": {},
17+
"users": {},
18+
"rate_limit": {},
19+
"tokens": {}
20+
},
21+
"scripts": {
22+
"get_flow_status": {
23+
"name": "Get Flow Status",
24+
"description": "Get the status of a login flow by the flow token. Returns the userToken if the flow is completed.",
25+
"public": true
26+
},
27+
"cancel_flow": {
28+
"name": "Cancel Flow",
29+
"description": "Cancels a login flow. This is irreversible and will error if the flow is not `pending`."
30+
},
31+
"complete_flow": {
32+
"name": "Complete Flow",
33+
"description": "Completes a login flow and generates a user token. This is irreversible and will error if the flow is not `pending`."
34+
},
35+
"list_providers": {
36+
"name": "Send Email Verification",
37+
"description": "Send a one-time verification code to a user's email address to authenticate them.",
38+
"public": true
39+
},
40+
"start_login_flow": {
41+
"name": "Send Email Verification",
42+
"description": "Send a one-time verification code to a user's email address to authenticate them.",
43+
"public": true
44+
},
45+
"list_identities": {
46+
"name": "Complete Email Verification",
47+
"description": "Verify a user's email address with a one-time verification code.",
48+
"public": true
49+
}
50+
},
51+
"errors": {
52+
"provider_disabled": {
53+
"name": "Provider Disabled"
54+
},
55+
"verification_code_invalid": {
56+
"name": "Verification Code Invalid"
57+
},
58+
"verification_code_attempt_limit": {
59+
"name": "Verification Code Attempt Limit"
60+
},
61+
"verification_code_expired": {
62+
"name": "Verification Code Expired"
63+
},
64+
"verification_code_already_used": {
65+
"name": "Verification Code Already Used"
66+
},
67+
"email_already_used": {
68+
"name": "Email Already Used"
69+
}
70+
}
5271
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { Empty, ScriptContext } from "../module.gen.ts";
2+
import { cancelFlow } from "../utils/flow.ts";
3+
4+
export interface Request {
5+
flowToken: string;
6+
}
7+
export type Response = Empty;
8+
9+
export async function run(
10+
ctx: ScriptContext,
11+
req: Request,
12+
): Promise<Response> {
13+
await cancelFlow(ctx, req.flowToken);
14+
return {};
15+
}

0 commit comments

Comments
 (0)