auth: e-mail + password login with e-mailed password links
Sign-up asks no password: a link goes to the address and the password is set there (also for forgotten passwords), by whoever opens the mailbox. So nobody can register a password for someone else's address, and linking Google or Apple to an account with the same address stays safe. Opening the link confirms the address, ends other sessions and logs in. Sign-up answers alike for registered addresses (the owner gets an 'account exists' link), links are limited to one per kind a minute and five an hour, and unconfirmed password accounts cannot log in. Frontend: login form with sign-up and forgotten password next to the social buttons, and the /jelszo page for the link. Still behind PASSWORD_LOGIN (off on prod). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012vLHzh5qUhFtmJhzkcTbXL
This commit is contained in:
@@ -0,0 +1,92 @@
|
||||
"use client";
|
||||
|
||||
import Link from "next/link";
|
||||
import { useRouter } from "next/navigation";
|
||||
import { useState } from "react";
|
||||
|
||||
import { ApiError, api, errorText } from "@/lib/api";
|
||||
|
||||
/** Sets the password with the e-mailed link's token; logs in on success. */
|
||||
export function SetPasswordForm({ token, minLength }: { token: string; minLength: number }) {
|
||||
const router = useRouter();
|
||||
const [password, setPassword] = useState("");
|
||||
const [again, setAgain] = useState("");
|
||||
const [busy, setBusy] = useState(false);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [expired, setExpired] = useState(false);
|
||||
|
||||
async function submit(e: React.FormEvent) {
|
||||
e.preventDefault();
|
||||
setError(null);
|
||||
if (password.length < minLength) {
|
||||
setError(`A jelszó legalább ${minLength} karakter legyen.`);
|
||||
return;
|
||||
}
|
||||
if (password !== again) {
|
||||
setError("A két jelszó nem egyezik.");
|
||||
return;
|
||||
}
|
||||
setBusy(true);
|
||||
try {
|
||||
await api("/auth/set-password", { body: { token, password } });
|
||||
router.replace("/");
|
||||
router.refresh();
|
||||
} catch (err) {
|
||||
if (err instanceof ApiError && err.code === "invalid_token") setExpired(true);
|
||||
else setError(errorText(err));
|
||||
setBusy(false);
|
||||
}
|
||||
}
|
||||
|
||||
if (expired) {
|
||||
return (
|
||||
<div className="field" role="alert">
|
||||
<p className="msg msg-error" style={{ margin: 0 }}>
|
||||
Ez a link lejárt, vagy már felhasználták.
|
||||
</p>
|
||||
<p className="hint" style={{ margin: 0 }}>
|
||||
A <Link href="/belepes?mod=elfelejtett">belépésnél</Link> az „Elfelejtett jelszó?” linkkel kérhetsz újat.
|
||||
</p>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<form className="field" onSubmit={submit} style={{ gap: 14 }}>
|
||||
<div className="field">
|
||||
<label htmlFor="new-password">Új jelszó</label>
|
||||
<input
|
||||
id="new-password"
|
||||
className="inp"
|
||||
type="password"
|
||||
required
|
||||
minLength={minLength}
|
||||
autoComplete="new-password"
|
||||
value={password}
|
||||
onChange={(e) => setPassword(e.target.value)}
|
||||
/>
|
||||
<span className="hint">Legalább {minLength} karakter.</span>
|
||||
</div>
|
||||
<div className="field">
|
||||
<label htmlFor="new-password-again">Új jelszó még egyszer</label>
|
||||
<input
|
||||
id="new-password-again"
|
||||
className="inp"
|
||||
type="password"
|
||||
required
|
||||
autoComplete="new-password"
|
||||
value={again}
|
||||
onChange={(e) => setAgain(e.target.value)}
|
||||
/>
|
||||
</div>
|
||||
<button type="submit" className="btn btn-primary" disabled={busy}>
|
||||
{busy ? "Mentés…" : "Jelszó mentése és belépés"}
|
||||
</button>
|
||||
{error && (
|
||||
<p className="msg msg-error" role="alert" style={{ margin: 0 }}>
|
||||
{error}
|
||||
</p>
|
||||
)}
|
||||
</form>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user