import { useState } from 'react';
import { useTranslation } from 'react-i18next';
import { Link, useNavigate, useSearchParams } from 'react-router';
import { Wordmark } from '../components/Logo';
import { Button } from '../components/ui/button';
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '../components/ui/card';
import { Input } from '../components/ui/input';
import { authClient } from '../lib/auth-client';
import { authErrorKey, unwrap } from '../lib/auth-errors';
const MIN_PASSWORD_LENGTH = 8;
/**
* Landing page of the emailed reset link. The auth API checks the link and
* sends the browser here with `?token=` (valid) or `?error=INVALID_TOKEN`.
* Setting the password signs out every session of the account.
*/
export default function ResetPassword() {
const { t } = useTranslation();
const navigate = useNavigate();
const [searchParams] = useSearchParams();
const token = searchParams.get('token');
const validToken = !!token && !searchParams.get('error');
const [password, setPassword] = useState('');
const [confirmPassword, setConfirmPassword] = useState('');
const [error, setError] = useState('');
const [success, setSuccess] = useState(false);
const [loading, setLoading] = useState(false);
const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault();
setError('');
if (password !== confirmPassword) {
setError(t('auth.reset.mismatch'));
return;
}
if (password.length < MIN_PASSWORD_LENGTH) {
setError(t('auth.errors.passwordTooShort'));
return;
}
setLoading(true);
try {
unwrap(await authClient.resetPassword({ newPassword: password, token: token ?? '' }));
setSuccess(true);
setTimeout(() => navigate('/login'), 2000);
} catch (err) {
setError(t(authErrorKey(err, 'auth.reset.failed')));
} finally {
setLoading(false);
}
};
const header = (