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