Spend login, invite, and email-change links only after a confirm POST so mail scanners cannot use them up.
GET still shows a one-button page, a used invitation can resend or reopen for the signed-in invitee, and an email-change GET no longer signs the account out.
This commit is contained in:
1 parent
600056499c
commit
a725cf1a32
33 files changed
+1002
-89
No files matched your search
@@ -34,10 +34,13 @@ vi.mock("../../lib/server/db", () => ({
|
||||
user: {
|
||||
upsert: (...args: unknown[]) => upsertMock(...args),
|
||||
},
|
||||
ruleDraft: {
|
||||
upsert: vi.fn(),
|
||||
},
|
||||
},
|
||||
}));
|
||||
|
||||
import { GET } from "../../app/api/auth/magic-link/verify/route";
|
||||
import { GET, POST } from "../../app/api/auth/magic-link/verify/route";
|
||||
|
||||
beforeEach(() => {
|
||||
isDatabaseConfiguredMock.mockReset();
|
||||
@@ -87,5 +90,54 @@ describe("GET /api/auth/magic-link/verify", () => {
|
||||
const res = await GET(getWithToken("a-token-value-long-enough"));
|
||||
expect(res.status).toBe(307);
|
||||
expect(res.headers.get("location")).toContain("error=expired_link");
|
||||
expect(deleteMock).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("redirects a valid token to the confirm page without spending it", async () => {
|
||||
const token = "a-token-value-long-enough";
|
||||
findUniqueMock.mockResolvedValue({
|
||||
id: "row-1",
|
||||
email: "a@b.c",
|
||||
expiresAt: new Date(Date.now() + 60_000),
|
||||
nextPath: "/create/review",
|
||||
draftPayload: null,
|
||||
});
|
||||
const res = await GET(getWithToken(token));
|
||||
expect(res.status).toBe(307);
|
||||
expect(res.headers.get("location")).toContain("/login/confirm");
|
||||
expect(res.headers.get("location")).toContain(encodeURIComponent(token));
|
||||
expect(deleteMock).not.toHaveBeenCalled();
|
||||
expect(upsertMock).not.toHaveBeenCalled();
|
||||
expect(createSessionForUserMock).not.toHaveBeenCalled();
|
||||
expect(setSessionCookieMock).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe("POST /api/auth/magic-link/verify", () => {
|
||||
it("spends a valid token, starts a session, and redirects", async () => {
|
||||
findUniqueMock.mockResolvedValue({
|
||||
id: "row-1",
|
||||
email: "a@b.c",
|
||||
expiresAt: new Date(Date.now() + 60_000),
|
||||
nextPath: "/create/review",
|
||||
draftPayload: null,
|
||||
});
|
||||
upsertMock.mockResolvedValue({ id: "u1", email: "a@b.c" });
|
||||
createSessionForUserMock.mockResolvedValue({
|
||||
token: "sess",
|
||||
expiresAt: new Date(),
|
||||
});
|
||||
|
||||
const res = await POST(
|
||||
new NextRequest(getWithToken("a-token-value-long-enough").url, {
|
||||
method: "POST",
|
||||
}),
|
||||
);
|
||||
|
||||
expect(res.status).toBe(303);
|
||||
expect(res.headers.get("location")).toContain("/create/review");
|
||||
expect(deleteMock).toHaveBeenCalledWith({ where: { id: "row-1" } });
|
||||
expect(upsertMock).toHaveBeenCalled();
|
||||
expect(setSessionCookieMock).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user