-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: ✨ feat(email-verification): Implement email verification functi…
…onality Description: This commit introduces the email verification functionality. The changes are part of the ongoing work on the `email-verification` branch. - In `README.md`, updates have been made to reflect the current state of the project. - In `components/spinner.tsx`, changes have been made to improve the loading spinner. - The file `data/verificiation-token.ts` has been deleted and replaced with `data/verification-token.ts` to correct a typo. - In `lib/mail.ts` and `lib/tokens.ts`, updates have been made to handle the email verification process. - New files `actions/new-verification.ts`, `app/auth/new-verification/`, and `components/auth/new-verification-form.tsx` have been added to handle the new verification process. Changes: - Update `README.md` to reflect current project state - Improve loading spinner in `components/spinner.tsx` - Delete `data/verificiation-token.ts` and add `data/verification-token.ts` - Update `lib/mail.ts` and `lib/tokens.ts` for email verification - Add new files for handling new verification process This commit doesn't introduce any breaking changes. The updates are compatible with the existing codebase. Co-authored-by: Ricardo Esteves <[email protected]>
- Loading branch information
1 parent
09c9061
commit 0fe5180
Showing
9 changed files
with
108 additions
and
7 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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
"use server"; | ||
|
||
import { db } from "@/lib/db"; | ||
import { getUserByEmail } from "@/data/user"; | ||
import { getVerificationTokenByToken } from "@/data/verification-token"; | ||
|
||
export const newVerification = async (token: string) => { | ||
const existingToken = await getVerificationTokenByToken(token); | ||
|
||
if (!existingToken) { | ||
return { error: "Token does not exist!" }; | ||
} | ||
|
||
const hasExpired = new Date(existingToken.expires) < new Date(); | ||
|
||
if (hasExpired) { | ||
return { error: "Token has expired!" }; | ||
} | ||
|
||
const existingUser = await getUserByEmail(existingToken.email); | ||
|
||
if (!existingUser) { | ||
return { error: "User not registered!" }; | ||
} | ||
|
||
await db.user.update({ | ||
where: { id: existingUser.id }, | ||
data: { | ||
emailVerified: new Date(), | ||
email: existingToken.email, | ||
}, | ||
}); | ||
|
||
await db.verificationToken.delete({ | ||
where: { id: existingToken.id }, | ||
}); | ||
|
||
return { success: "Account verified!" }; | ||
}; |
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,5 @@ | ||
import NewVerificationForm from "@/components/auth/new-verification-form"; | ||
|
||
export default function NewVerificationPage() { | ||
return <NewVerificationForm />; | ||
} |
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,57 @@ | ||
"use client"; | ||
|
||
import { useCallback, useEffect, useState } from "react"; | ||
import { useSearchParams } from "next/navigation"; | ||
|
||
import { newVerification } from "@/actions/new-verification"; | ||
import CardWrapper from "@/components/auth/card-wrapper"; | ||
import FormError from "@/components/form-error"; | ||
import FormSuccess from "@/components/form-success"; | ||
import Spinner from "../spinner"; | ||
|
||
const NewVerificationForm = () => { | ||
const [error, setError] = useState<string | undefined>(); | ||
const [success, setSuccess] = useState<string | undefined>(); | ||
|
||
const searchParams = useSearchParams(); | ||
|
||
const token = searchParams.get("token"); | ||
|
||
const onSubmit = useCallback(() => { | ||
if (success || error) return; | ||
|
||
if (!token) { | ||
setError("Missing token!"); | ||
return; | ||
} | ||
|
||
newVerification(token) | ||
.then((data) => { | ||
setSuccess(data.success); | ||
setError(data.error); | ||
}) | ||
.catch(() => { | ||
setError("Something went wrong!"); | ||
}); | ||
}, [token, success, error]); | ||
|
||
useEffect(() => { | ||
onSubmit(); | ||
}, [onSubmit]); | ||
|
||
return ( | ||
<CardWrapper | ||
headerLabel="Confirming your verification" | ||
backButtonLabel="Back to login" | ||
backButtonHref="/auth/login" | ||
> | ||
<div className="flex items-center w-full justify-center"> | ||
{!success && !error && <Spinner />} | ||
{!success && <FormError message={error} />} | ||
{success && <FormSuccess message={success} />} | ||
</div> | ||
</CardWrapper> | ||
); | ||
}; | ||
|
||
export default NewVerificationForm; |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ export const sendVerificationEmail = async (email: string, token: string) => { | |
from: "Acme <[email protected]>", | ||
to: email, | ||
subject: "Verify your email.", | ||
// TODO: Add a template for this email | ||
html: `<p>Click <a href="${confirmLink}">here</a> to verify email.</p>`, | ||
}); | ||
}; |
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