Files
musicshare/apps/web/src/app/reset-password/page.tsx
T
chrisandClaude Opus 5.5 1f0cdb68f5
CI / check (push) Canceled after 0s
Password reset by email
"Forgot your password?" on web and mobile sends a one-time link (1 hour)
to the account's email; the website's reset page sets the new password,
signs out every other session and signs in. The request responds the same
whether or not the email has an account, and the email goes out in the
background so timing doesn't reveal it either. Only a hash of the link
token is stored. Sends through SMTP_* settings; without them, emails are
logged (and captured in tests).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01MzFywujuH9TiuXbr9vjoX8
2026-10-06 01:05:30 -04:00

82 lines
2.4 KiB
TypeScript

'use client';
import Link from 'next/link';
import { useRouter, useSearchParams } from 'next/navigation';
import { Suspense, useState } from 'react';
import type { MeDTO } from '@musicshare/shared';
import { api, useMe } from '@/lib/api';
export default function ResetPasswordPage() {
return (
<Suspense>
<ResetPassword />
</Suspense>
);
}
function ResetPassword() {
const token = useSearchParams().get('token');
const router = useRouter();
const { mutate } = useMe();
const [show, setShow] = useState(false);
const [error, setError] = useState<string | null>(null);
const [busy, setBusy] = useState(false);
if (!token) {
return (
<div className="card form narrow">
<h1>Link incomplete</h1>
<p>This reset link is missing part of its address. Open it straight from the email, or ask for a new one.</p>
<Link href="/forgot-password" className="btn btn-primary">
Send a new link
</Link>
</div>
);
}
const onSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault();
const password = String(new FormData(e.currentTarget).get('password') ?? '');
setBusy(true);
setError(null);
try {
const { user } = await api<{ user: MeDTO }>('/auth/reset-password', { body: { token, password } });
await mutate(user, { revalidate: false });
router.replace('/');
} catch (err) {
setError((err as Error).message);
setBusy(false);
}
};
return (
<form className="card form narrow" onSubmit={onSubmit}>
<h1>Choose a new password</h1>
<label>
New password
<input
name="password"
type={show ? 'text' : 'password'}
required
minLength={8}
maxLength={200}
autoComplete="new-password"
autoFocus
/>
</label>
<label className="checkbox">
<input type="checkbox" checked={show} onChange={(e) => setShow(e.target.checked)} /> Show password
</label>
{error && (
<p className="error">
{error} {error.includes('expired') && <Link href="/forgot-password">Send a new link</Link>}
</p>
)}
<button className="btn btn-primary" disabled={busy}>
{busy ? 'Saving…' : 'Save and log in'}
</button>
<p className="muted">This signs you out on your other devices.</p>
</form>
);
}