diff --git a/app/api/user/change/profile/route.ts b/app/api/user/change/profile/route.ts index 8bfef59..cfdcf0a 100644 --- a/app/api/user/change/profile/route.ts +++ b/app/api/user/change/profile/route.ts @@ -33,6 +33,17 @@ export async function POST(request: NextRequest) { return NextResponse.json({ error: "User not found" }, { status: 404 }); } + await prisma.user.update({ + where: { + id: decoded.id, + }, + data: { + username: body.username, + name: body.name, + email: body.email, + }, + }); + return NextResponse.json({ message: "Profile updated successfully" }, { status: 200 }); } catch (error: any) { return NextResponse.json({ error: "Internal Server Error" }, { status: 500 }); diff --git a/app/dashboard/settings/SettingsPage.tsx b/app/dashboard/settings/SettingsPage.tsx index 3f6f862..b284f1d 100644 --- a/app/dashboard/settings/SettingsPage.tsx +++ b/app/dashboard/settings/SettingsPage.tsx @@ -1,34 +1,56 @@ "use client"; import Sidebar from "@/components/Sidebar"; +import ErrorToast from "@/components/Error"; import { useState } from "react"; import axios from "axios"; import Cookies from "js-cookie"; -interface DashboardPageProps { +interface SettingsPageProps { username: string; name: string; email: string; } -export default function DashboardPage({ username, name, email }: DashboardPageProps) { +export default function SettingsPage({ username, name, email }: SettingsPageProps) { const [profileUsername, setProfileUsername] = useState(username); const [profileName, setProfileName] = useState(name); const [profileEmail, setProfileEmail] = useState(email); + const [oldPassword, setOldPassword] = useState(""); const [password, setPassword] = useState(""); const [passwordConfirm, setPasswordConfirm] = useState(""); + const [error, setError] = useState(""); + const saveProfile = async () => { - const response = await axios.post("/api/user/change/profile", { token: Cookies.get("token"), username: profileUsername, name: profileName, email: profileEmail }); - if (response.data.message !== "Profile updated successfully") { - alert("Failed to update profile"); + try { + const response = await axios.post("/api/user/change/profile", { token: Cookies.get("token"), username: profileUsername, name: profileName, email: profileEmail }); + if (response.data.message !== "Profile updated successfully") { + setError("Failed to update profile"); + } else { + window.location.reload(); + } + } catch (error: any) { + setError(error.response.data.error); } } const savePassword = async () => { - const response = await axios.post("/api/user/change/password", { token: Cookies.get("token"), old_password: password, password: passwordConfirm }); - if (response.data.message !== "Password updated successfully") { - alert("Failed to update password"); + if (password !== passwordConfirm) { + setError("Passwords do not match"); + return; + } + if (oldPassword === password) { + setError("Old password and new password cannot be the same"); + return; + } + try { + const response = await axios.post("/api/user/change/password", { token: Cookies.get("token"), old_password: oldPassword, password: password }); + if (response.data.message !== "Password updated successfully") { + setError("Failed to update password"); + } + } catch (error: any) { + setError(error.response.data.error); } } @@ -71,6 +93,10 @@ export default function DashboardPage({ username, name, email }: DashboardPagePr

Password Settings

Manage your password

+
+ + setOldPassword(e.target.value)} className="input w-full" /> +
setPassword(e.target.value)} className="input w-full" /> @@ -88,6 +114,7 @@ export default function DashboardPage({ username, name, email }: DashboardPagePr + setError("")} />
); } \ No newline at end of file