aboutsummaryrefslogtreecommitdiff
path: root/src/app/api/user/profile/route.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/app/api/user/profile/route.ts')
-rw-r--r--src/app/api/user/profile/route.ts78
1 files changed, 78 insertions, 0 deletions
diff --git a/src/app/api/user/profile/route.ts b/src/app/api/user/profile/route.ts
new file mode 100644
index 0000000..0cac7a3
--- /dev/null
+++ b/src/app/api/user/profile/route.ts
@@ -0,0 +1,78 @@
+import { NextRequest, NextResponse } from "next/server"
+import { getServerSession } from "next-auth/next"
+import dbConnect from "@/lib/mongodb"
+import User from "@/model/User"
+import { authOptions } from "@/lib/auth"
+import { updateProfileSchema } from "@/lib/validation"
+
+export async function PATCH(request: NextRequest) {
+ try {
+ const session = await getServerSession(authOptions)
+
+ if (!session?.user?.id) {
+ return NextResponse.json(
+ { error: "Unauthorized" },
+ { status: 401 }
+ )
+ }
+
+ const body = await request.json()
+
+ const result = updateProfileSchema.safeParse(body)
+
+ if (!result.success) {
+ return NextResponse.json(
+ { error: "Validation failed", details: result.error.errors },
+ { status: 400 }
+ )
+ }
+
+ const { name, email } = result.data
+
+ await dbConnect()
+
+ // Check if email is already taken by another user
+ const existingUser = await User.findOne({
+ email,
+ _id: { $ne: session.user.id }
+ })
+
+ if (existingUser) {
+ return NextResponse.json(
+ { error: "Email is already in use" },
+ { status: 409 }
+ )
+ }
+
+ // Update user
+ const updatedUser = await User.findByIdAndUpdate(
+ session.user.id,
+ { name, email },
+ { new: true }
+ )
+
+ if (!updatedUser) {
+ return NextResponse.json(
+ { error: "User not found" },
+ { status: 404 }
+ )
+ }
+
+ return NextResponse.json({
+ message: "Profile updated successfully",
+ user: {
+ id: updatedUser._id,
+ name: updatedUser.name,
+ email: updatedUser.email,
+ }
+ })
+
+ } catch (error) {
+ console.error("Profile update error:", error)
+
+ return NextResponse.json(
+ { error: "Internal server error" },
+ { status: 500 }
+ )
+ }
+} \ No newline at end of file