From 140311d7419fa2192e5149df8f589c3c3733a399 Mon Sep 17 00:00:00 2001 From: Mohamed Bassem Date: Thu, 10 Jul 2025 20:50:19 +0000 Subject: feat: Support forget and reset password --- apps/web/app/forgot-password/page.tsx | 22 +++ apps/web/app/reset-password/page.tsx | 36 +++++ apps/web/components/signin/CredentialsForm.tsx | 9 ++ apps/web/components/signin/ForgotPasswordForm.tsx | 149 ++++++++++++++++++ apps/web/components/signin/ResetPasswordForm.tsx | 181 ++++++++++++++++++++++ 5 files changed, 397 insertions(+) create mode 100644 apps/web/app/forgot-password/page.tsx create mode 100644 apps/web/app/reset-password/page.tsx create mode 100644 apps/web/components/signin/ForgotPasswordForm.tsx create mode 100644 apps/web/components/signin/ResetPasswordForm.tsx (limited to 'apps/web') diff --git a/apps/web/app/forgot-password/page.tsx b/apps/web/app/forgot-password/page.tsx new file mode 100644 index 00000000..1faa8967 --- /dev/null +++ b/apps/web/app/forgot-password/page.tsx @@ -0,0 +1,22 @@ +import { redirect } from "next/navigation"; +import KarakeepLogo from "@/components/KarakeepIcon"; +import ForgotPasswordForm from "@/components/signin/ForgotPasswordForm"; +import { getServerAuthSession } from "@/server/auth"; + +export default async function ForgotPasswordPage() { + const session = await getServerAuthSession(); + if (session) { + redirect("/"); + } + + return ( +
+
+
+ +
+ +
+
+ ); +} diff --git a/apps/web/app/reset-password/page.tsx b/apps/web/app/reset-password/page.tsx new file mode 100644 index 00000000..1d05606e --- /dev/null +++ b/apps/web/app/reset-password/page.tsx @@ -0,0 +1,36 @@ +import { redirect } from "next/navigation"; +import KarakeepLogo from "@/components/KarakeepIcon"; +import ResetPasswordForm from "@/components/signin/ResetPasswordForm"; +import { getServerAuthSession } from "@/server/auth"; + +interface ResetPasswordPageProps { + searchParams: { + token?: string; + }; +} + +export default async function ResetPasswordPage({ + searchParams, +}: ResetPasswordPageProps) { + const session = await getServerAuthSession(); + if (session) { + redirect("/"); + } + + const { token } = searchParams; + + if (!token) { + redirect("/signin"); + } + + return ( +
+
+
+ +
+ +
+
+ ); +} diff --git a/apps/web/components/signin/CredentialsForm.tsx b/apps/web/components/signin/CredentialsForm.tsx index 1ad240a7..a3b854b7 100644 --- a/apps/web/components/signin/CredentialsForm.tsx +++ b/apps/web/components/signin/CredentialsForm.tsx @@ -142,6 +142,15 @@ export default function CredentialsForm() { > Sign In + +
+ + Forgot your password? + +
diff --git a/apps/web/components/signin/ForgotPasswordForm.tsx b/apps/web/components/signin/ForgotPasswordForm.tsx new file mode 100644 index 00000000..29d55f2b --- /dev/null +++ b/apps/web/components/signin/ForgotPasswordForm.tsx @@ -0,0 +1,149 @@ +"use client"; + +import { useState } from "react"; +import { useRouter } from "next/navigation"; +import { ActionButton } from "@/components/ui/action-button"; +import { Alert, AlertDescription } from "@/components/ui/alert"; +import { + Card, + CardContent, + CardDescription, + CardHeader, + CardTitle, +} from "@/components/ui/card"; +import { + Form, + FormControl, + FormField, + FormItem, + FormLabel, + FormMessage, +} from "@/components/ui/form"; +import { Input } from "@/components/ui/input"; +import { api } from "@/lib/trpc"; +import { zodResolver } from "@hookform/resolvers/zod"; +import { TRPCClientError } from "@trpc/client"; +import { AlertCircle, CheckCircle } from "lucide-react"; +import { useForm } from "react-hook-form"; +import { z } from "zod"; + +const forgotPasswordSchema = z.object({ + email: z.string().email("Please enter a valid email address"), +}); + +export default function ForgotPasswordForm() { + const [isSubmitted, setIsSubmitted] = useState(false); + const [errorMessage, setErrorMessage] = useState(""); + const router = useRouter(); + + const form = useForm>({ + resolver: zodResolver(forgotPasswordSchema), + }); + + const forgotPasswordMutation = api.users.forgotPassword.useMutation(); + + const onSubmit = async (values: z.infer) => { + try { + setErrorMessage(""); + await forgotPasswordMutation.mutateAsync(values); + setIsSubmitted(true); + } catch (error) { + if (error instanceof TRPCClientError) { + setErrorMessage(error.message); + } else { + setErrorMessage("An unexpected error occurred. Please try again."); + } + } + }; + + return ( + + + + {isSubmitted ? "Check your email" : "Forgot your password?"} + + + {isSubmitted + ? "If an account with that email exists, we've sent you a password reset link." + : "Enter your email address and we'll send you a link to reset your password."} + + + + {isSubmitted ? ( + <> +
+ +
+ + + If an account with that email exists, we've sent you a + password reset link. + + + router.push("/signin")} + className="w-full" + > + Back to Sign In + + + ) : ( + <> +
+ + {errorMessage && ( + + + {errorMessage} + + )} + + ( + + Email + + + + + + )} + /> + + + Send Reset Link + + + + +
+ router.push("/signin")} + className="w-full" + > + Back to Sign In + +
+ + )} +
+
+ ); +} diff --git a/apps/web/components/signin/ResetPasswordForm.tsx b/apps/web/components/signin/ResetPasswordForm.tsx new file mode 100644 index 00000000..d4d8a285 --- /dev/null +++ b/apps/web/components/signin/ResetPasswordForm.tsx @@ -0,0 +1,181 @@ +"use client"; + +import { useState } from "react"; +import { useRouter } from "next/navigation"; +import { ActionButton } from "@/components/ui/action-button"; +import { Alert, AlertDescription } from "@/components/ui/alert"; +import { + Card, + CardContent, + CardDescription, + CardHeader, + CardTitle, +} from "@/components/ui/card"; +import { + Form, + FormControl, + FormField, + FormItem, + FormLabel, + FormMessage, +} from "@/components/ui/form"; +import { Input } from "@/components/ui/input"; +import { api } from "@/lib/trpc"; +import { zodResolver } from "@hookform/resolvers/zod"; +import { TRPCClientError } from "@trpc/client"; +import { AlertCircle, CheckCircle } from "lucide-react"; +import { useForm } from "react-hook-form"; +import { z } from "zod"; + +import { zResetPasswordSchema } from "@karakeep/shared/types/users"; + +const resetPasswordSchema = z + .object({ + confirmPassword: z.string(), + }) + .merge(zResetPasswordSchema.pick({ newPassword: true })) + .refine((data) => data.newPassword === data.confirmPassword, { + message: "Passwords don't match", + path: ["confirmPassword"], + }); + +interface ResetPasswordFormProps { + token: string; +} + +export default function ResetPasswordForm({ token }: ResetPasswordFormProps) { + const [isSuccess, setIsSuccess] = useState(false); + const [errorMessage, setErrorMessage] = useState(""); + const router = useRouter(); + + const form = useForm>({ + resolver: zodResolver(resetPasswordSchema), + }); + + const resetPasswordMutation = api.users.resetPassword.useMutation(); + + const onSubmit = async (values: z.infer) => { + try { + setErrorMessage(""); + await resetPasswordMutation.mutateAsync({ + token, + newPassword: values.newPassword, + }); + setIsSuccess(true); + } catch (error) { + if (error instanceof TRPCClientError) { + setErrorMessage(error.message); + } else { + setErrorMessage("An unexpected error occurred. Please try again."); + } + } + }; + + return ( + + + + {isSuccess ? "Password reset successful" : "Reset your password"} + + + {isSuccess + ? "Your password has been successfully reset. You can now sign in with your new password." + : "Enter your new password below."} + + + + {isSuccess ? ( + <> +
+ +
+ + + Your password has been successfully reset. You can now sign in + with your new password. + + + router.push("/signin")} + className="w-full" + > + Go to Sign In + + + ) : ( + <> +
+ + {errorMessage && ( + + + {errorMessage} + + )} + + ( + + New Password + + + + + + )} + /> + + ( + + Confirm New Password + + + + + + )} + /> + + + Reset Password + + + + +
+ router.push("/signin")} + className="w-full" + > + Back to Sign In + +
+ + )} +
+
+ ); +} -- cgit v1.2.3-70-g09d2