Skip to content

fix: cookie not being deleted correctly when basepath set #2223

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

mustafagft
Copy link

  • All new/changed/fixed functionality is covered by tests (or N/A)
  • I have added documentation for all new/changed functionality (or N/A)

📋 Changes

Fixed: Cookie deletion during logout when NEXT_PUBLIC_BASE_PATH is configured

This PR addresses a critical bug where session and transaction cookies were not being properly cleared during logout when a Next.js application uses a base path configuration.

Key Changes:

  • Enhanced Auth0Client constructor: Now automatically detects NEXT_PUBLIC_BASE_PATH environment variable and configures cookie paths accordingly
  • Updated cookie deletion functions: All cookie deletion methods (deleteCookie, deleteChunkedCookie) now accept and use the correct path parameter to ensure cookies are cleared with the same path they were set with
  • Fixed route handling in AuthClient: Added proper base path stripping and normalization to handle authentication routes correctly when base path is configured
  • Maintained configuration hierarchy: Explicit cookie path configurations (via AUTH0_COOKIE_PATH or client options) still take precedence over auto-detected base path

Types and methods changed:

  • deleteCookie() - Added optional path parameter
  • deleteChunkedCookie() - Added optional path parameter
  • Auth0Client constructor - Enhanced to auto-configure cookie paths based on base path
  • AuthClient.handler() - Added base path stripping logic

Backward Compatibility: All changes are backward compatible. Applications without base path configuration continue to work exactly as before, using root path (/) for cookies.

📎 References

This fix addresses issues with Next.js applications deployed with base path configurations where users would remain logged in after attempting to logout because the session cookies were not being properly cleared.

Related to base path support added in #2167 and cookie deletion improvements in #2200.

🎯 Testing

Manual Testing Steps:

  1. Set up a Next.js application with NEXT_PUBLIC_BASE_PATH="/dashboard" in environment variables
  2. Configure Auth0 with the SDK
  3. Log in to create a session
  4. Navigate to /dashboard/auth/logout
  5. Verify that session cookies are properly cleared and user is logged out
  6. Confirm that browser dev tools show cookies being deleted with correct path (/dashboard)

Automated Testing:

  • New test file: src/server/base-path-logout.test.ts - Comprehensive tests covering base path cookie configuration and logout scenarios
  • Enhanced existing tests: Updated auth-client.test.ts, client.test.ts, and cookie-related tests to verify path parameter handling
  • Edge cases covered: Tests for nested base paths, trailing slashes, explicit path overrides, and regression testing without base path

Test Coverage:

  • ✅ Cookie path auto-configuration from base path
  • ✅ Logout cookie clearing with correct paths
  • ✅ Transaction cookie cleanup
  • ✅ Override behavior (explicit paths take precedence)
  • ✅ Backward compatibility (no base path scenarios)
  • ✅ Various base path formats (with/without leading slash, nested paths)

All existing tests continue to pass, ensuring no regression in functionality.

@tusharpandey13
Copy link
Contributor

Thanks @mustafagft , we'll take a look and merge this if it looks correct 👍

@codecov-commenter
Copy link

Codecov Report

Attention: Patch coverage is 97.43590% with 1 line in your changes missing coverage. Please review.

Project coverage is 84.44%. Comparing base (a063a8d) to head (bbf14b4).

Files with missing lines Patch % Lines
src/server/session/stateless-session-store.ts 50.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2223      +/-   ##
==========================================
+ Coverage   84.23%   84.44%   +0.21%     
==========================================
  Files          22       22              
  Lines        2208     2225      +17     
  Branches      397      409      +12     
==========================================
+ Hits         1860     1879      +19     
+ Misses        342      340       -2     
  Partials        6        6              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

import { describe, it, expect, beforeEach, afterEach } from "vitest";
import { NextRequest } from "next/server.js";
import { Auth0Client } from "./client.js";
import { AuthClient } from "./auth-client.js";

Check notice

Code scanning / CodeQL

Unused variable, import, function or class Note

Unused import AuthClient.
import { NextRequest } from "next/server.js";
import { Auth0Client } from "./client.js";
import { AuthClient } from "./auth-client.js";
import { StatelessSessionStore } from "./session/stateless-session-store.js";

Check notice

Code scanning / CodeQL

Unused variable, import, function or class Note

Unused import StatelessSessionStore.
import { Auth0Client } from "./client.js";
import { AuthClient } from "./auth-client.js";
import { StatelessSessionStore } from "./session/stateless-session-store.js";
import { TransactionStore } from "./transaction-store.js";

Check notice

Code scanning / CodeQL

Unused variable, import, function or class Note

Unused import TransactionStore.
sid: "session_123"
};

function getMockAuthorizationServer() {

Check notice

Code scanning / CodeQL

Unused variable, import, function or class Note

Unused function getMockAuthorizationServer.
@@ -36,6 +36,7 @@
TransactionCookieOptions,
TransactionStore
} from "./transaction-store.js";
import { ensureLeadingSlash, normalizeWithBasePath } from "../utils/pathUtils.js";

Check notice

Code scanning / CodeQL

Unused variable, import, function or class Note

Unused import normalizeWithBasePath.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants