Skip to content

Commit

Permalink
fix(user): fixed registration bug email confirmation restriction
Browse files Browse the repository at this point in the history
  • Loading branch information
Giacomo Materozzi committed Apr 9, 2024
1 parent 1cedfad commit 019a165
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
34 changes: 29 additions & 5 deletions app/api/users/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,55 @@ import { z } from "zod"

import { env } from "@/env.mjs"
import { db } from "@/lib/db"
import i18n from "@/lib/i18n"
import sendEmail from "@/lib/mail"
import emailVerification from "@/lib/mail/templates/emailVerification"
import { ErrorResponse } from "@/lib/response"
import { generateEmailVerificationToken, hashPassword } from "@/lib/utils"

import { findUserByEmail } from "./utils"

const userCreateSchema = z.object({
email: z.string(),
password: z.string(),
})

// @ts-ignore
BigInt.prototype.toJSON = function () {
const int = Number.parseInt(this.toString())
return int ?? this.toString()
}

export async function POST(req: Request) {
try {
const json = await req.json()
const body = userCreateSchema.parse(json)

const userAlreadyExists = await findUserByEmail(body.email)

if (userAlreadyExists?.emailVerified) {
return ErrorResponse({
error: i18n.t("Email already exists"),
})
}

const token = generateEmailVerificationToken()

const user = await db.user.create({
data: {
const user = await db.user.upsert({
where: {
email: body.email,
},
create: {
email: body.email,
password: hashPassword(body.password),
emailVerificationToken: token,
tokens: freePlanTokens,
tokens: BigInt(freePlanTokens),
},
select: {
id: true,
update: {
email: body.email,
password: hashPassword(body.password),
emailVerificationToken: token,
tokens: BigInt(freePlanTokens),
},
})

Expand Down
3 changes: 3 additions & 0 deletions components/app/login/user-auth-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export function UserAuthForm({ className, type, ...props }: UserAuthFormProps) {
register,
handleSubmit,
formState: { errors },
reset,
} = useForm<FormData>({
resolver: zodResolver(userAuthSchema),
})
Expand Down Expand Up @@ -78,6 +79,8 @@ export function UserAuthForm({ className, type, ...props }: UserAuthFormProps) {
})
}

reset()

return toast({
title: i18n.t("Check your email"),
description: i18n.t(
Expand Down
1 change: 1 addition & 0 deletions lib/i18n/languages/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"Edit term": "Edit term",
"Edit word: {keyword}": "Edit word: {{keyword}}",
"Email": "Email",
"Email already exists": "Email already exists",
"Email verified successfully": "Email verified successfully",
"Email verified successfully. Please relogin": "Email verified successfully. Please relogin",
"Enter the old and the new password": "Enter the old and the new password",
Expand Down

0 comments on commit 019a165

Please sign in to comment.