This repository has been archived by the owner on Sep 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Link the
auth_email
module to the auth_providers
module
- Loading branch information
1 parent
a603a06
commit 1a763f4
Showing
21 changed files
with
333 additions
and
295 deletions.
There are no files selected for viewing
48 changes: 0 additions & 48 deletions
48
modules/auth/db/migrations/20240310214734_init/migration.sql
This file was deleted.
Oops, something went wrong.
12 changes: 0 additions & 12 deletions
12
modules/auth/db/migrations/20240312024843_init/migration.sql
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,8 +1,4 @@ | ||
export interface Config { | ||
email?: EmailConfig; | ||
} | ||
|
||
export interface EmailConfig { | ||
fromEmail: string; | ||
fromName?: string; | ||
} |
16 changes: 16 additions & 0 deletions
16
modules/auth_email/db/migrations/20240701232228_init/migration.sql
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,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"); |
File renamed without changes.
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,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" | ||
} | ||
} | ||
} |
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 |
---|---|---|
|
@@ -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, | ||
|
@@ -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", | ||
|
Oops, something went wrong.