import { useAdminResetPassword } from "medusa-react" import qs from "qs" import { useForm } from "react-hook-form" import { decodeToken } from "react-jwt" import { useLocation, useNavigate } from "react-router-dom" import InputError from "../components/atoms/input-error" import Button from "../components/fundamentals/button" import SigninInput from "../components/molecules/input-signin" import SEO from "../components/seo" import PublicLayout from "../components/templates/login-layout" import useNotification from "../hooks/use-notification" import { getErrorMessage } from "../utils/error-messages" import FormValidator from "../utils/form-validator" type formValues = { password: string repeat_password: string } const ResetPasswordPage = () => { const navigate = useNavigate() const location = useLocation() const parsed = qs.parse(location.search.substring(1)) let token: { email: string } | null = null if (parsed?.token) { try { token = decodeToken(parsed.token as string) } catch (e) { token = null } } const email = (token?.email || parsed?.email || "") as string const { register, handleSubmit, formState: { errors }, setError, } = useForm({ defaultValues: { password: "", repeat_password: "", }, }) const reset = useAdminResetPassword() const notification = useNotification() const onSubmit = handleSubmit((data: formValues) => { if (data.password !== data.repeat_password) { setError( "repeat_password", { type: "manual", message: "Passwords do not match", }, { shouldFocus: true, } ) return } reset.mutate( { token: parsed.token as string, password: data.password, email: email, }, { onSuccess: () => { navigate("/login") }, onError: (err) => { notification("Error", getErrorMessage(err), "error") }, } ) }) return (
{token ? (

Reset your password

Go back to sign in
) : (

Your reset link is invalid

Try resetting your password again.

)}
) } export default ResetPasswordPage