-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b5444d8
commit 46a93a7
Showing
2 changed files
with
119 additions
and
43 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,65 @@ | ||
import { NextRequest } from "next/server"; | ||
import prisma from "@/db"; | ||
import { hash } from "bcrypt"; | ||
import * as z from "zod"; | ||
import { contract } from "@/lib/constant"; | ||
|
||
interface BodyType { | ||
Name: string; | ||
username: string; | ||
password: string; | ||
} | ||
|
||
const UserSchema = z.object({ | ||
Name: z.string().min(1, "Required"), | ||
username: z | ||
.string() | ||
.min(1, { | ||
message: "email is Required", | ||
}) | ||
.email("Invalid email"), | ||
password: z | ||
.string() | ||
.min(8, { message: "password must contain atleat 8 character" }), | ||
}); | ||
|
||
export async function POST(req: NextRequest) { | ||
try { | ||
const body = await req.json(); | ||
const { Name, username, password }: BodyType = UserSchema.parse(body); | ||
|
||
const existingUser = await prisma.user.findUnique({ | ||
where: { | ||
username: username, | ||
}, | ||
}); | ||
|
||
if (existingUser) { | ||
return Response.json({ error: "User already exists" }, { status: 400 }); | ||
} | ||
|
||
const hashedPassword = await hash(password, 10); | ||
|
||
const newUser = await prisma.user.create({ | ||
data: { | ||
Name, | ||
username, | ||
password: hashedPassword, | ||
}, | ||
}); | ||
|
||
const createUser = await contract.createUser(Name, username); | ||
await createUser.wait(); | ||
return Response.json({ | ||
user: newUser, | ||
message: "Signed up Successfully", | ||
status: 201, | ||
}); | ||
} catch (error) { | ||
console.error("Error occurred while creating user:", error); | ||
return Response.json( | ||
{ error: "An error occurred while creating user" }, | ||
{ status: 500 }, | ||
); | ||
} | ||
} |