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>
|
||||
);
|
||||
}
|
||||
@@ -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>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user