-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update auth/recover and auth/verify-email services
Add Dependencies interface to facilitate testing
- Loading branch information
Showing
2 changed files
with
42 additions
and
11 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,38 +1,50 @@ | ||
import type { User } from "app:entities/user"; | ||
import { AuditLogsRepository } from "app:repositories.server/audit-logs"; | ||
import { UsersRepository } from "app:repositories.server/users"; | ||
import { type Entity, waitUntil } from "@edgefirst-dev/core"; | ||
import type { Email } from "@edgefirst-dev/email"; | ||
|
||
/** | ||
* Verifies a user's email address by setting the `emailVerifiedAt` field. | ||
* | ||
* @param input - The registration input data containing the email and password. | ||
* @param repos - The dependency injection object containing repositories. | ||
* @param deps - The dependency injection object containing repositories. | ||
* @throws {Error} If the user is not found. | ||
* @returns A promise that resolves when the email is verified, or immediately if the user is already verified. | ||
*/ | ||
export async function verifyEmail( | ||
input: verifyEmail.Input, | ||
repos = { | ||
deps: verifyEmail.Dependencies = { | ||
audits: new AuditLogsRepository(), | ||
user: new UsersRepository(), | ||
}, | ||
) { | ||
let [user] = await repos.user.findByEmail(input.email); | ||
let [user] = await deps.user.findByEmail(input.email); | ||
if (!user) throw new Error("User not found"); | ||
|
||
// If the user is already verified, we don't need to do anything | ||
if (user.hasEmailVerified) return; | ||
|
||
// Here we should validate that data.code is valid | ||
|
||
await repos.user.verifyEmail(user); | ||
await deps.user.verifyEmail(user); | ||
|
||
await repos.audits.create(user, "verified_email"); | ||
waitUntil(deps.audits.create(user, "verified_email")); | ||
} | ||
|
||
export namespace verifyEmail { | ||
export interface Input { | ||
readonly email: Email; | ||
readonly code: string; | ||
} | ||
|
||
export interface Dependencies { | ||
audits: { | ||
create(user: User, action: string, entity?: Entity): Promise<void>; | ||
}; | ||
user: { | ||
findByEmail(email: Email): Promise<User[]>; | ||
verifyEmail(user: User): Promise<void>; | ||
}; | ||
} | ||
} |