import { useEffect, useState } from "react"; import { toast } from "sonner"; import { ThemeToggle } from "../components/ThemeToggle"; import { Button } from "../components/ui/button"; import { authClient } from "../lib/auth-client"; interface ResetPasswordPageProps { navigate: ( route: | "/" | "/login" | "/signup" | "/pricing" | "/dashboard" | "/forgot-password" | "/reset-password", ) => void; } export default function ResetPasswordPage({ navigate }: ResetPasswordPageProps) { const [password, setPassword] = useState(""); const [confirmPassword, setConfirmPassword] = useState(""); const [isLoading, setIsLoading] = useState(false); const [error, setError] = useState(null); const [token, setToken] = useState(null); useEffect(() => { // Extract token from URL - Better Auth adds it as a query param const hash = window.location.hash; const searchParams = new URLSearchParams(hash.split("?")[1] || ""); const tokenParam = searchParams.get("token"); setToken(tokenParam); }, []); const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); setError(null); if (password.length < 8) { setError("Password must be at least 8 characters long."); return; } if (password !== confirmPassword) { setError("Passwords do not match."); return; } if (!token) { setError("Invalid or missing reset token. Please request a new reset link."); return; } setIsLoading(true); try { const result = await authClient.resetPassword({ newPassword: password, token, }); if (result.error) { setError(result.error.message || "Failed to reset password. The link may have expired."); setIsLoading(false); return; } toast.success("Password reset successfully", { description: "You can now sign in with your new password.", }); navigate("/login"); } catch (err) { setError("An unexpected error occurred. Please try again."); } setIsLoading(false); }; if (!token) { return (
{/* Navigation */}

Invalid Reset Link

This password reset link is invalid or has expired. Please request a new one.

); } return (
{/* Navigation */} {/* Form */}

Set new password

Enter your new password below

{error && (

{error}

)}
setPassword(e.target.value)} placeholder="Enter new password" required className="w-full px-3 py-2 border border-border rounded-md bg-background focus:outline-none focus:ring-2 focus:ring-primary focus:border-transparent" />
setConfirmPassword(e.target.value)} placeholder="Confirm new password" required className="w-full px-3 py-2 border border-border rounded-md bg-background focus:outline-none focus:ring-2 focus:ring-primary focus:border-transparent" />
); }