-
Notifications
You must be signed in to change notification settings - Fork 0
[FEAT] Created Password Reset Page #31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
55e33e6
Merge workflow fix into main (#29)
OverDsh 74f856c
Chore: Update package-lock.json
OverDsh 3243418
Feat: Enabled password reset in auth.ts
OverDsh 48a8476
Feat: Created reset password page
OverDsh 63945bb
Apply suggestions from code review
OverDsh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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,110 @@ | ||
| import { Button } from "@/src/components/ui/button"; | ||
| import { | ||
| Empty, | ||
| EmptyContent, | ||
| EmptyDescription, | ||
| EmptyHeader, | ||
| EmptyTitle, | ||
| } from "@/src/components/ui/empty"; | ||
| import { | ||
| Form, | ||
| FormControl, | ||
| FormField, | ||
| FormItem, | ||
| FormMessage, | ||
| } from "@/src/components/ui/form"; | ||
| import { Input } from "@/src/components/ui/input"; | ||
| import { authClient } from "@/src/lib/auth-client"; | ||
| import { zodResolver } from "@hookform/resolvers/zod"; | ||
| import Link from "next/link"; | ||
| import { useState } from "react"; | ||
| import { useForm } from "react-hook-form"; | ||
| import { toast } from "sonner"; | ||
| import z from "zod"; | ||
|
|
||
| export default function SendResetLink() { | ||
| const [isLoading, setIsLoading] = useState(false); | ||
| const formSchema = z.object({ | ||
| email: z.email({ error: "Invalid email address" }), | ||
| }); | ||
|
|
||
| const form = useForm({ | ||
| resolver: zodResolver(formSchema), | ||
| defaultValues: { | ||
| email: "", | ||
| }, | ||
| }); | ||
|
|
||
| const handleSendResetLink = async (email: string) => { | ||
| setIsLoading(true); | ||
| const toastId = toast.loading("Sending password reset link..."); | ||
| const { error } = await authClient.requestPasswordReset({ | ||
| email, | ||
| redirectTo: "/reset-password", | ||
| }); | ||
|
|
||
| if (error) { | ||
| toast.error(error.message, { id: toastId }); | ||
| } else { | ||
| toast.success( | ||
| "Successfully sent password reset link! Please check your inbox.", | ||
| { id: toastId }, | ||
| ); | ||
| } | ||
| setIsLoading(false); | ||
| }; | ||
|
|
||
| return ( | ||
| <div className="flex min-h-dvh justify-center items-center"> | ||
| <Empty className="w-full p-10"> | ||
| <EmptyHeader> | ||
| <EmptyTitle>Reset your password</EmptyTitle> | ||
| <EmptyDescription> | ||
| We will send you a link to reset your password | ||
| </EmptyDescription> | ||
| </EmptyHeader> | ||
| <EmptyContent> | ||
| <Form {...form}> | ||
| <form | ||
| className="w-full flex flex-col gap-4" | ||
| onSubmit={form.handleSubmit((data) => | ||
| handleSendResetLink(data.email), | ||
| )} | ||
| > | ||
| <FormField | ||
| control={form.control} | ||
| name="email" | ||
| render={({ field }) => ( | ||
| <FormItem> | ||
| <FormControl> | ||
| <Input | ||
| placeholder="Email" | ||
| className="h-13 w-full" | ||
| {...field} | ||
| /> | ||
| </FormControl> | ||
| <FormMessage /> | ||
| </FormItem> | ||
| )} | ||
| /> | ||
| <Button | ||
| className="h-13 w-full" | ||
| disabled={isLoading || !form.formState.isValid} | ||
| > | ||
| Send Reset Link | ||
| </Button> | ||
| <Button | ||
| className="h-13 w-full" | ||
| variant={"outline"} | ||
| type="button" | ||
| asChild | ||
| > | ||
| <Link href={"/sign-in"}>Back To Login</Link> | ||
| </Button> | ||
| </form> | ||
| </Form> | ||
| </EmptyContent> | ||
| </Empty> | ||
| </div> | ||
| ); | ||
| } | ||
This file contains hidden or 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,135 @@ | ||
| import { Button } from "@/src/components/ui/button"; | ||
| import { | ||
| Empty, | ||
| EmptyContent, | ||
| EmptyDescription, | ||
| EmptyHeader, | ||
| EmptyTitle, | ||
| } from "@/src/components/ui/empty"; | ||
| import { | ||
| Form, | ||
| FormControl, | ||
| FormField, | ||
| FormItem, | ||
| FormMessage, | ||
| } from "@/src/components/ui/form"; | ||
| import { Input } from "@/src/components/ui/input"; | ||
| import { authClient } from "@/src/lib/auth-client"; | ||
| import { zodResolver } from "@hookform/resolvers/zod"; | ||
| import { useRouter } from "next/navigation"; | ||
| import { useState } from "react"; | ||
| import { useForm } from "react-hook-form"; | ||
| import { toast } from "sonner"; | ||
| import z from "zod"; | ||
|
|
||
| interface SetNewPasswordProps { | ||
| token: string; | ||
| } | ||
| export default function SetNewPassword({ token }: SetNewPasswordProps) { | ||
| const [isLoading, setIsLoading] = useState(false); | ||
| const router = useRouter(); | ||
|
|
||
| const formSchema = z | ||
| .object({ | ||
| newPassword: z | ||
| .string() | ||
| .min(6, { error: "Password must be at least 6 characters" }) | ||
| .max(256, { error: "Password must not have more than 256 characters" }), | ||
|
|
||
| confirmPassword: z.string(), | ||
| }) | ||
| .refine((data) => data.newPassword === data.confirmPassword, { | ||
| error: "Passwords do not match", | ||
OverDsh marked this conversation as resolved.
Show resolved
Hide resolved
OverDsh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| path: ["confirmPassword"], | ||
| }); | ||
|
|
||
| const form = useForm({ | ||
| resolver: zodResolver(formSchema), | ||
| defaultValues: { | ||
| newPassword: "", | ||
| confirmPassword: "", | ||
| }, | ||
| }); | ||
|
|
||
| const handlePasswordReset = async (newPassword: string) => { | ||
| setIsLoading(true); | ||
| const toastId = toast.loading("Resetting your password..."); | ||
| const { error } = await authClient.resetPassword({ | ||
| newPassword, | ||
| token, | ||
| }); | ||
|
|
||
| if (error) { | ||
| toast.error(error.message, { id: toastId }); | ||
| } else { | ||
| toast.success("Successfully reset your password!", { id: toastId }); | ||
| } | ||
|
|
||
| setIsLoading(false); | ||
| router.push("/sign-in"); | ||
| }; | ||
|
|
||
| return ( | ||
| <div className="flex min-h-dvh justify-center items-center"> | ||
| <Empty className="w-full p-10"> | ||
| <EmptyHeader> | ||
| <EmptyTitle>Reset your password</EmptyTitle> | ||
| <EmptyDescription> | ||
| Enter your new password below | ||
| </EmptyDescription> | ||
| </EmptyHeader> | ||
| <EmptyContent> | ||
| <Form {...form}> | ||
| <form | ||
| className="w-full flex flex-col gap-4" | ||
| onSubmit={form.handleSubmit((data) => | ||
| handlePasswordReset(data.newPassword), | ||
| )} | ||
| > | ||
| <FormField | ||
| control={form.control} | ||
| name="newPassword" | ||
| render={({ field }) => ( | ||
| <FormItem> | ||
| <FormControl> | ||
| <Input | ||
| placeholder="New Password" | ||
| type="password" | ||
| className="h-13 w-full" | ||
| {...field} | ||
| /> | ||
| </FormControl> | ||
| <FormMessage /> | ||
| </FormItem> | ||
| )} | ||
| /> | ||
| <FormField | ||
| control={form.control} | ||
| name="confirmPassword" | ||
| render={({ field }) => ( | ||
| <FormItem> | ||
| <FormControl> | ||
| <Input | ||
| placeholder="Confirm Password" | ||
| type="password" | ||
| className="h-13 w-full" | ||
| {...field} | ||
| /> | ||
| </FormControl> | ||
| <FormMessage /> | ||
| </FormItem> | ||
| )} | ||
| /> | ||
| <Button | ||
| className="h-13 w-full" | ||
| disabled={isLoading || !form.formState.isValid} | ||
| > | ||
| Reset Password | ||
| </Button> | ||
| </form> | ||
| </Form> | ||
| </EmptyContent> | ||
| </Empty> | ||
| </div> | ||
| ); | ||
| } | ||
This file contains hidden or 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,10 @@ | ||
| import ProtectedRoute from "@/src/components/ProtectedRoute"; | ||
| import React from "react"; | ||
|
|
||
| export default function Layout({ children }: React.PropsWithChildren) { | ||
| return ( | ||
| <ProtectedRoute rules={["requireLoggedOff"]} fallbackRoute="/dashboard"> | ||
| {children} | ||
| </ProtectedRoute> | ||
| ); | ||
| } |
This file contains hidden or 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,29 @@ | ||
| "use client"; | ||
| import { useRouter, useSearchParams } from "next/navigation"; | ||
| import { useEffect } from "react"; | ||
| import { toast } from "sonner"; | ||
| import SendResetLink from "./SendResetLink"; | ||
| import SetNewPassword from "./SetNewPassword"; | ||
|
|
||
| export default function Page() { | ||
| const searchParams = useSearchParams(); | ||
| const router = useRouter(); | ||
|
|
||
| const token = searchParams.get("token"); | ||
| const error = searchParams.get("error"); | ||
|
|
||
| useEffect(() => { | ||
| if (error) { | ||
| setTimeout(() => { | ||
| toast.error("There was an error while loading reset password form"); | ||
| router.replace("/reset-password"); | ||
| }, 100); | ||
| } | ||
| }, [error, router]); | ||
|
|
||
| if (token) { | ||
| return <SetNewPassword token={token} />; | ||
| } else { | ||
| return <SendResetLink />; | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.