2024-03-08 14:58:36 +01:00
|
|
|
import {
|
|
|
|
Button,
|
|
|
|
Container,
|
|
|
|
FormControl,
|
|
|
|
FormErrorMessage,
|
|
|
|
FormLabel,
|
|
|
|
Heading,
|
|
|
|
Input,
|
|
|
|
Text,
|
2024-03-17 17:28:45 +01:00
|
|
|
} 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"
|
2024-02-29 17:30:58 -05:00
|
|
|
|
2024-03-17 17:28:45 +01:00
|
|
|
import { type ApiError, LoginService, type NewPassword } from "../client"
|
|
|
|
import { isLoggedIn } from "../hooks/useAuth"
|
|
|
|
import useCustomToast from "../hooks/useCustomToast"
|
2024-04-01 22:53:33 -05:00
|
|
|
import { confirmPasswordRules, passwordRules } from "../utils"
|
2024-02-29 17:30:58 -05:00
|
|
|
|
|
|
|
interface NewPasswordForm extends NewPassword {
|
2024-03-08 14:58:36 +01:00
|
|
|
confirm_password: string
|
2024-02-29 17:30:58 -05:00
|
|
|
}
|
|
|
|
|
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-07 19:16:23 +01:00
|
|
|
}
|
2024-03-08 14:58:36 +01:00
|
|
|
},
|
2024-03-07 19:16:23 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
function ResetPassword() {
|
2024-03-08 14:58:36 +01:00
|
|
|
const {
|
|
|
|
register,
|
|
|
|
handleSubmit,
|
|
|
|
getValues,
|
2024-03-10 00:53:46 +01:00
|
|
|
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 showToast = useCustomToast()
|
2024-03-10 00:53:46 +01:00
|
|
|
const navigate = useNavigate()
|
2024-03-07 19:16:23 +01:00
|
|
|
|
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")
|
2024-03-13 22:59:28 +01:00
|
|
|
if (!token) return
|
2024-03-08 14:58:36 +01:00
|
|
|
await LoginService.resetPassword({
|
2024-03-13 22:59:28 +01:00
|
|
|
requestBody: { new_password: data.new_password, token: token },
|
2024-03-07 19:16:23 +01:00
|
|
|
})
|
2024-03-08 14:58:36 +01:00
|
|
|
}
|
2024-03-07 19:16:23 +01:00
|
|
|
|
2024-04-04 16:30:42 +02:00
|
|
|
const mutation = useMutation({
|
|
|
|
mutationFn: resetPassword,
|
2024-03-08 14:58:36 +01:00
|
|
|
onSuccess: () => {
|
2024-07-30 09:09:57 -05:00
|
|
|
showToast("Success!", "Password updated successfully.", "success")
|
2024-03-10 00:53:46 +01:00
|
|
|
reset()
|
2024-03-17 17:28:45 +01:00
|
|
|
navigate({ to: "/login" })
|
2024-03-08 14:58:36 +01:00
|
|
|
},
|
|
|
|
onError: (err: ApiError) => {
|
2024-04-03 16:40:33 -05:00
|
|
|
const errDetail = (err.body as any)?.detail
|
2024-03-17 17:28:45 +01:00
|
|
|
showToast("Something went wrong.", `${errDetail}`, "error")
|
2024-03-08 14:58:36 +01:00
|
|
|
},
|
|
|
|
})
|
2024-03-07 19:16:23 +01:00
|
|
|
|
2024-03-08 14:58:36 +01:00
|
|
|
const onSubmit: SubmitHandler<NewPasswordForm> = async (data) => {
|
|
|
|
mutation.mutate(data)
|
|
|
|
}
|
2024-02-29 17:30:58 -05:00
|
|
|
|
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>
|
|
|
|
<FormControl mt={4} isInvalid={!!errors.new_password}>
|
|
|
|
<FormLabel htmlFor="password">Set Password</FormLabel>
|
|
|
|
<Input
|
|
|
|
id="password"
|
2024-04-01 22:53:33 -05:00
|
|
|
{...register("new_password", passwordRules())}
|
2024-03-08 14:58:36 +01:00
|
|
|
placeholder="Password"
|
|
|
|
type="password"
|
|
|
|
/>
|
|
|
|
{errors.new_password && (
|
|
|
|
<FormErrorMessage>{errors.new_password.message}</FormErrorMessage>
|
|
|
|
)}
|
|
|
|
</FormControl>
|
|
|
|
<FormControl mt={4} isInvalid={!!errors.confirm_password}>
|
|
|
|
<FormLabel htmlFor="confirm_password">Confirm Password</FormLabel>
|
|
|
|
<Input
|
|
|
|
id="confirm_password"
|
2024-04-01 22:53:33 -05:00
|
|
|
{...register("confirm_password", confirmPasswordRules(getValues))}
|
2024-03-08 14:58:36 +01:00
|
|
|
placeholder="Password"
|
|
|
|
type="password"
|
|
|
|
/>
|
|
|
|
{errors.confirm_password && (
|
|
|
|
<FormErrorMessage>{errors.confirm_password.message}</FormErrorMessage>
|
|
|
|
)}
|
|
|
|
</FormControl>
|
2024-03-15 01:05:28 +01:00
|
|
|
<Button variant="primary" type="submit">
|
2024-03-08 14:58:36 +01:00
|
|
|
Reset Password
|
|
|
|
</Button>
|
|
|
|
</Container>
|
|
|
|
)
|
|
|
|
}
|