import { zodResolver } from "@hookform/resolvers/zod" import { useMutation } from "@tanstack/react-query" import { createFileRoute, Link as RouterLink, redirect, useNavigate, } from "@tanstack/react-router" import { useForm } from "react-hook-form" import { z } from "zod" import { LoginService } from "@/client" import { AuthLayout } from "@/components/Common/AuthLayout" import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage, } from "@/components/ui/form" import { LoadingButton } from "@/components/ui/loading-button" import { PasswordInput } from "@/components/ui/password-input" import { isLoggedIn } from "@/hooks/useAuth" import useCustomToast from "@/hooks/useCustomToast" import { handleError } from "@/utils" const searchSchema = z.object({ token: z.string().catch(""), }) const formSchema = z .object({ new_password: z .string() .min(1, { message: "Password is required" }) .min(8, { message: "Password must be at least 8 characters" }), confirm_password: z .string() .min(1, { message: "Password confirmation is required" }), }) .refine((data) => data.new_password === data.confirm_password, { message: "The passwords don't match", path: ["confirm_password"], }) type FormData = z.infer export const Route = createFileRoute("/reset-password")({ component: ResetPassword, validateSearch: searchSchema, beforeLoad: async ({ search }) => { if (isLoggedIn()) { throw redirect({ to: "/" }) } if (!search.token) { throw redirect({ to: "/login" }) } }, head: () => ({ meta: [ { title: "Reset Password - FastAPI Cloud", }, ], }), }) function ResetPassword() { const { token } = Route.useSearch() const { showSuccessToast, showErrorToast } = useCustomToast() const navigate = useNavigate() const form = useForm({ resolver: zodResolver(formSchema), mode: "onBlur", criteriaMode: "all", defaultValues: { new_password: "", confirm_password: "", }, }) const mutation = useMutation({ mutationFn: (data: { new_password: string; token: string }) => LoginService.resetPassword({ requestBody: data }), onSuccess: () => { showSuccessToast("Password updated successfully") form.reset() navigate({ to: "/login" }) }, onError: handleError.bind(showErrorToast), }) const onSubmit = (data: FormData) => { mutation.mutate({ new_password: data.new_password, token }) } return (

Reset Password

( New Password )} /> ( Confirm Password )} /> Reset Password
Remember your password?{" "} Log in
) }