|
| 1 | +import Head from "next/head"; |
| 2 | +import { z } from "zod"; |
| 3 | +import { useRouter } from "next/router"; |
| 4 | +import { zodResolver } from "@hookform/resolvers/zod"; |
| 5 | +import { Button, Flex, Stack } from "@chakra-ui/react"; |
| 6 | +import { SubmitHandler, useForm } from "react-hook-form"; |
| 7 | + |
| 8 | +import { API } from "@/utils/constant"; |
| 9 | +import { Toast } from "@/components/Toast"; |
| 10 | +import { Input } from "@/components/Form/Input"; |
| 11 | +import { LogoSkateHub } from "@/components/LogoSkateHub"; |
| 12 | + |
| 13 | +const resetPasswordFormSchema = z |
| 14 | + .object({ |
| 15 | + password: z.string().min(6, { |
| 16 | + message: "Senha obrigatória. Mínimo de 6 caracteres." |
| 17 | + }), |
| 18 | + passwordConfirmation: z.string().min(6, { |
| 19 | + message: "Senha de confirmação obrigatória." |
| 20 | + }) |
| 21 | + }) |
| 22 | + .refine(data => data.password === data.passwordConfirmation, { |
| 23 | + path: ["passwordConfirmation"], |
| 24 | + message: "As senhas não coincidem." |
| 25 | + }); |
| 26 | + |
| 27 | +type ResetPasswordFormSchema = z.infer<typeof resetPasswordFormSchema>; |
| 28 | + |
| 29 | +export default function ResetPassword() { |
| 30 | + const route = useRouter(); |
| 31 | + const { addToast } = Toast(); |
| 32 | + |
| 33 | + const { |
| 34 | + handleSubmit, |
| 35 | + register, |
| 36 | + resetField, |
| 37 | + formState: { errors, isSubmitting } |
| 38 | + } = useForm<ResetPasswordFormSchema>({ |
| 39 | + resolver: zodResolver(resetPasswordFormSchema), |
| 40 | + defaultValues: { passwordConfirmation: "", password: "" }, |
| 41 | + mode: "onChange" |
| 42 | + }); |
| 43 | + |
| 44 | + const onSubmit: SubmitHandler<ResetPasswordFormSchema> = async values => { |
| 45 | + const response = await fetch(`${API}/api/auth/reset-password`, { |
| 46 | + method: "POST", |
| 47 | + headers: { |
| 48 | + "Content-Type": "application/json" |
| 49 | + }, |
| 50 | + body: JSON.stringify({ ...values, code: route.query.code }) |
| 51 | + }); |
| 52 | + |
| 53 | + const data = await response.json(); |
| 54 | + |
| 55 | + if (data.error) { |
| 56 | + addToast({ |
| 57 | + title: "Erro ao processar solicitação.", |
| 58 | + message: |
| 59 | + "Houve um erro ao tentar resetar sua senha. Por favor, verifique seus dados e tente novamente. " + |
| 60 | + data.error.message, |
| 61 | + type: "error" |
| 62 | + }); |
| 63 | + resetField("password"); |
| 64 | + resetField("passwordConfirmation"); |
| 65 | + return; |
| 66 | + } |
| 67 | + |
| 68 | + addToast({ |
| 69 | + title: "Senha alterada com sucesso.", |
| 70 | + message: "Sua senha foi alterada com sucesso. Faça login para acessar a plataforma.", |
| 71 | + type: "success" |
| 72 | + }); |
| 73 | + |
| 74 | + resetField("password"); |
| 75 | + resetField("passwordConfirmation"); |
| 76 | + |
| 77 | + setTimeout(() => { |
| 78 | + route.push("/auth/signin"); |
| 79 | + }, 3000); |
| 80 | + }; |
| 81 | + |
| 82 | + return ( |
| 83 | + <> |
| 84 | + <Head> |
| 85 | + <title>Resetar senha - SkateHub</title> |
| 86 | + </Head> |
| 87 | + <Flex |
| 88 | + w={["100dvw"]} |
| 89 | + h={["100dvh"]} |
| 90 | + alignItems="center" |
| 91 | + justifyContent="center" |
| 92 | + flexDirection="column" |
| 93 | + bg="gray.900" |
| 94 | + backgroundSize="cover" |
| 95 | + backgroundRepeat="no-repeat" |
| 96 | + backgroundBlendMode="overlay" |
| 97 | + backgroundPosition="center bottom" |
| 98 | + backgroundImage="../alexander-londono-unsplash.jpeg" |
| 99 | + px={["4", "0"]} |
| 100 | + > |
| 101 | + <Flex |
| 102 | + as="form" |
| 103 | + w="100%" |
| 104 | + maxWidth={425} |
| 105 | + bg="gray.800" |
| 106 | + p="8" |
| 107 | + borderRadius={8} |
| 108 | + flexDir="column" |
| 109 | + onSubmit={handleSubmit(onSubmit)} |
| 110 | + > |
| 111 | + <Stack spacing={4}> |
| 112 | + <Flex justifyContent="center" mb="4"> |
| 113 | + <LogoSkateHub /> |
| 114 | + </Flex> |
| 115 | + <Flex flexDir="column"> |
| 116 | + <Input |
| 117 | + id="password" |
| 118 | + type="password" |
| 119 | + label="Password" |
| 120 | + placeholder="Digite sua nova senha" |
| 121 | + {...register("password")} |
| 122 | + error={errors.password} |
| 123 | + /> |
| 124 | + </Flex> |
| 125 | + <Flex flexDir="column"> |
| 126 | + <Input |
| 127 | + id="passwordConfirmation" |
| 128 | + type="password" |
| 129 | + label="passwordConfirmation" |
| 130 | + placeholder="Digite seu passwordConfirmation" |
| 131 | + {...register("passwordConfirmation")} |
| 132 | + error={errors.passwordConfirmation} |
| 133 | + /> |
| 134 | + </Flex> |
| 135 | + </Stack> |
| 136 | + <Button type="submit" mt="6" colorScheme="green" size="lg" isLoading={isSubmitting} loadingText="Enviando..."> |
| 137 | + Confirmar |
| 138 | + </Button> |
| 139 | + </Flex> |
| 140 | + </Flex> |
| 141 | + </> |
| 142 | + ); |
| 143 | +} |
0 commit comments