Skip to content
Draft
648 changes: 541 additions & 107 deletions src/server/auth-client.test.ts

Large diffs are not rendered by default.

460 changes: 272 additions & 188 deletions src/server/auth-client.ts

Large diffs are not rendered by default.

53 changes: 41 additions & 12 deletions src/server/base-path-logout-integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { RequestCookies, ResponseCookies } from "@edge-runtime/cookies";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";

import { Auth0Client } from "./client.js";
import { deleteChunkedCookie, deleteCookie } from "./cookies.js";
import { deleteChunkedCookie } from "./cookies.js";
import { Auth0RequestCookies, Auth0ResponseCookies } from "./http/index.js";
import { StatelessSessionStore } from "./session/stateless-session-store.js";
import { TransactionStore } from "./transaction-store.js";

Expand Down Expand Up @@ -55,7 +56,10 @@ describe("Base path and cookie configuration tests", () => {
mockReqCookies.getAll = () => [];

// Call delete method (this would be called during logout)
await sessionStore.delete(mockReqCookies, mockResCookies);
await sessionStore.delete(
new Auth0RequestCookies(mockReqCookies),
new Auth0ResponseCookies(mockResCookies)
);

// Verify that the cookie deletion header includes the correct path
const cookieHeader = mockResCookies.toString();
Expand Down Expand Up @@ -90,7 +94,10 @@ describe("Base path and cookie configuration tests", () => {
mockReqCookies.get = () => ({ value: "mock-session-value" });
mockReqCookies.getAll = () => [];

await sessionStore.delete(mockReqCookies, mockResCookies);
await sessionStore.delete(
new Auth0RequestCookies(mockReqCookies),
new Auth0ResponseCookies(mockResCookies)
);

const cookieHeader = mockResCookies.toString();

Expand Down Expand Up @@ -132,7 +139,8 @@ describe("Base path and cookie configuration tests", () => {
});

it("should delete cookie with default path when no path is specified", () => {
deleteCookie(mockResCookies, "test-cookie");
const auth0ResCookies = new Auth0ResponseCookies(mockResCookies);
auth0ResCookies.delete("test-cookie");

const cookieHeader = mockResCookies.toString();
expect(cookieHeader).toContain("test-cookie=");
Expand All @@ -141,7 +149,11 @@ describe("Base path and cookie configuration tests", () => {
});

it("should delete cookie with specified path", () => {
deleteCookie(mockResCookies, "test-cookie", { path: "/dashboard" });
const auth0ResCookies = new Auth0ResponseCookies(mockResCookies);
auth0ResCookies.delete({
name: "test-cookie",
path: "/dashboard"
});

const cookieHeader = mockResCookies.toString();
expect(cookieHeader).toContain("test-cookie=");
Expand All @@ -150,7 +162,11 @@ describe("Base path and cookie configuration tests", () => {
});

it("should delete cookie with root path explicitly", () => {
deleteCookie(mockResCookies, "test-cookie", { path: "/" });
const auth0ResCookies = new Auth0ResponseCookies(mockResCookies);
auth0ResCookies.delete({
name: "test-cookie",
path: "/"
});

const cookieHeader = mockResCookies.toString();
expect(cookieHeader).toContain("test-cookie=");
Expand All @@ -168,8 +184,8 @@ describe("Base path and cookie configuration tests", () => {

deleteChunkedCookie(
"test-cookie",
mockReqCookies,
mockResCookies,
new Auth0RequestCookies(mockReqCookies),
new Auth0ResponseCookies(mockResCookies),
false,
{ path: "/dashboard" }
);
Expand Down Expand Up @@ -284,7 +300,10 @@ describe("Base path and cookie configuration tests", () => {
}
});

await sessionStore.delete(mockReqCookies, mockResCookies);
await sessionStore.delete(
new Auth0RequestCookies(mockReqCookies),
new Auth0ResponseCookies(mockResCookies)
);

const cookieHeader = mockResCookies.toString();
expect(cookieHeader).toContain("__session=");
Expand All @@ -304,7 +323,10 @@ describe("Base path and cookie configuration tests", () => {
}
});

await transactionStore.delete(mockResCookies, "test-state");
await transactionStore.delete(
new Auth0ResponseCookies(mockResCookies),
"test-state"
);

const cookieHeader = mockResCookies.toString();
expect(cookieHeader).toContain("__txn_test-state=");
Expand All @@ -330,7 +352,10 @@ describe("Base path and cookie configuration tests", () => {
}
});

await transactionStore.deleteAll(mockReqCookies, mockResCookies);
await transactionStore.deleteAll(
new Auth0RequestCookies(mockReqCookies),
new Auth0ResponseCookies(mockResCookies)
);

const cookieHeader = mockResCookies.toString();
expect(cookieHeader).toContain("__txn_state1=");
Expand Down Expand Up @@ -373,7 +398,11 @@ describe("Base path and cookie configuration tests", () => {
});

it("should handle empty path correctly", () => {
deleteCookie(new ResponseCookies(new Headers()), "test-cookie", {
const auth0ResponseCookies = new Auth0ResponseCookies(
new ResponseCookies(new Headers())
);
auth0ResponseCookies.delete({
name: "test-cookie",
path: ""
});

Expand Down
11 changes: 9 additions & 2 deletions src/server/beforeSessionSaved-token-refresh-flow.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { NextRequest } from "next/server.js";
import { NextRequest, NextResponse } from "next/server.js";
import * as jose from "jose";
import { http, HttpResponse } from "msw";
import { setupServer } from "msw/node";
Expand All @@ -10,6 +10,7 @@ import { generateSecret } from "../test/utils.js";
import { SessionData } from "../types/index.js";
import { AuthClient } from "./auth-client.js";
import { decrypt, encrypt } from "./cookies.js";
import { Auth0NextRequest, Auth0NextResponse } from "./http/index.js";
import { StatelessSessionStore } from "./session/stateless-session-store.js";
import { TransactionStore } from "./transaction-store.js";

Expand Down Expand Up @@ -217,7 +218,13 @@ describe("AuthClient - beforeSessionSaved hook", async () => {
headers
});

const response = await authClient.handleAccessToken(request);
const auth0Req = new Auth0NextRequest(request);

const auth0Res = new Auth0NextResponse(NextResponse.next());

await authClient.handleAccessToken(auth0Req, auth0Res);

const response = auth0Res.res;

// Verify the response
expect(response.status).toEqual(200);
Expand Down
Loading