Zugang
+Anmelden
+Melden Sie sich mit der E-Mail-Adresse Ihrer Redaktion an.
+Zugang
+Melden Sie sich mit der E-Mail-Adresse Ihrer Redaktion an.
+Zugang
+Fordern Sie einen Link zum Zurücksetzen Ihres Passworts an.
+Zugang
+Wählen Sie ein neues Passwort für den Zugang Ihrer Redaktion.
+Registrierung
++ Bitte folgen Sie den weiteren Hinweisen, die wir Ihnen gegebenenfalls zusenden. Danach + prüfen wir Ihre Registrierung. +
+ + Zur Anmeldung + +B2B-Zugang
++ Das Redaktionsportal steht ausschließlich Unternehmen und Redaktionen zur Verfügung. +
++ Falls ein Zugang zu dieser Adresse gehört, erhalten Sie in Kürze weitere Hinweise. +
+ ); + } + + return ( + + ); +} + +export function PasswordResetForm({ token }: { token?: string }) { + const [message, setMessage] = useState+ {message} +
+ ); + } + + return ( + + ); +} + +function FieldError({ message }: { message?: string }) { + return message ? ( ++ {message} +
+ ) : null; +} + +function readText(formData: FormData, name: string): string { + const value = formData.get(name); + return typeof value === "string" ? value : ""; +} diff --git a/src/modules/auth/registration.test.ts b/src/modules/auth/registration.test.ts new file mode 100644 index 0000000..1e9b398 --- /dev/null +++ b/src/modules/auth/registration.test.ts @@ -0,0 +1,169 @@ +import { describe, expect, test, vi } from "vitest"; + +import { + RegistrationValidationError, + createRegistrationService, + type RegistrationInput, +} from "./registration"; +import { createAdminBootstrapService } from "./admin-bootstrap"; + +const validRegistration: RegistrationInput = { + companyName: "Neue Redaktion GmbH", + contactName: "Mara Muster", + email: "Mara.Muster@Example.com", + password: "sicheres-passwort-123", + billingAddress: { + addressLine1: "Presseweg 1", + addressLine2: "Haus B", + postalCode: "10115", + city: "Berlin", + countryCode: "DE", + }, + actsForBusiness: true, + termsVersion: "2026-08", + privacyAcknowledged: true, +}; + +function createService() { + const auth = { + signUpEmail: vi.fn().mockResolvedValue({ user: { id: "user-1" } }), + sendVerificationEmail: vi.fn().mockResolvedValue(undefined), + }; + const repo = { + createEditorial: vi.fn().mockResolvedValue(undefined), + markPendingApproval: vi.fn().mockResolvedValue(true), + }; + const audit = { + recordVerificationInconsistency: vi.fn().mockResolvedValue(undefined), + }; + const now = new Date("2026-08-09T10:00:00.000Z"); + + return { + auth, + repo, + audit, + service: createRegistrationService({ auth, repo, audit, now: () => now }), + }; +} + +describe("editorial registration", () => { + test("creates a pending editorial and sends verification mail", async () => { + const { auth, repo, service } = createService(); + + await expect(service.register(validRegistration)).resolves.toEqual({ status: "pending_email" }); + + expect(auth.signUpEmail).toHaveBeenCalledWith({ + email: "mara.muster@example.com", + name: "Mara Muster", + password: "sicheres-passwort-123", + }); + expect(repo.createEditorial).toHaveBeenCalledWith({ + userId: "user-1", + name: "Neue Redaktion GmbH", + contactName: "Mara Muster", + loginEmail: "mara.muster@example.com", + status: "pending_email", + acceptedTermsVersion: "2026-08", + acceptedTermsAt: new Date("2026-08-09T10:00:00.000Z"), + privacyAcknowledgedAt: new Date("2026-08-09T10:00:00.000Z"), + billingAddress: { + companyName: "Neue Redaktion GmbH", + contactName: "Mara Muster", + addressLine1: "Presseweg 1", + addressLine2: "Haus B", + postalCode: "10115", + city: "Berlin", + countryCode: "DE", + }, + }); + expect(auth.sendVerificationEmail).toHaveBeenCalledWith({ + email: "mara.muster@example.com", + }); + }); + + test("rejects a registration that is not made for a business", async () => { + const { auth, repo, service } = createService(); + + await expect( + service.register({ ...validRegistration, actsForBusiness: false }), + ).rejects.toEqual( + new RegistrationValidationError( + "actsForBusiness", + "Bitte bestätigen Sie, dass Sie im Namen eines Unternehmens oder einer Redaktion handeln.", + ), + ); + + expect(auth.signUpEmail).not.toHaveBeenCalled(); + expect(repo.createEditorial).not.toHaveBeenCalled(); + }); + + test("rejects missing terms and privacy acknowledgements with stable German field errors", async () => { + const { service } = createService(); + + await expect( + service.register({ ...validRegistration, termsVersion: "" }), + ).rejects.toEqual( + new RegistrationValidationError( + "termsVersion", + "Bitte akzeptieren Sie die Nutzungs- und Lizenzbedingungen.", + ), + ); + await expect( + service.register({ ...validRegistration, privacyAcknowledged: false }), + ).rejects.toEqual( + new RegistrationValidationError( + "privacyAcknowledged", + "Bitte bestätigen Sie die Kenntnisnahme der Datenschutzerklärung.", + ), + ); + }); + + test("moves a verified account from pending email to pending approval", async () => { + const { audit, repo, service } = createService(); + + await service.onEmailVerified("user-1"); + + expect(repo.markPendingApproval).toHaveBeenCalledWith("user-1"); + expect(audit.recordVerificationInconsistency).not.toHaveBeenCalled(); + }); + + test("audits a verified user without an editorial account without raw request data", async () => { + const { audit, repo, service } = createService(); + repo.markPendingApproval.mockResolvedValue(false); + + await service.onEmailVerified("user-without-editorial"); + + expect(audit.recordVerificationInconsistency).toHaveBeenCalledWith({ + userId: "user-without-editorial", + }); + }); +}); + +test("bootstraps the first admin only once", async () => { + const auth = { + signUpEmail: vi.fn().mockResolvedValue({ user: { id: "admin-1" } }), + sendVerificationEmail: vi.fn().mockResolvedValue(undefined), + }; + const repo = { + countAdmins: vi.fn().mockResolvedValue(0), + findUserByEmail: vi.fn().mockResolvedValue(undefined), + promoteUserToAdmin: vi.fn().mockResolvedValue(undefined), + }; + const service = createAdminBootstrapService({ auth, repo }); + + await expect( + service.bootstrap({ + email: "ADMIN@EXAMPLE.COM", + name: "Portal Admin", + password: "sicheres-passwort-123", + }), + ).resolves.toEqual({ status: "created" }); + + expect(auth.signUpEmail).toHaveBeenCalledWith({ + email: "admin@example.com", + name: "Portal Admin", + password: "sicheres-passwort-123", + }); + expect(repo.promoteUserToAdmin).toHaveBeenCalledWith("admin-1"); + expect(auth.sendVerificationEmail).toHaveBeenCalledWith({ email: "admin@example.com" }); +}); diff --git a/src/modules/auth/registration.ts b/src/modules/auth/registration.ts new file mode 100644 index 0000000..f1eb232 --- /dev/null +++ b/src/modules/auth/registration.ts @@ -0,0 +1,276 @@ +import "server-only"; + +import { and, eq } from "drizzle-orm"; +import type { NodePgDatabase } from "drizzle-orm/node-postgres"; + +import { auditEvents, billingAddressVersions, editorialAccounts, users } from "@/db/schema"; +import type * as schema from "@/db/schema"; + +export type BillingAddressInput = { + addressLine1: string; + addressLine2?: string; + postalCode: string; + city: string; + countryCode: string; +}; + +export type RegistrationInput = { + companyName: string; + contactName: string; + email: string; + password: string; + billingAddress: BillingAddressInput; + actsForBusiness: boolean; + termsVersion: string; + privacyAcknowledged: boolean; +}; + +export type PendingEditorial = { + userId: string; + name: string; + contactName: string; + loginEmail: string; + status: "pending_email"; + acceptedTermsVersion: string; + acceptedTermsAt: Date; + privacyAcknowledgedAt: Date; + billingAddress: BillingAddressInput & { companyName: string; contactName: string }; +}; + +export type RegistrationAuth = { + signUpEmail(input: { email: string; name: string; password: string }): Promise<{ user: { id: string } }>; + sendVerificationEmail(input: { email: string }): Promise