feat: add editorial registration and authentication

This commit is contained in:
Leon Tonhäuser committed 2026-08-09 20:48:43 +02:00
1 parent 7acaacb92e
commit 9b22752ef2
20 files changed
+1728 -20

No files matched your search

+36
View File
@@ -0,0 +1,36 @@
import { expect, test, vi } from "vitest";
import { createMailer } from "./mailer";
test("sends a verification link through the configured SMTP boundary", async () => {
const transport = { sendMail: vi.fn().mockResolvedValue({ messageId: "message-1" }) };
const mailer = createMailer({ from: "portal@example.com", transport });
await mailer.sendVerification({
to: "editorial@example.com",
verificationUrl: "https://portal.example.com/api/auth/verify-email?token=secret-token",
});
expect(transport.sendMail).toHaveBeenCalledWith({
from: "portal@example.com",
to: "editorial@example.com",
subject: "Bitte bestätigen Sie Ihre E-Mail-Adresse",
text: expect.stringContaining(
"https://portal.example.com/api/auth/verify-email?token=secret-token",
),
});
});
test("sends an approval notice without leaking SMTP credentials", async () => {
const transport = { sendMail: vi.fn().mockResolvedValue({ messageId: "message-2" }) };
const mailer = createMailer({ from: "portal@example.com", transport });
await mailer.sendApprovalNotice({ to: "editorial@example.com", editorialName: "Neue Redaktion" });
expect(transport.sendMail).toHaveBeenCalledWith({
from: "portal@example.com",
to: "editorial@example.com",
subject: "Ihr Zugang zum Redaktionsportal ist freigegeben",
text: expect.stringContaining("Neue Redaktion"),
});
});
+64
View File
@@ -0,0 +1,64 @@
import "server-only";
import nodemailer from "nodemailer";
import { env } from "@/config/env";
export type MailMessage = {
from: string;
to: string;
subject: string;
text: string;
};
export type MailTransport = {
sendMail(message: MailMessage): Promise<unknown>;
};
export type Mailer = {
sendVerification(input: { to: string; verificationUrl: string }): Promise<void>;
sendPasswordReset(input: { to: string; resetUrl: string }): Promise<void>;
sendApprovalNotice(input: { to: string; editorialName: string }): Promise<void>;
};
export function createMailer({ from, transport }: { from: string; transport: MailTransport }): Mailer {
async function send(message: Omit<MailMessage, "from">): Promise<void> {
await transport.sendMail({ from, ...message });
}
return {
async sendVerification({ to, verificationUrl }) {
await send({
to,
subject: "Bitte bestätigen Sie Ihre E-Mail-Adresse",
text: `Bitte bestätigen Sie Ihre E-Mail-Adresse für das Redaktionsportal:\n\n${verificationUrl}`,
});
},
async sendPasswordReset({ to, resetUrl }) {
await send({
to,
subject: "Passwort für das Redaktionsportal zurücksetzen",
text: `Setzen Sie Ihr Passwort für das Redaktionsportal zurück:\n\n${resetUrl}`,
});
},
async sendApprovalNotice({ to, editorialName }) {
await send({
to,
subject: "Ihr Zugang zum Redaktionsportal ist freigegeben",
text: `Der Zugang für ${editorialName} wurde freigegeben. Sie können sich jetzt im Redaktionsportal anmelden.`,
});
},
};
}
const smtpTransport = nodemailer.createTransport({
host: env.SMTP_HOST,
port: env.SMTP_PORT,
secure: env.SMTP_PORT === 465,
auth: {
user: env.SMTP_USER,
pass: env.SMTP_PASSWORD,
},
});
export const mailer = createMailer({ from: env.SMTP_FROM, transport: smtpTransport });