All files / src/pages reset-password.tsx

83.78% Statements 31/37
95.45% Branches 21/22
50% Functions 4/8
88.57% Lines 31/35

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183                                    55x 55x   55x 55x 55x 55x 55x     55x       55x       55x 11x   11x 2x 2x     9x 1x 1x     8x         8x   8x 8x 4x   3x   7x       55x 2x                                     53x 4x                                         49x                                                   10x                                         10x                                                      
import { useState, type FormEvent } from "react";
import { Link, useSearch } from "@tanstack/react-router";
import { authApi, getErrorMessage } from "../lib/api";
import { notify } from "../lib/notify";
import { isStrongPassword } from "../lib/validation";
import { Button } from "../components/ui/button";
import { Input } from "../components/ui/input";
import { PasswordStrength } from "../components/ui/password-strength";
import {
	Card,
	CardContent,
	CardDescription,
	CardHeader,
	CardTitle,
} from "../components/ui/card";
import { LogoIcon } from "../components/ui/logo-icon";
 
export function ResetPasswordPage() {
	const search = useSearch({ strict: false }) as { token?: string };
	const token = search.token || "";
 
	const [password, setPassword] = useState("");
	const [confirmPassword, setConfirmPassword] = useState("");
	const [isLoading, setIsLoading] = useState(false);
	const [success, setSuccess] = useState(false);
	const [touched, setTouched] = useState({ password: false, confirmPassword: false });
 
	const passwordError =
		touched.password && password.length > 0 && !isStrongPassword(password)
			? "Please meet all password requirements"
			: undefined;
	const confirmError =
		touched.confirmPassword && confirmPassword.length > 0 && password !== confirmPassword
			? "Passwords do not match"
			: undefined;
 
	const handleSubmit = async (e: FormEvent) => {
		e.preventDefault();
 
		if (!isStrongPassword(password)) {
			setTouched({ password: true, confirmPassword: true });
			return;
		}
 
		if (password !== confirmPassword) {
			setTouched({ password: true, confirmPassword: true });
			return;
		}
 
		Iif (!token) {
			notify.error("Invalid or missing reset token");
			return;
		}
 
		setIsLoading(true);
 
		try {
			await authApi.resetPassword(token, password);
			setSuccess(true);
		} catch (err) {
			notify.error(getErrorMessage(err));
		} finally {
			setIsLoading(false);
		}
	};
 
	if (!token) {
		return (
			<div className="min-h-screen flex items-center justify-center bg-background-muted px-4">
				<Card className="w-full max-w-md">
					<CardHeader className="space-y-1">
						<CardTitle className="text-2xl text-center">Invalid Link</CardTitle>
						<CardDescription className="text-center">
							This password reset link is invalid or has expired.
						</CardDescription>
					</CardHeader>
					<CardContent>
						<Link to="/forgot-password">
							<Button className="w-full">Request New Reset Link</Button>
						</Link>
					</CardContent>
				</Card>
			</div>
		);
	}
 
	if (success) {
		return (
			<div className="min-h-screen flex items-center justify-center bg-background-muted px-4">
				<Card className="w-full max-w-md">
					<CardHeader className="space-y-1">
						<CardTitle className="text-2xl text-center">
							Password Reset
						</CardTitle>
						<CardDescription className="text-center">
							Your password has been successfully reset.
						</CardDescription>
					</CardHeader>
					<CardContent>
						<Link to="/login">
							<Button className="w-full">Sign In</Button>
						</Link>
					</CardContent>
				</Card>
			</div>
		);
	}
 
	return (
		<div className="min-h-screen flex items-center justify-center bg-background-muted px-4">
			<Card className="w-full max-w-md">
				<CardHeader className="space-y-1">
					<div className="flex items-center justify-center gap-2 mb-1">
						<LogoIcon size={36} className="text-foreground-default" />
					</div>
					<CardTitle className="text-2xl text-center">Reset Password</CardTitle>
					<CardDescription className="text-center">
						Enter your new password below
					</CardDescription>
				</CardHeader>
				<CardContent>
					<form onSubmit={handleSubmit} className="space-y-4">
						<div className="space-y-2">
							<label
								htmlFor="password"
								className="text-sm font-medium text-foreground-default"
							>
								New Password
							</label>
							<Input
								id="password"
								type="password"
								placeholder="Enter new password"
								value={password}
								onChange={(e) => setPassword(e.target.value)}
								onBlur={() => setTouched((t) => ({ ...t, password: true }))}
								error={passwordError}
								required
								autoComplete="new-password"
							/>
							<PasswordStrength password={password} />
						</div>
 
						<div className="space-y-2">
							<label
								htmlFor="confirmPassword"
								className="text-sm font-medium text-foreground-default"
							>
								Confirm Password
							</label>
							<Input
								id="confirmPassword"
								type="password"
								placeholder="Confirm new password"
								value={confirmPassword}
								onChange={(e) => setConfirmPassword(e.target.value)}
								onBlur={() => setTouched((t) => ({ ...t, confirmPassword: true }))}
								error={confirmError}
								required
								autoComplete="new-password"
							/>
						</div>
 
						<Button type="submit" className="w-full" isLoading={isLoading}>
							Reset Password
						</Button>
					</form>
 
					<div className="mt-6 text-center text-sm text-foreground-muted">
						Remember your password?{" "}
						<Link
							to="/login"
							className="text-primary-600 hover:underline font-medium"
						>
							Sign in
						</Link>
					</div>
				</CardContent>
			</Card>
		</div>
	);
}