Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .env.template
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ SENDGRID_SENDER_ID=""

NEXTAUTH_URL="http://localhost:3000"
NEXTAUTH_SECRET="change-me"
NEXTAUTH_ROOT_DOMAIN="http://localhost"
NEXTAUTH_ROOT_DOMAIN="http://localhost"

SECRET_LINK="http://localhost:3000/verify-email/"
110 changes: 103 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@
"dependencies": {
"@prisma/client": "^5.9.1",
"@sendgrid/mail": "^8.1.0",
"@types/jsonwebtoken": "^9.0.6",
"bcrypt": "^5.1.1",
"eclipse-components": "^0.0.141",
"eclipse-components": "^0.0.149",
"jsonwebtoken": "^9.0.2",
"next": "14.1.0",
"next-auth": "^4.24.5",
"prisma": "^5.9.1",
Expand All @@ -31,4 +33,4 @@
"tailwindcss": "^3.3.0",
"typescript": "^5"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "Auth" ADD COLUMN "emailVerified" BOOLEAN NOT NULL DEFAULT false;
2 changes: 2 additions & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ model Auth {

createdAt DateTime @default(now()) @db.Timestamp(0)
updatedAt DateTime @default(now()) @updatedAt @db.Timestamp(0)

emailVerified Boolean @default(false)
}

model MailingList {
Expand Down
4 changes: 4 additions & 0 deletions src/app/api/auth/signup/route.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { prisma } from "@/lib/prisma";
import bcrypt from "bcrypt";
import { NextResponse } from "next/server";
import {tokenGeneration} from "@/app/api/auth/signup/verifyEmail";
import internal from "stream";

/**
*
Expand Down Expand Up @@ -60,6 +62,7 @@ export async function POST(request: Request) {

console.log(email, " Account Created");

await tokenGeneration(newUser.id, email);
return NextResponse.json({ user: newUser }, { status: 201 });
}

Expand All @@ -80,5 +83,6 @@ export async function POST(request: Request) {

console.log(email, " Account Created");

await tokenGeneration(newUser.id, email);
return NextResponse.json({ user: newUser }, { status: 201 });
}
15 changes: 15 additions & 0 deletions src/app/api/auth/signup/verifyEmail.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import jwt from "jsonwebtoken";
import { sendEmail } from "@/lib/email";
import { send } from "process";

export async function tokenGeneration(userId:string, email:string) {
const secret = process.env.NEXTAUTH_SECRET as string;

const token = jwt.sign({userId, email}, secret, {expiresIn: "7d"});
const link = process.env.SECRET_LINK + token;

const subject = "Verify your email";
const body = `Click this link to verify your email: ${link}`;

await sendEmail(email, subject, body);
}
4 changes: 2 additions & 2 deletions src/app/login/LoginForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import {
TextField,
Button,
EclipseLogoLong,
EclipseLogoTextOrbGlow,
Notification,
} from "eclipse-components";
import { signIn } from "next-auth/react";
Expand All @@ -22,7 +22,7 @@ export default function LoginForm() {

return (
<>
<EclipseLogoLong className="h-32 md:h-40" />
<EclipseLogoTextOrbGlow className="h-32 md:h-40" />
<form
onSubmit={async (e) => {
e.preventDefault();
Expand Down
4 changes: 2 additions & 2 deletions src/app/signup/SignupForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import {
TextField,
Button,
EclipseLogoLong,
EclipseLogoTextOrbGlow,
Notification,
} from "eclipse-components";
import { signIn } from "next-auth/react";
Expand All @@ -18,7 +18,7 @@ export default function SignupForm() {
const searchParams = useSearchParams();
return (
<>
<EclipseLogoLong className="h-32 md:h-40" />
<EclipseLogoTextOrbGlow className="h-32 md:h-40" />
<form
onSubmit={async (e) => {
e.preventDefault();
Expand Down
69 changes: 69 additions & 0 deletions src/app/verify-email/[jwt]/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
"use client"

import { useEffect, useState } from "react";
import {
LoadingSpinner,
EclipseLogoTextOrbGlow,
StarBackground,
MainWrapper
} from "eclipse-components";
import {verifyUserToken} from "./verifyUserToken"


export default function Home({ params }: { params: { jwt: string } }) {
return (
<Components token={params.jwt as string} />
);
}

enum Status {
IDLE,
LOADING,
SUCCESS,
ERROR,
}

interface Props {
token: string;
}

function Components(props: Props) {

const [status, setStatus] = useState<Status>(Status.LOADING);
const [error, setError] = useState<string>("");

const userToken = props.token;

async function fetchVerification(userToken: string){
const verification = await verifyUserToken(userToken);
if (verification.success) {
setStatus(Status.SUCCESS);
}
else {
setStatus(Status.ERROR);
setError(verification.error as string);
}
}

useEffect(() => {
fetchVerification(userToken);
}, [userToken]);


return (
<>
<StarBackground />
<MainWrapper>
<EclipseLogoTextOrbGlow className="h-16 md:h-40" />


{status === Status.LOADING && <LoadingSpinner /> }
{status === Status.SUCCESS && <p>Your email has been verified</p>}
{status === Status.ERROR &&
<p>{error}</p>
}

</MainWrapper>
</>
);
}
Loading