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

Commit

Permalink
feat: Link the auth_email module to the auth_providers module
Browse files Browse the repository at this point in the history
  • Loading branch information
Blckbrry-Pi committed Jul 3, 2024
1 parent fb94688 commit bad4814
Show file tree
Hide file tree
Showing 22 changed files with 334 additions and 409 deletions.
48 changes: 0 additions & 48 deletions modules/auth/db/migrations/20240310214734_init/migration.sql

This file was deleted.

12 changes: 0 additions & 12 deletions modules/auth/db/migrations/20240312024843_init/migration.sql

This file was deleted.

2 changes: 0 additions & 2 deletions modules/auth/db/migrations/20240312033322_/migration.sql

This file was deleted.

21 changes: 0 additions & 21 deletions modules/auth/db/migrations/20240312035811_/migration.sql

This file was deleted.

52 changes: 0 additions & 52 deletions modules/auth/module.json

This file was deleted.

75 changes: 0 additions & 75 deletions modules/auth/tests/e2e.ts

This file was deleted.

4 changes: 0 additions & 4 deletions modules/auth/config.ts → modules/auth_email/config.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
export interface Config {
email?: EmailConfig;
}

export interface EmailConfig {
fromEmail: string;
fromName?: string;
}
16 changes: 16 additions & 0 deletions modules/auth_email/db/migrations/20240701232228_init/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
-- CreateTable
CREATE TABLE "Verifications" (
"id" UUID NOT NULL,
"email" TEXT NOT NULL,
"code" TEXT NOT NULL,
"attemptCount" INTEGER NOT NULL DEFAULT 0,
"maxAttemptCount" INTEGER NOT NULL,
"createdAt" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
"expireAt" TIMESTAMP NOT NULL,
"completedAt" TIMESTAMP,

CONSTRAINT "Verifications_pkey" PRIMARY KEY ("id")
);

-- CreateIndex
CREATE UNIQUE INDEX "Verifications_code_key" ON "Verifications"("code");
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,9 @@ datasource db {
url = env("DATABASE_URL")
}

model EmailPasswordless {
id String @id @default(uuid()) @db.Uuid
userId String @db.Uuid @unique
email String @unique
createdAt DateTime @default(now()) @db.Timestamp
}

model EmailPasswordlessVerification {
model Verifications {
id String @id @default(uuid()) @db.Uuid
// If exists, link to existing identity. If null, create new identity.
userId String? @db.Uuid
email String
// Code the user has to input to verify the email
Expand Down
62 changes: 62 additions & 0 deletions modules/auth_email/module.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{
"name": "Authentication",
"description": "Authenticate users with multiple authentication methods.",
"icon": "key",
"tags": [
"core",
"auth",
"user"
],
"authors": [
"rivet-gg",
"NathanFlurry"
],
"status": "stable",
"dependencies": {
"email": {},
"auth_providers": {},
"users": {},
"rate_limit": {}
},
"scripts": {
"send_email": {
"name": "Send Email Verification",
"description": "Send a one-time verification code to an email address to verify ownership.",
"public": true
},
"verify_and_add": {
"name": "Verify and add Email to Existing User",
"description": "Verify a user's email address and register it with an existing account.",
"public": true
},
"verify_and_login_or_create": {
"name": "Verify and Login as (or Create) User",
"description": "Verify the email address code and return a userToken to AN account (creates a new account if one doesn't exist)",
"public": true
},
"verify_code": {
"name": "Verify Code",
"description": "Verify the email address code"
}
},
"errors": {
"provider_disabled": {
"name": "Provider Disabled"
},
"verification_code_invalid": {
"name": "Verification Code Invalid"
},
"verification_code_attempt_limit": {
"name": "Verification Code Attempt Limit"
},
"verification_code_expired": {
"name": "Verification Code Expired"
},
"verification_code_already_used": {
"name": "Verification Code Already Used"
},
"email_already_used": {
"name": "Email Already Used"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,35 +17,12 @@ export async function run(
): Promise<Response> {
await ctx.modules.rateLimit.throttlePublic({});

if (!ctx.config.email) throw new RuntimeError("provider_disabled");

// Check if the email is already associated with an identity
const existingIdentity = await ctx.db.emailPasswordless.findFirst({
where: { email: req.email },
});

// Fetch existing user if session token is provided
let userId: string | undefined = existingIdentity?.userId;

if (req.userToken) {
const authRes = await ctx.modules.users.authenticateToken({
userToken: req.userToken,
});

if (existingIdentity && existingIdentity.userId !== authRes.userId) {
throw new RuntimeError("email_already_used");
}

userId = authRes.userId;
}

// Create verification
const code = generateCode();
const maxAttemptCount = 3;
const expiration = 60 * 60 * 1000;
const verification = await ctx.db.emailPasswordlessVerification.create({
const verification = await ctx.db.verifications.create({
data: {
userId,
email: req.email,
code,
maxAttemptCount,
Expand All @@ -54,11 +31,13 @@ export async function run(
select: { id: true },
});


console.log(ctx.config);
// Send email
await ctx.modules.email.sendEmail({
from: {
email: ctx.config.email.fromEmail ?? "[email protected]",
name: ctx.config.email.fromName ?? "Authentication Code",
email: ctx.config.fromEmail ?? "[email protected]",
name: ctx.config.fromName ?? "Authentication Code",
},
to: [{ email: req.email }],
subject: "Your verification code",
Expand Down
Loading

0 comments on commit bad4814

Please sign in to comment.