fix: wire editorial approval, harden registration, set production auth URL
- Wire admin approval UI to editorialService.approveEditorial so editorial accounts can move pending_approval -> active and log in. The approve button was permanently disabled and the service unreachable, so editorial users could never sign in. - Surface non-validation errors (e.g. SMTP failures) on the registration form instead of silently redirecting to the confirmation page. - Set BETTER_AUTH_URL default to the production domain in .env.example and compose.yaml so verification/reset links and the login origin check resolve against the live origin. - Add scripts/check-smtp.ts to verify SMTP connectivity against the configured transport. Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
1 parent
907beb6e42
commit
268ea7544e
8 files changed
+225
-9
No files matched your search
+2
-2
@@ -1,9 +1,9 @@
|
|||||||
# Portal database (separate from Immich)
|
# Portal database (separate from Immich)
|
||||||
DATABASE_URL=postgres://portal:change-me@postgres:5432/portal
|
DATABASE_URL=postgres://portal:change-me@postgres:5432/portal
|
||||||
|
|
||||||
# Better Auth
|
# Better Auth — production public URL. For local dev, set http://localhost:3000 in .env
|
||||||
BETTER_AUTH_SECRET=replace-with-a-long-random-secret
|
BETTER_AUTH_SECRET=replace-with-a-long-random-secret
|
||||||
BETTER_AUTH_URL=http://localhost:3000
|
BETTER_AUTH_URL=https://redaktionen.leon-tonhaeuser.de
|
||||||
|
|
||||||
# Internal Immich API access; never expose these values to browsers
|
# Internal Immich API access; never expose these values to browsers
|
||||||
IMMICH_BASE_URL=http://immich-server:2283
|
IMMICH_BASE_URL=http://immich-server:2283
|
||||||
|
|||||||
+1
-1
@@ -28,7 +28,7 @@ services:
|
|||||||
environment:
|
environment:
|
||||||
DATABASE_URL: ${DATABASE_URL:-postgres://portal:change-me@portal-db:5432/portal}
|
DATABASE_URL: ${DATABASE_URL:-postgres://portal:change-me@portal-db:5432/portal}
|
||||||
BETTER_AUTH_SECRET: ${BETTER_AUTH_SECRET}
|
BETTER_AUTH_SECRET: ${BETTER_AUTH_SECRET}
|
||||||
BETTER_AUTH_URL: ${BETTER_AUTH_URL:-http://localhost:3000}
|
BETTER_AUTH_URL: ${BETTER_AUTH_URL:-https://redaktionen.leon-tonhaeuser.de}
|
||||||
IMMICH_BASE_URL: ${IMMICH_BASE_URL}
|
IMMICH_BASE_URL: ${IMMICH_BASE_URL}
|
||||||
IMMICH_API_KEY: ${IMMICH_API_KEY}
|
IMMICH_API_KEY: ${IMMICH_API_KEY}
|
||||||
SMTP_HOST: ${SMTP_HOST}
|
SMTP_HOST: ${SMTP_HOST}
|
||||||
|
|||||||
@@ -0,0 +1,40 @@
|
|||||||
|
import nodemailer from "nodemailer";
|
||||||
|
|
||||||
|
import { env } from "../src/config/env";
|
||||||
|
|
||||||
|
// Verifies the SMTP connection configured via env, and optionally sends a test mail.
|
||||||
|
// Run with production env: set -a; . ./.env; set +a; pnpm tsx scripts/check-smtp.ts [recipient]
|
||||||
|
async function main() {
|
||||||
|
const recipient = process.argv[2];
|
||||||
|
|
||||||
|
const transport = nodemailer.createTransport({
|
||||||
|
host: env.SMTP_HOST,
|
||||||
|
port: env.SMTP_PORT,
|
||||||
|
secure: env.SMTP_PORT === 465,
|
||||||
|
auth: { user: env.SMTP_USER, pass: env.SMTP_PASSWORD },
|
||||||
|
});
|
||||||
|
|
||||||
|
console.log(
|
||||||
|
`Verifying SMTP ${env.SMTP_HOST}:${env.SMTP_PORT} (secure=${env.SMTP_PORT === 465}) as ${env.SMTP_USER}, from=${env.SMTP_FROM} …`,
|
||||||
|
);
|
||||||
|
|
||||||
|
await transport.verify();
|
||||||
|
console.log("OK: SMTP connection verified.");
|
||||||
|
|
||||||
|
if (recipient) {
|
||||||
|
const info = await transport.sendMail({
|
||||||
|
from: env.SMTP_FROM,
|
||||||
|
to: recipient,
|
||||||
|
subject: "Redaktionsportal — SMTP-Test",
|
||||||
|
text: "Test-E-Mail vom Redaktionsportal. Wenn diese ankommt, ist SMTP korrekt verbunden.",
|
||||||
|
});
|
||||||
|
console.log(`OK: test mail sent to ${recipient} (messageId=${info.messageId}).`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
main()
|
||||||
|
.then(() => process.exit(0))
|
||||||
|
.catch((error) => {
|
||||||
|
console.error("FAILED: SMTP check failed:", error?.message ?? error);
|
||||||
|
process.exit(1);
|
||||||
|
});
|
||||||
@@ -67,6 +67,11 @@ async function registerAction(
|
|||||||
if (error instanceof RegistrationValidationError) {
|
if (error instanceof RegistrationValidationError) {
|
||||||
return { errors: { [error.field]: error.message } };
|
return { errors: { [error.field]: error.message } };
|
||||||
}
|
}
|
||||||
|
return {
|
||||||
|
errors: {
|
||||||
|
form: "Die Registrierung konnte nicht abgeschlossen werden. Bitte versuchen Sie es später erneut.",
|
||||||
|
},
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
redirect("/registrieren/bestaetigt");
|
redirect("/registrieren/bestaetigt");
|
||||||
|
|||||||
@@ -0,0 +1,82 @@
|
|||||||
|
import { beforeEach, expect, test, vi } from "vitest";
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
vi.clearAllMocks();
|
||||||
|
});
|
||||||
|
|
||||||
|
const mocks = vi.hoisted(() => ({
|
||||||
|
approveEditorial: vi.fn(),
|
||||||
|
headers: vi.fn(),
|
||||||
|
requireAdmin: vi.fn(),
|
||||||
|
revalidatePath: vi.fn(),
|
||||||
|
}));
|
||||||
|
|
||||||
|
vi.mock("next/headers", () => ({ headers: mocks.headers }));
|
||||||
|
vi.mock("next/cache", () => ({ revalidatePath: mocks.revalidatePath }));
|
||||||
|
vi.mock("@/modules/auth/principal", () => ({ requireAdmin: mocks.requireAdmin }));
|
||||||
|
vi.mock("@/modules/editorials/service", () => ({
|
||||||
|
editorialService: { approveEditorial: mocks.approveEditorial },
|
||||||
|
EditorialServiceError: class EditorialServiceError extends Error {
|
||||||
|
constructor(readonly code: string) {
|
||||||
|
super(code);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}));
|
||||||
|
|
||||||
|
import { approveEditorialAction } from "./actions";
|
||||||
|
|
||||||
|
test("the approve action rejects unauthenticated callers before touching the service", async () => {
|
||||||
|
mocks.headers.mockResolvedValue(new Headers());
|
||||||
|
mocks.requireAdmin.mockRejectedValue(new Error("ADMIN_REQUIRED"));
|
||||||
|
const formData = new FormData();
|
||||||
|
formData.set("priceEuros", "10.00");
|
||||||
|
|
||||||
|
await expect(
|
||||||
|
approveEditorialAction("00000000-0000-4000-8000-000000000001", formData),
|
||||||
|
).rejects.toThrow("ADMIN_REQUIRED");
|
||||||
|
expect(mocks.approveEditorial).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
test("the approve action forwards the authenticated principal and cents price to the service", async () => {
|
||||||
|
mocks.headers.mockResolvedValue(new Headers());
|
||||||
|
mocks.requireAdmin.mockResolvedValue({ userId: "admin-1" });
|
||||||
|
mocks.approveEditorial.mockResolvedValue(undefined);
|
||||||
|
const formData = new FormData();
|
||||||
|
formData.set("priceEuros", "12,50");
|
||||||
|
|
||||||
|
await approveEditorialAction("00000000-0000-4000-8000-000000000001", formData);
|
||||||
|
|
||||||
|
expect(mocks.approveEditorial).toHaveBeenCalledWith({
|
||||||
|
editorialId: "00000000-0000-4000-8000-000000000001",
|
||||||
|
priceCents: 1250,
|
||||||
|
adminUserId: "admin-1",
|
||||||
|
});
|
||||||
|
expect(mocks.revalidatePath).toHaveBeenCalledWith(
|
||||||
|
"/admin/redaktionen/00000000-0000-4000-8000-000000000001",
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("the approve action rejects a non-positive price before calling the service", async () => {
|
||||||
|
mocks.headers.mockResolvedValue(new Headers());
|
||||||
|
mocks.requireAdmin.mockResolvedValue({ userId: "admin-1" });
|
||||||
|
const formData = new FormData();
|
||||||
|
formData.set("priceEuros", "0");
|
||||||
|
|
||||||
|
await expect(
|
||||||
|
approveEditorialAction("00000000-0000-4000-8000-000000000001", formData),
|
||||||
|
).rejects.toThrow("Bitte geben Sie einen gültigen Nettopreis an.");
|
||||||
|
expect(mocks.approveEditorial).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
test("the approve action translates a service error into a German message", async () => {
|
||||||
|
mocks.headers.mockResolvedValue(new Headers());
|
||||||
|
mocks.requireAdmin.mockResolvedValue({ userId: "admin-1" });
|
||||||
|
const { EditorialServiceError } = await import("@/modules/editorials/service");
|
||||||
|
mocks.approveEditorial.mockRejectedValue(new EditorialServiceError("ALBUM_GRANT_REQUIRED"));
|
||||||
|
const formData = new FormData();
|
||||||
|
formData.set("priceEuros", "10.00");
|
||||||
|
|
||||||
|
await expect(
|
||||||
|
approveEditorialAction("00000000-0000-4000-8000-000000000001", formData),
|
||||||
|
).rejects.toThrow("Bitte weisen Sie zuerst mindestens ein Album zu.");
|
||||||
|
});
|
||||||
@@ -0,0 +1,62 @@
|
|||||||
|
"use server";
|
||||||
|
|
||||||
|
import { revalidatePath } from "next/cache";
|
||||||
|
import { headers } from "next/headers";
|
||||||
|
|
||||||
|
import { requireAdmin } from "@/modules/auth/principal";
|
||||||
|
import { editorialService, EditorialServiceError } from "@/modules/editorials/service";
|
||||||
|
|
||||||
|
export async function approveEditorialAction(editorialId: string, formData: FormData): Promise<void> {
|
||||||
|
const principal = await requireAdmin(new Headers(await headers()));
|
||||||
|
const safeEditorialId = requiredId(editorialId);
|
||||||
|
const priceCents = parsePriceCents(formData.get("priceEuros"));
|
||||||
|
|
||||||
|
try {
|
||||||
|
await editorialService.approveEditorial({
|
||||||
|
editorialId: safeEditorialId,
|
||||||
|
priceCents,
|
||||||
|
adminUserId: principal.userId,
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
throw new Error(approveErrorMessage(error));
|
||||||
|
}
|
||||||
|
|
||||||
|
revalidatePath(`/admin/redaktionen/${safeEditorialId}`);
|
||||||
|
revalidatePath(`/admin/redaktionen`);
|
||||||
|
}
|
||||||
|
|
||||||
|
function parsePriceCents(value: unknown): number {
|
||||||
|
const euros =
|
||||||
|
typeof value === "string" ? Number.parseFloat(value.replace(",", ".")) : Number.NaN;
|
||||||
|
if (!Number.isFinite(euros) || euros <= 0) {
|
||||||
|
throw new Error("Bitte geben Sie einen gültigen Nettopreis an.");
|
||||||
|
}
|
||||||
|
return Math.round(euros * 100);
|
||||||
|
}
|
||||||
|
|
||||||
|
function approveErrorMessage(error: unknown): string {
|
||||||
|
if (error instanceof EditorialServiceError) {
|
||||||
|
const messages: Record<string, string> = {
|
||||||
|
INVALID_PRICE: "Bitte geben Sie einen gültigen Nettopreis an.",
|
||||||
|
EDITORIAL_NOT_FOUND: "Redaktion nicht gefunden.",
|
||||||
|
EMAIL_NOT_VERIFIED: "Die E-Mail-Adresse der Redaktion ist noch nicht bestätigt.",
|
||||||
|
ALBUM_GRANT_REQUIRED: "Bitte weisen Sie zuerst mindestens ein Album zu.",
|
||||||
|
INVALID_ACCOUNT_TRANSITION:
|
||||||
|
"Die Redaktion kann in ihrem aktuellen Status nicht freigegeben werden.",
|
||||||
|
APPROVAL_NOTICE_DELIVERY_UNKNOWN:
|
||||||
|
"Der Freigabe-Hinweis konnte nicht eindeutig zugeordnet werden.",
|
||||||
|
};
|
||||||
|
return messages[error.code] ?? "Die Freigabe ist fehlgeschlagen.";
|
||||||
|
}
|
||||||
|
return "Die Freigabe ist fehlgeschlagen.";
|
||||||
|
}
|
||||||
|
|
||||||
|
function requiredId(value: unknown): string {
|
||||||
|
if (
|
||||||
|
typeof value !== "string" ||
|
||||||
|
!/^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(value)
|
||||||
|
) {
|
||||||
|
throw new Error("INVALID_ID");
|
||||||
|
}
|
||||||
|
return value;
|
||||||
|
}
|
||||||
@@ -7,6 +7,7 @@ import { authClient } from "./client";
|
|||||||
export type RegistrationFormState = {
|
export type RegistrationFormState = {
|
||||||
errors?: Partial<
|
errors?: Partial<
|
||||||
Record<
|
Record<
|
||||||
|
| "form"
|
||||||
| "companyName"
|
| "companyName"
|
||||||
| "contactName"
|
| "contactName"
|
||||||
| "email"
|
| "email"
|
||||||
@@ -47,6 +48,7 @@ export function RegistrationForm({
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<form action={formAction} className="account-form">
|
<form action={formAction} className="account-form">
|
||||||
|
<FieldError message={state.errors?.form} />
|
||||||
<div className="form-grid">
|
<div className="form-grid">
|
||||||
<label>
|
<label>
|
||||||
Redaktion oder Unternehmen
|
Redaktion oder Unternehmen
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
import Link from "next/link";
|
import Link from "next/link";
|
||||||
|
|
||||||
|
import { approveEditorialAction } from "@/app/admin/redaktionen/[id]/actions";
|
||||||
|
|
||||||
export type EditorialDetail = {
|
export type EditorialDetail = {
|
||||||
id: string;
|
id: string;
|
||||||
name: string;
|
name: string;
|
||||||
@@ -14,6 +16,14 @@ export function EditorialDetailContent({
|
|||||||
editorial,
|
editorial,
|
||||||
hasActiveGrant,
|
hasActiveGrant,
|
||||||
}: Readonly<{ editorial: EditorialDetail; hasActiveGrant: boolean }>) {
|
}: Readonly<{ editorial: EditorialDetail; hasActiveGrant: boolean }>) {
|
||||||
|
const emailVerified = Boolean(editorial.emailVerifiedAt);
|
||||||
|
const canApprove = hasActiveGrant && emailVerified;
|
||||||
|
const approvalHint = !emailVerified
|
||||||
|
? "Eine Freigabe ist erst möglich, nachdem die E-Mail-Adresse bestätigt wurde."
|
||||||
|
: hasActiveGrant
|
||||||
|
? "Mit der Freigabe wird der Nettopreis festgelegt und die Redaktion kann sich anmelden."
|
||||||
|
: "Eine Freigabe ist erst möglich, wenn mindestens ein aktives Albumrecht zugewiesen wurde.";
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<section className="content-section" aria-labelledby="editorial-title">
|
<section className="content-section" aria-labelledby="editorial-title">
|
||||||
<p className="eyebrow">Redaktion</p>
|
<p className="eyebrow">Redaktion</p>
|
||||||
@@ -37,15 +47,30 @@ export function EditorialDetailContent({
|
|||||||
|
|
||||||
<section aria-labelledby="approval-title">
|
<section aria-labelledby="approval-title">
|
||||||
<h2 id="approval-title">Freigabe</h2>
|
<h2 id="approval-title">Freigabe</h2>
|
||||||
<button type="button" disabled aria-describedby="approval-help">
|
{editorial.status === "active" ? (
|
||||||
|
<p>Die Redaktion ist freigegeben (Netto {formatCents(editorial.currentPriceCents ?? 0)}).</p>
|
||||||
|
) : (
|
||||||
|
<>
|
||||||
|
<form action={approveEditorialAction.bind(null, editorial.id)} className="account-form">
|
||||||
|
<label>
|
||||||
|
Individueller Nettopreis (EUR)
|
||||||
|
<input
|
||||||
|
inputMode="decimal"
|
||||||
|
min="0.01"
|
||||||
|
name="priceEuros"
|
||||||
|
required
|
||||||
|
step="0.01"
|
||||||
|
type="number"
|
||||||
|
/>
|
||||||
|
</label>
|
||||||
|
<button type="submit" disabled={!canApprove}>
|
||||||
Redaktion freigeben
|
Redaktion freigeben
|
||||||
</button>
|
</button>
|
||||||
<p id="approval-help">
|
</form>
|
||||||
{hasActiveGrant
|
<p>{approvalHint}</p>
|
||||||
? "Mindestens ein Albumrecht ist zugewiesen. Die Freigabe bleibt bis zur weiteren Verwaltungsoberfläche deaktiviert."
|
|
||||||
: "Eine Freigabe ist erst möglich, wenn mindestens ein aktives Albumrecht zugewiesen wurde."}
|
|
||||||
</p>
|
|
||||||
<Link href={`/admin/redaktionen/${editorial.id}/alben`}>Albumrechte verwalten</Link>
|
<Link href={`/admin/redaktionen/${editorial.id}/alben`}>Albumrechte verwalten</Link>
|
||||||
|
</>
|
||||||
|
)}
|
||||||
</section>
|
</section>
|
||||||
</section>
|
</section>
|
||||||
);
|
);
|
||||||
|
|||||||
Reference in new issue
Block a user