Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

Use separate tables for auth, email verification, and password reset tokens

Tim Wilson20df9f6d4761
diff --git a/apps/web/app/api/auth/reset-password/route.ts b/apps/web/app/api/auth/reset-password/route.ts
index eac2fa1f63..0426849169 100644
--- a/apps/web/app/api/auth/reset-password/route.ts
+++ b/apps/web/app/api/auth/reset-password/route.ts
@@ -17,7 +17,7 @@ export async function POST(req: NextRequest) {
);
// Find the token
- const tokenFound = await prisma.verificationToken.findFirst({
+ const tokenFound = await prisma.passwordResetToken.findFirst({
where: {
token,
expires: {
@@ -41,7 +41,7 @@ export async function POST(req: NextRequest) {
await prisma.$transaction([
// Delete the token
- prisma.verificationToken.deleteMany({
+ prisma.passwordResetToken.deleteMany({
where: {
token,
},
diff --git a/apps/web/app/api/user/set-password/route.ts b/apps/web/app/api/user/set-password/route.ts
index a3d5744056..c3173acc10 100644
--- a/apps/web/app/api/user/set-password/route.ts
+++ b/apps/web/app/api/user/set-password/route.ts
@@ -28,7 +28,7 @@ export const POST = withSession(async ({ session }) => {
});
}
- const { token } = await prisma.verificationToken.create({
+ const { token } = await prisma.passwordResetToken.create({
data: {
identifier: session.user.email,
token: randomBytes(32).toString("hex"),
diff --git a/apps/web/app/app.dub.co/(auth)/auth/reset-password/[token]/page.tsx b/apps/web/app/app.dub.co/(auth)/auth/reset-password/[token]/page.tsx
index 32414b446f..f11bc40f3d 100644
--- a/apps/web/app/app.dub.co/(auth)/auth/reset-password/[token]/page.tsx
+++ b/apps/web/app/app.dub.co/(auth)/auth/reset-password/[token]/page.tsx
@@ -42,7 +42,7 @@ export default async function ResetPasswordPage({ params: { token } }: Props) {
}
const isValidToken = async (token: string) => {
- const resetToken = await prisma.verificationToken.findUnique({
+ const resetToken = await prisma.passwordResetToken.findUnique({
where: {
token,
expires: {
diff --git a/apps/web/lib/actions/create-user-account.ts b/apps/web/lib/actions/create-user-account.ts
index c9ea8fc2b7..85053a5963 100644
--- a/apps/web/lib/actions/create-user-account.ts
+++ b/apps/web/lib/actions/create-user-account.ts
@@ -61,7 +61,7 @@ export const createUserAccountAction = actionClient
// Generate the OTP
const code = generateOTP();
- await prisma.verificationToken.create({
+ await prisma.emailVerificationToken.create({
data: {
identifier: email,
token: code,
diff --git a/apps/web/lib/actions/request-password-reset.ts b/apps/web/lib/actions/request-password-reset.ts
index b697ae775c..f05eda4455 100644
--- a/apps/web/lib/actions/request-password-reset.ts
+++ b/apps/web/lib/actions/request-password-reset.ts
@@ -40,7 +40,7 @@ export const requestPasswordResetAction = actionClient
throw new Error("An user with this email does not exist.");
}
- const { token } = await prisma.verificationToken.create({
+ const { token } = await prisma.passwordResetToken.create({
data: {
identifier: email,
token: randomBytes(32).toString("hex"),
diff --git a/apps/web/lib/actions/resend-otp.ts b/apps/web/lib/actions/resend-otp.ts
index 1b12f203e7..2a51b1fbe1 100644
--- a/apps/web/lib/actions/resend-otp.ts
+++ b/apps/web/lib/actions/resend-otp.ts
@@ -44,7 +44,7 @@ export const resendOtpAction = actionClient
// Generate the OTP
const code = generateOTP();
- await prisma.verificationToken.create({
+ await prisma.emailVerificationToken.create({
data: {
identifier: email,
token: code,
diff --git a/apps/web/lib/actions/verify-email.ts b/apps/web/lib/actions/verify-email.ts
index f34c7e12cc..224788dd0d 100644
--- a/apps/web/lib/actions/verify-email.ts
+++ b/apps/web/lib/actions/verify-email.ts
@@ -31,7 +31,7 @@ export const verifyEmailAction = actionClient
);
}
- const verificationToken = await prisma.verificationToken.findUnique({
+ const verificationToken = await prisma.emailVerificationToken.findUnique({
where: {
identifier: email,
token: code,
@@ -46,7 +46,7 @@ export const verifyEmailAction = actionClient
}
await Promise.all([
- prisma.verificationToken.delete({
+ prisma.emailVerificationToken.delete({
where: {
token: code,
},
diff --git a/apps/web/prisma/schema/token.prisma b/apps/web/prisma/schema/token.prisma
index fd5721eb87..745af51753 100644
--- a/apps/web/prisma/schema/token.prisma
+++ b/apps/web/prisma/schema/token.prisma
@@ -39,6 +39,7 @@ model RestrictedToken {
@@index([installationId])
}
+// Login tokens
model VerificationToken {
identifier String
token String @unique
@@ -46,3 +47,21 @@ model VerificationToken {
@@unique([identifier, token])
}
+
+// Email verification OTPs
+model EmailVerificationToken {
+ identifier String
+ token String @unique
+ expires DateTime
+
+ @@unique([identifier, token])
+}
+
+// Password reset tokens
+model PasswordResetToken {
+ identifier String
+ token String @unique
+ expires DateTime
+
+ @@unique([identifier, token])
+}
\ No newline at end of file