All files / src/pages reset-password.tsx

100% Statements 34/34
100% Branches 20/20
100% Functions 8/8
100% Lines 32/32

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                                            62x 62x   62x 62x 62x 62x 62x           62x       62x           62x 11x   11x 2x 2x     9x 1x 1x     8x   8x 8x 4x   3x   7x       62x 2x                               60x 4x                               56x                                                   12x 1x                                 11x   1x                                  
/**
 * Reset password — the page a reset link lands on.
 *
 * Reskinned to the Job Book (redesign plan 4.2): the `AuthShell` frame, ruled
 * fields composed from `Field`/`Label`/`Input`, and the two dead ends (no
 * token, done) printed as the same frame with a different statement rather
 * than as their own centred cards.
 */
 
import { Link, useSearch } from "@tanstack/react-router";
import { type FormEvent, useState } from "react";
import { AuthShell } from "../components/auth/auth-shell";
import { Button } from "../components/ui/button";
import { Card, CardContent } from "../components/ui/card";
import { Field, Label } from "../components/ui/field";
import { Input } from "../components/ui/input";
import { PasswordStrength } from "../components/ui/password-strength";
import { authApi, getErrorMessage } from "../lib/api";
import { notify } from "../lib/notify";
import { isStrongPassword } from "../lib/validation";
 
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;
		}
 
		setIsLoading(true);
 
		try {
			await authApi.resetPassword(token, password);
			setSuccess(true);
		} catch (err) {
			notify.error(getErrorMessage(err));
		} finally {
			setIsLoading(false);
		}
	};
 
	if (!token) {
		return (
			<AuthShell
				title="This link has expired"
				lead="This password reset link is invalid or has expired."
			>
				<Card>
					<CardContent className="p-5">
						<Link to="/forgot-password" className="block">
							<Button className="w-full">Request a new link</Button>
						</Link>
					</CardContent>
				</Card>
			</AuthShell>
		);
	}
 
	if (success) {
		return (
			<AuthShell
				title="Password reset"
				lead="Your password has been successfully reset."
			>
				<Card>
					<CardContent className="p-5">
						<Link to="/login" className="block">
							<Button className="w-full">Sign in</Button>
						</Link>
					</CardContent>
				</Card>
			</AuthShell>
		);
	}
 
	return (
		<AuthShell
			title="Choose a new password"
			lead="Make it one you don't use anywhere else."
			footer={
				<>
					Remembered your password?{" "}
					<Link
						to="/login"
						className="font-semibold text-foreground-default underline decoration-2 underline-offset-4 hover:text-primary-600"
					>
						Sign in
					</Link>
				</>
			}
		>
			<Card>
				<CardContent className="p-5">
					<form onSubmit={handleSubmit} className="space-y-4" noValidate>
						<Field controlId="password">
							<Label>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"
							/>
							<div className="mt-2">
								<PasswordStrength password={password} />
							</div>
						</Field>
 
						<Field controlId="confirmPassword">
							<Label>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"
							/>
						</Field>
 
						<Button type="submit" className="w-full" isLoading={isLoading}>
							Reset password
						</Button>
					</form>
				</CardContent>
			</Card>
		</AuthShell>
	);
}