feat: add editorial registration and authentication
This commit is contained in:
1 parent
7acaacb92e
commit
9b22752ef2
20 files changed
+1728
-20
No files matched your search
@@ -0,0 +1,256 @@
|
||||
"use client";
|
||||
|
||||
import { type FormEvent, useActionState, useState } from "react";
|
||||
|
||||
import { authClient } from "./client";
|
||||
|
||||
export type RegistrationFormState = {
|
||||
errors?: Partial<
|
||||
Record<"actsForBusiness" | "termsVersion" | "privacyAcknowledged", string>
|
||||
>;
|
||||
};
|
||||
|
||||
export type RegistrationFormAction = (
|
||||
state: RegistrationFormState,
|
||||
formData: FormData,
|
||||
) => Promise<RegistrationFormState>;
|
||||
|
||||
export type LegalUrls = {
|
||||
imprint: string;
|
||||
privacy: string;
|
||||
terms: string;
|
||||
};
|
||||
|
||||
const initialRegistrationState: RegistrationFormState = {};
|
||||
|
||||
export function RegistrationForm({
|
||||
action,
|
||||
legalUrls,
|
||||
}: {
|
||||
action: RegistrationFormAction;
|
||||
legalUrls: LegalUrls;
|
||||
}) {
|
||||
const [state, formAction, pending] = useActionState(action, initialRegistrationState);
|
||||
|
||||
return (
|
||||
<form action={formAction} className="account-form">
|
||||
<input name="termsVersion" type="hidden" value="2026-08" />
|
||||
<div className="form-grid">
|
||||
<label>
|
||||
Redaktion oder Unternehmen
|
||||
<input autoComplete="organization" name="companyName" required />
|
||||
</label>
|
||||
<label>
|
||||
Ansprechpartner:in
|
||||
<input autoComplete="name" name="contactName" required />
|
||||
</label>
|
||||
<label>
|
||||
E-Mail-Adresse
|
||||
<input autoComplete="email" name="email" required type="email" />
|
||||
</label>
|
||||
<label>
|
||||
Passwort
|
||||
<input autoComplete="new-password" minLength={8} name="password" required type="password" />
|
||||
</label>
|
||||
<label>
|
||||
Straße und Hausnummer
|
||||
<input autoComplete="address-line1" name="addressLine1" required />
|
||||
</label>
|
||||
<label>
|
||||
Adresszusatz
|
||||
<input autoComplete="address-line2" name="addressLine2" />
|
||||
</label>
|
||||
<label>
|
||||
Postleitzahl
|
||||
<input autoComplete="postal-code" name="postalCode" required />
|
||||
</label>
|
||||
<label>
|
||||
Ort
|
||||
<input autoComplete="address-level2" name="city" required />
|
||||
</label>
|
||||
<label>
|
||||
Land (ISO-Code)
|
||||
<input
|
||||
autoComplete="country"
|
||||
defaultValue="DE"
|
||||
maxLength={2}
|
||||
name="countryCode"
|
||||
required
|
||||
/>
|
||||
</label>
|
||||
</div>
|
||||
|
||||
<fieldset className="acknowledgements">
|
||||
<legend>Verbindliche Angaben</legend>
|
||||
<label className="checkbox-label">
|
||||
<input name="actsForBusiness" required type="checkbox" />
|
||||
Ich handle im Namen einer Redaktion oder eines Unternehmens.
|
||||
</label>
|
||||
<FieldError message={state.errors?.actsForBusiness} />
|
||||
<label className="checkbox-label">
|
||||
<input name="termsAccepted" required type="checkbox" />
|
||||
Ich akzeptiere die{" "}
|
||||
<a href={legalUrls.terms} rel="noreferrer" target="_blank">
|
||||
Nutzungs- und Lizenzbedingungen
|
||||
</a>
|
||||
.
|
||||
</label>
|
||||
<FieldError message={state.errors?.termsVersion} />
|
||||
<label className="checkbox-label">
|
||||
<input name="privacyAcknowledged" required type="checkbox" />
|
||||
Ich habe die{" "}
|
||||
<a href={legalUrls.privacy} rel="noreferrer" target="_blank">
|
||||
Datenschutzerklärung
|
||||
</a>{" "}
|
||||
zur Kenntnis genommen.
|
||||
</label>
|
||||
<FieldError message={state.errors?.privacyAcknowledged} />
|
||||
</fieldset>
|
||||
|
||||
<button className="primary-action" disabled={pending} type="submit">
|
||||
{pending ? "Registrierung wird gesendet …" : "Registrierung senden"}
|
||||
</button>
|
||||
<p className="form-note">
|
||||
Wir informieren Sie auf der nächsten Seite über den weiteren Ablauf.
|
||||
</p>
|
||||
<a className="legal-link" href={legalUrls.imprint} rel="noreferrer" target="_blank">
|
||||
Impressum
|
||||
</a>
|
||||
</form>
|
||||
);
|
||||
}
|
||||
|
||||
export function LoginForm() {
|
||||
const [error, setError] = useState<string>();
|
||||
const [pending, setPending] = useState(false);
|
||||
|
||||
async function submit(event: FormEvent<HTMLFormElement>) {
|
||||
event.preventDefault();
|
||||
setPending(true);
|
||||
setError(undefined);
|
||||
|
||||
const formData = new FormData(event.currentTarget);
|
||||
const result = await authClient.signIn.email({
|
||||
email: readText(formData, "email").trim().toLowerCase(),
|
||||
password: readText(formData, "password"),
|
||||
callbackURL: "/",
|
||||
});
|
||||
|
||||
if (result.error) {
|
||||
setError("Die Anmeldung war nicht erfolgreich. Bitte prüfen Sie Ihre Angaben.");
|
||||
setPending(false);
|
||||
return;
|
||||
}
|
||||
|
||||
window.location.assign(result.data?.url ?? "/");
|
||||
}
|
||||
|
||||
return (
|
||||
<form className="account-form" onSubmit={submit}>
|
||||
<label>
|
||||
E-Mail-Adresse
|
||||
<input autoComplete="email" name="email" required type="email" />
|
||||
</label>
|
||||
<label>
|
||||
Passwort
|
||||
<input autoComplete="current-password" name="password" required type="password" />
|
||||
</label>
|
||||
<FieldError message={error} />
|
||||
<button className="primary-action" disabled={pending} type="submit">
|
||||
{pending ? "Anmeldung wird geprüft …" : "Anmelden"}
|
||||
</button>
|
||||
</form>
|
||||
);
|
||||
}
|
||||
|
||||
export function PasswordResetRequestForm() {
|
||||
const [submitted, setSubmitted] = useState(false);
|
||||
const [pending, setPending] = useState(false);
|
||||
|
||||
async function submit(event: FormEvent<HTMLFormElement>) {
|
||||
event.preventDefault();
|
||||
setPending(true);
|
||||
const formData = new FormData(event.currentTarget);
|
||||
|
||||
try {
|
||||
await authClient.requestPasswordReset({
|
||||
email: readText(formData, "email").trim().toLowerCase(),
|
||||
redirectTo: `${window.location.origin}/passwort-zuruecksetzen`,
|
||||
});
|
||||
} finally {
|
||||
setSubmitted(true);
|
||||
setPending(false);
|
||||
}
|
||||
}
|
||||
|
||||
if (submitted) {
|
||||
return (
|
||||
<p className="form-note" role="status">
|
||||
Falls ein Zugang zu dieser Adresse gehört, erhalten Sie in Kürze weitere Hinweise.
|
||||
</p>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<form className="account-form" onSubmit={submit}>
|
||||
<label>
|
||||
E-Mail-Adresse
|
||||
<input autoComplete="email" name="email" required type="email" />
|
||||
</label>
|
||||
<button className="primary-action" disabled={pending} type="submit">
|
||||
{pending ? "Anfrage wird gesendet …" : "Link anfordern"}
|
||||
</button>
|
||||
</form>
|
||||
);
|
||||
}
|
||||
|
||||
export function PasswordResetForm({ token }: { token?: string }) {
|
||||
const [message, setMessage] = useState<string>();
|
||||
const [pending, setPending] = useState(false);
|
||||
|
||||
async function submit(event: FormEvent<HTMLFormElement>) {
|
||||
event.preventDefault();
|
||||
setPending(true);
|
||||
const formData = new FormData(event.currentTarget);
|
||||
|
||||
try {
|
||||
await authClient.resetPassword({ newPassword: readText(formData, "password"), token });
|
||||
} finally {
|
||||
setMessage("Wenn der Link gültig war, wurde Ihr Passwort aktualisiert.");
|
||||
setPending(false);
|
||||
}
|
||||
}
|
||||
|
||||
if (message) {
|
||||
return (
|
||||
<p className="form-note" role="status">
|
||||
{message}
|
||||
</p>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<form className="account-form" onSubmit={submit}>
|
||||
<label>
|
||||
Neues Passwort
|
||||
<input autoComplete="new-password" minLength={8} name="password" required type="password" />
|
||||
</label>
|
||||
<button className="primary-action" disabled={pending} type="submit">
|
||||
{pending ? "Passwort wird aktualisiert …" : "Passwort aktualisieren"}
|
||||
</button>
|
||||
</form>
|
||||
);
|
||||
}
|
||||
|
||||
function FieldError({ message }: { message?: string }) {
|
||||
return message ? (
|
||||
<p className="field-error" role="alert">
|
||||
{message}
|
||||
</p>
|
||||
) : null;
|
||||
}
|
||||
|
||||
function readText(formData: FormData, name: string): string {
|
||||
const value = formData.get(name);
|
||||
return typeof value === "string" ? value : "";
|
||||
}
|
||||
Reference in new issue
Block a user