auth: e-mail + password login with e-mailed password links
CI / test (push) Failing after 1m39s
CI / build (push) Skipped
CI / deploy (push) Skipped

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:
feco
2026-11-21 18:24:48 +00:00
co-authored by Claude Opus 5.5
parent a63f8f9928
commit 7d6a6c3d92
11 changed files with 651 additions and 192 deletions
@@ -0,0 +1,203 @@
"use client";
import { useRouter } from "next/navigation";
import { useState } from "react";
import { Waitlist } from "@/components/Waitlist";
import { ApiError, api, errorText } from "@/lib/api";
type Mode = "login" | "register" | "forgot";
function loginError(e: unknown): string {
if (e instanceof ApiError) {
if (e.code === "invalid_credentials") return "Hibás e-mail-cím vagy jelszó.";
if (e.code === "too_many_requests") return "Túl sok sikertelen próbálkozás. Próbáld újra negyedóra múlva.";
}
return errorText(e);
}
/**
* E-mail and password: login, sign-up and forgotten password. Sign-up asks
* no password: it is set through the link sent to the address, so nobody can
* set a password for someone else's address.
*/
export function PasswordLogin({
target,
inviteToken = null,
initialMode = "login",
}: {
target: string;
inviteToken?: string | null;
initialMode?: Mode;
}) {
const router = useRouter();
const [mode, setMode] = useState<Mode>(initialMode);
const [email, setEmail] = useState("");
const [password, setPassword] = useState("");
const [name, setName] = useState("");
const [busy, setBusy] = useState(false);
const [error, setError] = useState<string | null>(null);
const [sent, setSent] = useState<Mode | null>(null);
const [uninvited, setUninvited] = useState(false);
function switchTo(next: Mode) {
setMode(next);
setError(null);
setSent(null);
setUninvited(false);
}
async function submit(e: React.FormEvent) {
e.preventDefault();
setBusy(true);
setError(null);
setUninvited(false);
try {
if (mode === "login") {
await api("/auth/login", { body: { email, password } });
router.replace(target);
router.refresh();
return;
}
if (mode === "register") {
let referral: string | null = null;
try {
referral = localStorage.getItem("sf_ref");
} catch {
// storage blocked
}
await api("/auth/register", {
body: { email, display_name: name.trim() || null, locale: "hu", invite_token: inviteToken, referral },
});
} else {
await api("/auth/password-reset", { body: { email } });
}
setSent(mode);
} catch (err) {
if (err instanceof ApiError && err.code === "not_invited") setUninvited(true);
else setError(mode === "login" ? loginError(err) : errorText(err));
} finally {
setBusy(false);
}
}
if (sent) {
return (
<div className="field pw-sent" role="status">
<p className="msg msg-ok" style={{ margin: 0 }}>
{sent === "register"
? "Küldtünk egy levelet a(z) "
: "Ha ehhez a címhez tartozik fiók, küldtünk egy levelet a(z) "}
<strong>{email}</strong> címre. A benne lévő linkkel adhatod meg a jelszavad
{sent === "register" ? ", és utána már be is vagy lépve." : "."}
</p>
<p className="hint" style={{ margin: 0 }}>
Nem jött meg pár percen belül? Nézd meg a spam mappát is, vagy{" "}
<button type="button" className="link-btn" onClick={() => switchTo(sent)}>
próbáld újra
</button>
.
</p>
</div>
);
}
return (
<form className="field pw-form" onSubmit={submit} style={{ gap: 14 }}>
{mode === "register" && (
<div className="field">
<label htmlFor="pw-name">Név (nem kötelező)</label>
<input
id="pw-name"
className="inp"
autoComplete="name"
maxLength={100}
value={name}
onChange={(e) => setName(e.target.value)}
/>
</div>
)}
<div className="field">
<label htmlFor="pw-email">E-mail-cím</label>
<input
id="pw-email"
className="inp"
type="email"
required
autoComplete={mode === "login" ? "username" : "email"}
value={email}
onChange={(e) => setEmail(e.target.value)}
placeholder="nev@pelda.hu"
/>
</div>
{mode === "login" && (
<div className="field">
<div className="pw-label-row">
<label htmlFor="pw-password">Jelszó</label>
<button type="button" className="link-btn" onClick={() => switchTo("forgot")}>
Elfelejtett jelszó?
</button>
</div>
<input
id="pw-password"
className="inp"
type="password"
required
autoComplete="current-password"
value={password}
onChange={(e) => setPassword(e.target.value)}
/>
</div>
)}
{mode === "register" && (
<p className="hint" style={{ margin: 0 }}>
A jelszavadat a levélben kapott linken adod meg. Így biztos, hogy a cím a tiéd.
</p>
)}
{mode === "forgot" && (
<p className="hint" style={{ margin: 0 }}>
Küldünk egy linket, amellyel új jelszót adhatsz meg. Ha eddig Google- vagy Apple-fiókkal léptél be, így
jelszót is beállíthatsz.
</p>
)}
<button type="submit" className="btn btn-primary" disabled={busy}>
{busy
? "Küldés…"
: mode === "login"
? "Belépés"
: mode === "register"
? "Regisztráció"
: "Link küldése"}
</button>
{error && (
<p className="msg msg-error" role="alert" style={{ margin: 0 }}>
{error}
</p>
)}
{uninvited && (
<>
<p className="msg msg-error" role="alert" style={{ margin: 0 }}>
{inviteToken
? "Ez a meghívó már nem érvényes, és ehhez a címhez sem tartozik másik."
: "A Sorsfonalak jelenleg meghívásos, és ehhez az e-mail-címhez nem tartozik meghívó. Ha kaptál meghívót, nyisd meg a levélben kapott linket."}
</p>
<Waitlist email={email} />
</>
)}
<p className="hint pw-switch" style={{ margin: 0 }}>
{mode === "login" ? (
<>
Még nincs fiókod?{" "}
<button type="button" className="link-btn" onClick={() => switchTo("register")}>
Regisztráció e-mail-címmel
</button>
</>
) : (
<button type="button" className="link-btn" onClick={() => switchTo("login")}>
Vissza a belépéshez
</button>
)}
</p>
</form>
);
}
+37 -9
View File
@@ -10,6 +10,7 @@ import type { InvitationLanding, PublicConfig } from "@/lib/types";
import { AppleLogin } from "./AppleLogin";
import { GoogleLogin } from "./GoogleLogin";
import { PasswordLogin } from "./PasswordLogin";
export const metadata: Metadata = { title: "Belépés" };
@@ -48,7 +49,15 @@ const CLOSED: Record<string, string> = {
};
/** The invitation the visitor came with: who sent it and what they wrote. */
function InvitationCard({ invitation, apple }: { invitation: InvitationLanding | null; apple: boolean }) {
function InvitationCard({
invitation,
apple,
password,
}: {
invitation: InvitationLanding | null;
apple: boolean;
password: boolean;
}) {
if (!invitation) {
return (
<p className="msg msg-error" role="alert">
@@ -71,7 +80,9 @@ function InvitationCard({ invitation, apple }: { invitation: InvitationLanding |
</p>
{invitation.message && <blockquote className="invite-quote">{invitation.message}</blockquote>}
<p className="hint" style={{ margin: 0 }}>
Bármelyik {apple ? "Google- vagy Apple-fiókoddal" : "Google-fiókoddal"} elfogadhatod. Érvényes: {formatDate(invitation.expires_at).replace(/\.$/, "")}-ig.
Bármelyik {apple ? "Google- vagy Apple-fiókoddal" : "Google-fiókoddal"}
{password ? ", vagy e-mail-címmel regisztrálva" : ""} elfogadhatod. Érvényes:{" "}
{formatDate(invitation.expires_at).replace(/\.$/, "")}-ig.
</p>
</div>
);
@@ -91,6 +102,9 @@ export default async function LoginPage({ searchParams }: { searchParams: Promis
const appleId = config?.apple_login?.enabled ? config.apple_login.client_id : null;
const pendingInvite = invitation?.status === "pending" ? inviteToken : null;
const inviteOnly = config?.signup_mode === "invite";
const password = config?.password_login === true;
const mod = Array.isArray(params.mod) ? params.mod[0] : params.mod;
const initialMode = mod === "elfelejtett" ? "forgot" : mod === "regisztracio" || rawToken ? "register" : "login";
return (
<div className="auth">
@@ -114,16 +128,28 @@ export default async function LoginPage({ searchParams }: { searchParams: Promis
<h1 style={{ fontSize: 44 }}>{rawToken ? "Meghívó" : "Belépés"}</h1>
<p className="muted">{rawToken ? "Lépj be, és már kezdheted is." : "Üdv újra! Lépj be a fiókodba."}</p>
</div>
{rawToken && <InvitationCard invitation={inviteToken ? invitation : null} apple={appleId !== null} />}
{rawToken && (
<InvitationCard invitation={inviteToken ? invitation : null} apple={appleId !== null} password={password} />
)}
{config === null ? (
<p className="msg msg-error" role="alert">
A szerver most nem érhető el. Próbáld újra egy kicsit később.
</p>
) : clientId ? (
<div className="field" style={{ gap: 12 }}>
<GoogleLogin clientId={clientId} target={target} inviteToken={pendingInvite} />
{appleId && <AppleLogin clientId={appleId} target={target} inviteToken={pendingInvite} />}
</div>
) : clientId || password ? (
<>
{clientId && (
<div className="field" style={{ gap: 12 }}>
<GoogleLogin clientId={clientId} target={target} inviteToken={pendingInvite} />
{appleId && <AppleLogin clientId={appleId} target={target} inviteToken={pendingInvite} />}
</div>
)}
{clientId && password && (
<div className="auth-or" role="separator">
<span>vagy</span>
</div>
)}
{password && <PasswordLogin target={target} inviteToken={pendingInvite} initialMode={initialMode} />}
</>
) : (
<p className="msg msg-error" role="alert">
A Google-belépés még nincs beállítva (a backend <code>GOOGLE_CLIENT_IDS</code> változója üres).
@@ -134,7 +160,9 @@ export default async function LoginPage({ searchParams }: { searchParams: Promis
? "A fiókod az első belépéskor jön létre."
: inviteOnly
? "Az oldal jelenleg meghívásos: új fiók csak meghívóval jön létre."
: "Még nincs fiókod? Az első belépéskor létrejön."}
: password
? "Google- vagy Apple-fiókkal az első belépéskor létrejön a fiókod."
: "Még nincs fiókod? Az első belépéskor létrejön."}
</p>
<p className="hint">
A belépéssel elfogadod az <Link href="/aszf">általános szerződési feltételeket</Link>, és hogy a születési
+12
View File
@@ -198,6 +198,18 @@ td.num { font-variant-numeric: tabular-nums; white-space: nowrap; }
.tagline { font-family: var(--font-display); font-size: 38px; font-weight: 600; line-height: 1.1; }
.auth-foot { font-size: 15px; color: var(--muted); text-align: center; }
/* E-mail and password next to the social buttons */
.auth-or { display: flex; align-items: center; gap: 14px; color: var(--muted); font-size: 13px; margin: -6px 0; }
.auth-or::before, .auth-or::after { content: ""; flex: 1; border-top: 1px solid var(--line); }
.pw-label-row { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; }
.pw-switch { text-align: center; }
.link-btn {
background: none; border: 0; padding: 0; font: inherit; color: var(--gold); cursor: pointer;
text-decoration: underline; text-underline-offset: 2px;
}
.link-btn:hover { color: var(--gold-hover); }
.pw-label-row .link-btn { font-size: 13px; text-decoration: none; }
/* Social login buttons; Google's own button lies invisibly on top (.gsi-overlay). */
.social-login { position: relative; }
.btn-social {
@@ -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>
);
}
+53
View File
@@ -0,0 +1,53 @@
import type { Metadata } from "next";
import Link from "next/link";
import { Logo } from "@/components/SiteHeader";
import { BACKEND_URL } from "@/lib/server";
import type { PublicConfig } from "@/lib/types";
import { SetPasswordForm } from "./SetPasswordForm";
export const metadata: Metadata = { title: "Jelszó megadása", robots: { index: false } };
async function minLength(): Promise<number> {
try {
const res = await fetch(`${BACKEND_URL}/v1/config`, { cache: "no-store" });
if (res.ok) return ((await res.json()) as PublicConfig).password_min_length;
} catch {
// backend down: the form still works, the backend checks the length
}
return 10;
}
/** The page of the e-mailed password link (sign-up and forgotten password). */
export default async function SetPasswordPage({
searchParams,
}: {
searchParams: Promise<Record<string, string | string[]>>;
}) {
const params = await searchParams;
const raw = Array.isArray(params.token) ? params.token[0] : params.token;
const token = raw && /^[A-Za-z0-9_-]{8,200}$/.test(raw) ? raw : null;
return (
<div className="auth">
<main className="auth-main">
<div className="auth-box">
<Logo />
<div className="page-head">
<h1 style={{ fontSize: 40 }}>Jelszó megadása</h1>
<p className="muted">Add meg a jelszavad, amellyel ezentúl belépsz.</p>
</div>
{token ? (
<SetPasswordForm token={token} minLength={await minLength()} />
) : (
<p className="msg msg-error" role="alert">
Hiányos a link. Nyisd meg a teljes linket a levélből, vagy kérj újat a{" "}
<Link href="/belepes?mod=elfelejtett">belépésnél</Link>.
</p>
)}
</div>
</main>
</div>
);
}