fix: magic links missing root prefix (#133)

* Fix magic links

* Include changeset

---------

Co-authored-by: Matt Kane <mkane@cloudflare.com>
This commit is contained in:
Justin White
2026-04-04 13:47:05 -05:00
committed by GitHub
parent 8fb4173036
commit 9269759674
8 changed files with 70 additions and 4 deletions

View File

@@ -56,6 +56,7 @@ describe("Invite", () => {
expect(result.email).toBe("new@example.com");
expect(result.url).toContain("https://example.com");
expect(result.url).toContain("/_emdash/api/auth/invite/accept?token=");
expect(result.url).toMatch(TOKEN_PARAM_REGEX);
// Should NOT have a token field on the result
expect("token" in result).toBe(false);

View File

@@ -0,0 +1,53 @@
import type { AuthAdapter, EmailSendFn } from "@emdash-cms/auth";
import type { EmailMessage } from "@emdash-cms/auth";
import { Role, sendMagicLink } from "@emdash-cms/auth";
import { createKyselyAdapter } from "@emdash-cms/auth/adapters/kysely";
import type { Kysely } from "kysely";
import { describe, it, expect, beforeEach, afterEach, vi } from "vitest";
import type { Database } from "../../../src/database/types.js";
import { setupTestDatabase, teardownTestDatabase } from "../../utils/test-db.js";
describe("Magic Link", () => {
let db: Kysely<Database>;
let adapter: AuthAdapter;
let mockEmailSend: EmailSendFn & ReturnType<typeof vi.fn>;
let sentEmails: Array<EmailMessage>;
beforeEach(async () => {
db = await setupTestDatabase();
adapter = createKyselyAdapter(db);
sentEmails = [];
mockEmailSend = vi.fn(async (email: EmailMessage) => {
sentEmails.push(email);
});
});
afterEach(async () => {
await teardownTestDatabase(db);
});
it("sends verify links through the injected EmDash auth route", async () => {
await adapter.createUser({
email: "author@example.com",
name: "Author",
role: Role.AUTHOR,
emailVerified: true,
});
await sendMagicLink(
{
baseUrl: "https://example.com",
siteName: "Test Site",
email: mockEmailSend,
},
adapter,
"author@example.com",
);
expect(mockEmailSend).toHaveBeenCalledOnce();
expect(sentEmails[0]!.text).toContain(
"https://example.com/_emdash/api/auth/magic-link/verify?token=",
);
});
});

View File

@@ -111,6 +111,9 @@ describe("Self-Signup", () => {
expect(mockEmailSend).toHaveBeenCalledTimes(1);
expect(sentEmails[0]!.to).toBe("newuser@allowed.com");
expect(sentEmails[0]!.subject).toContain("Test Site");
expect(sentEmails[0]!.text).toContain(
"https://example.com/_emdash/api/auth/signup/verify?token=",
);
expect(sentEmails[0]!.text).toContain("verify");
});