diff --git a/src/actions/changePassword.ts b/src/actions/changePassword.ts new file mode 100644 index 0000000..9be9124 --- /dev/null +++ b/src/actions/changePassword.ts @@ -0,0 +1,29 @@ +"use server"; + +import { getUser } from "@/auth/auth"; +import { getUserInfo, setPassword, validateUser } from "@/ldap"; +import { ActionResponse } from "."; +import { PasswordUpdateSchema, passwordUpdateSchema } from "@/schemas"; + +export default async function changePassword( + data: PasswordUpdateSchema +): Promise { + const user = await getUser(); + if (user == null) return { ok: false, error: "noUser" }; + + const userInfo = await getUserInfo(user); + if (userInfo == null) { + return { ok: false, error: "notRegisteredYet" }; + } + + const { password, newPassword } = passwordUpdateSchema.parse(data); + + const passwordMatches = await validateUser(user.username!, password); + if (!passwordMatches) { + return { ok: false, error: "incorrectPassword" }; + } + + await setPassword(user.username!, newPassword); + + return { ok: true }; +} diff --git a/src/app/me/AboutMe.tsx b/src/app/me/AboutMe.tsx index e2ca182..88f67af 100644 --- a/src/app/me/AboutMe.tsx +++ b/src/app/me/AboutMe.tsx @@ -18,6 +18,7 @@ import MigaduIcon from "@/components/icons/MigaduIcon"; import { AboutMeSchema, aboutMeSchema } from "@/schemas"; import update from "@/actions/update"; import { toFormikValidationSchema } from "zod-formik-adapter"; +import { useRouter } from "next/navigation"; export default function AboutMe({ info, @@ -30,6 +31,7 @@ export default function AboutMe({ const [globalError, setGlobalError] = React.useState(null); const [madeProfileChanges, setMadeChanges] = React.useState(false); const [madePasswordChanges, setMadePasswordChanges] = React.useState(false); + const router = useRouter(); const initialValues: AboutMeSchema = { username: info.username, @@ -120,7 +122,14 @@ export default function AboutMe({ />
- + + + )} + + + + ); +} diff --git a/src/app/me/change-password/page.tsx b/src/app/me/change-password/page.tsx new file mode 100644 index 0000000..6f666e9 --- /dev/null +++ b/src/app/me/change-password/page.tsx @@ -0,0 +1,11 @@ +import ChangePasswordForm from "./ChangePasswordForm"; + +export default function ChangePassword() { + return ( + // fuck it im lazy +
+

Change Password

+ +
+ ); +}