-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from Fingertips18/development
Create reset password page and consume its api
- Loading branch information
Showing
12 changed files
with
216 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export type ResetDTO = { | ||
token: string; | ||
password: string; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
client/src/pages/reset-password/_components/reset-password-form.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import { useNavigate, useParams } from "react-router-dom"; | ||
import { useMutation } from "@tanstack/react-query"; | ||
import { FormEvent, useState } from "react"; | ||
import { toast } from "sonner"; | ||
|
||
import { GenericResponse } from "@/lib/classes/generic-response-class"; | ||
import { ErrorResponse } from "@/lib/classes/error-response-class"; | ||
import { RESET_PASSWORD_INPUTS } from "@/constants/collections"; | ||
import { AuthService } from "@/lib/services/auth-service"; | ||
import { RESETPASSWORDKEY } from "@/constants/keys"; | ||
import { Button } from "@/components/text-button"; | ||
import { AppRoutes } from "@/constants/routes"; | ||
import { ResetDTO } from "@/lib/DTO/reset-dto"; | ||
import { Input } from "@/components/input"; | ||
|
||
const ResetPasswordForm = () => { | ||
const navigate = useNavigate(); | ||
const { token } = useParams(); | ||
const [confirmPassword, setConfirmPassword] = useState(""); | ||
const { mutate, isPending } = useMutation({ | ||
mutationKey: [RESETPASSWORDKEY], | ||
mutationFn: AuthService.resetPassword, | ||
onSuccess: (res: GenericResponse) => { | ||
toast.success(res.message); | ||
navigate(AppRoutes.SignIn); | ||
}, | ||
onError: (error: ErrorResponse) => toast.error(error.message), | ||
}); | ||
|
||
const onSubmit = (e: FormEvent<HTMLFormElement>) => { | ||
e.preventDefault(); | ||
|
||
if (!token) return; | ||
|
||
const formData = new FormData(e.currentTarget); | ||
|
||
const resetPasswordData = Object.fromEntries( | ||
formData.entries() | ||
) as ResetDTO; | ||
|
||
resetPasswordData.token = token; | ||
|
||
mutate(resetPasswordData); | ||
}; | ||
|
||
return ( | ||
<form | ||
onSubmit={onSubmit} | ||
className="p-4 lg:p-6 w-full md:w-fit rounded-md border border-primary/50 bg-primary/15 drop-shadow-2xl space-y-4 lg:space-y-6" | ||
> | ||
{RESET_PASSWORD_INPUTS.map((r) => ( | ||
<Input | ||
key={r.label} | ||
required | ||
disabled={isPending} | ||
{...r} | ||
validation={(value) => { | ||
if (r.name === "new-password") { | ||
setConfirmPassword(value); | ||
} | ||
if (r.name === undefined) { | ||
return r.validation({ | ||
pass1: value, | ||
pass2: confirmPassword, | ||
}); | ||
} | ||
|
||
return r.validation(value); | ||
}} | ||
/> | ||
))} | ||
|
||
<Button | ||
label="Sign In" | ||
disabled={isPending} | ||
loading={isPending} | ||
type="submit" | ||
/> | ||
</form> | ||
); | ||
}; | ||
|
||
export { ResetPasswordForm }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { SwitchAuth } from "@/components/switch-auth"; | ||
import { AppRoutes } from "@/constants/routes"; | ||
|
||
import { ResetPasswordForm } from "./_components/reset-password-form"; | ||
|
||
const ResetPasswordPage = () => { | ||
return ( | ||
<section className="px-4 lg:px-0 h-full flex-center flex-col gap-y-6 w-fit mx-auto"> | ||
<h2 | ||
className="text-lg lg:text-2xl font-extrabold text-center uppercase w-full | ||
rounded-md border border-primary/50 bg-primary/15 drop-shadow-2xl p-4" | ||
> | ||
Reset Password | ||
</h2> | ||
|
||
<ResetPasswordForm /> | ||
|
||
<SwitchAuth href={AppRoutes.Root} tag="Back" /> | ||
</section> | ||
); | ||
}; | ||
|
||
export { ResetPasswordPage }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters