Files
full-stack-fastapi-template/frontend/src/routes/reset-password.tsx

107 lines
2.8 KiB
TypeScript
Raw Normal View History

import { Container, Heading, Text } from "@chakra-ui/react"
2024-04-08 15:49:22 -05:00
import { useMutation } from "@tanstack/react-query"
2024-03-17 17:28:45 +01:00
import { createFileRoute, redirect, useNavigate } from "@tanstack/react-router"
import { type SubmitHandler, useForm } from "react-hook-form"
import { FiLock } from "react-icons/fi"
2024-03-17 17:28:45 +01:00
import { type ApiError, LoginService, type NewPassword } from "../client"
import { Button } from "../components/ui/button"
import { PasswordInput } from "../components/ui/password-input"
2024-03-17 17:28:45 +01:00
import { isLoggedIn } from "../hooks/useAuth"
import useCustomToast from "../hooks/useCustomToast"
2024-08-01 13:01:03 -05:00
import { confirmPasswordRules, handleError, passwordRules } from "../utils"
interface NewPasswordForm extends NewPassword {
2024-03-08 14:58:36 +01:00
confirm_password: string
}
2024-03-17 17:28:45 +01:00
export const Route = createFileRoute("/reset-password")({
2024-03-08 14:58:36 +01:00
component: ResetPassword,
beforeLoad: async () => {
if (isLoggedIn()) {
throw redirect({
2024-03-17 17:28:45 +01:00
to: "/",
2024-03-08 14:58:36 +01:00
})
}
2024-03-08 14:58:36 +01:00
},
})
function ResetPassword() {
2024-03-08 14:58:36 +01:00
const {
register,
handleSubmit,
getValues,
reset,
2024-03-08 14:58:36 +01:00
formState: { errors },
} = useForm<NewPasswordForm>({
2024-03-17 17:28:45 +01:00
mode: "onBlur",
criteriaMode: "all",
2024-03-08 14:58:36 +01:00
defaultValues: {
2024-03-17 17:28:45 +01:00
new_password: "",
2024-03-08 14:58:36 +01:00
},
})
const { showSuccessToast } = useCustomToast()
const navigate = useNavigate()
2024-03-08 14:58:36 +01:00
const resetPassword = async (data: NewPassword) => {
2024-03-17 17:28:45 +01:00
const token = new URLSearchParams(window.location.search).get("token")
if (!token) return
2024-03-08 14:58:36 +01:00
await LoginService.resetPassword({
requestBody: { new_password: data.new_password, token: token },
})
2024-03-08 14:58:36 +01:00
}
const mutation = useMutation({
mutationFn: resetPassword,
2024-03-08 14:58:36 +01:00
onSuccess: () => {
showSuccessToast("Password updated successfully.")
reset()
2024-03-17 17:28:45 +01:00
navigate({ to: "/login" })
2024-03-08 14:58:36 +01:00
},
onError: (err: ApiError) => {
handleError(err)
2024-03-08 14:58:36 +01:00
},
})
2024-03-08 14:58:36 +01:00
const onSubmit: SubmitHandler<NewPasswordForm> = async (data) => {
mutation.mutate(data)
}
2024-03-08 14:58:36 +01:00
return (
<Container
as="form"
onSubmit={handleSubmit(onSubmit)}
h="100vh"
maxW="sm"
alignItems="stretch"
justifyContent="center"
gap={4}
centerContent
>
<Heading size="xl" color="ui.main" textAlign="center" mb={2}>
Reset Password
</Heading>
<Text textAlign="center">
Please enter your new password and confirm it to reset your password.
</Text>
<PasswordInput
startElement={<FiLock />}
type="new_password"
errors={errors}
{...register("new_password", passwordRules())}
placeholder="New Password"
/>
<PasswordInput
startElement={<FiLock />}
type="confirm_password"
errors={errors}
{...register("confirm_password", confirmPasswordRules(getValues))}
placeholder="Confirm Password"
/>
<Button variant="solid" type="submit">
2024-03-08 14:58:36 +01:00
Reset Password
</Button>
</Container>
)
}