import { useForm } from "@tanstack/react-form"; import { useNavigate } from "@tanstack/react-router"; import axios from "axios"; import { useState } from "react"; import { toast } from "sonner"; import { LstCard } from "../extendedUI/LstCard"; import { CardContent, CardHeader } from "../ui/card"; import { Label } from "../ui/label"; import { Input } from "../ui/input"; import { Button } from "../ui/button"; export default function PasswordChange() { const [saving, setSaving] = useState(false); const token = localStorage.getItem("auth_token"); const navigate = useNavigate(); const form = useForm({ defaultValues: { password: "", confirmPassword: "", }, onSubmit: async ({ value }) => { setSaving(true); try { const res = await axios.patch("/api/auth/profile", value, { headers: { Authorization: `Bearer ${token}` }, }); //console.log(res.data); if (res.data.success) { localStorage.removeItem("auth_token"); navigate({ to: "/login" }); form.reset(); toast.success(`Your password was just updated.`); setSaving(false); } if (!res.data.success) { toast.error(res.data.message); setSaving(false); } } catch (error) { console.log(error); toast.error("There was an error updating your password"); setSaving(false); } }, }); return (

Password Change Form

{ e.preventDefault(); e.stopPropagation(); }} > { // if ( // window.location.pathname.includes( // "/users" // ) && // value.length === 0 // ) { // return; // } if (value.length < 4) { return "Password must be at least 4 characters long."; } if (!/[A-Z]/.test(value)) { return "Password must contain at least one uppercase letter."; } if (!/[a-z]/.test(value)) { return "Password must contain at least one lower case letter."; } if (!/[0-9]/.test(value)) { return "Password must contain at least one number."; } if ( !/[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]/.test( value ) ) { return "Password must contain at least one special character."; } }, }} children={(field) => { return (
field.handleChange( e.target.value ) } /> {field.state.meta.errors.length ? ( {field.state.meta.errors.join( "," )} ) : null}
); }} /> { const password = fieldApi.form.getFieldValue("password"); if (value !== password) { return "Passwords do not match."; } }, }} > {(field) => { return (
field.handleChange( e.target.value ) } /> {field.state.meta.errors.length ? ( {field.state.meta.errors.join( ", " )} ) : null}
); }}
); }