Skip to content

feature/add customize auth handlers docs #2218

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 6 commits into
base: main
Choose a base branch
from

Conversation

tusharpandey13
Copy link
Contributor

@tusharpandey13 tusharpandey13 commented Jul 4, 2025

Fixes: #1895

Adds documentation about customizing auth handlers at various parts of authentication:

  • Before / after login
  • Before / after logout
  • Before callback handler

Includes example and prose

@tusharpandey13 tusharpandey13 requested a review from a team as a code owner July 4, 2025 06:07
@codecov-commenter
Copy link

codecov-commenter commented Jul 4, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 85.01%. Comparing base (4e3a2fe) to head (1222403).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2218   +/-   ##
=======================================
  Coverage   85.01%   85.01%           
=======================================
  Files          24       24           
  Lines        2309     2309           
  Branches      429      429           
=======================================
  Hits         1963     1963           
  Misses        340      340           
  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.

@tusharpandey13 tusharpandey13 marked this pull request as draft July 4, 2025 06:17
@tusharpandey13 tusharpandey13 marked this pull request as ready for review July 9, 2025 04:40
@tusharpandey13
Copy link
Contributor Author

@guabu does this look right?


## Customizing Auth Handlers

In v4, authentication routes (`/auth/login`, `/auth/logout`, `/auth/callback`) are handled automatically by the middleware. While you can no longer customize individual route handlers directly like in v3, you can intercept these routes in your middleware to run custom logic before the auth handlers execute.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to mention older versions in the README? It feels more appropriate to leave those to the migration guide where it's more relevant.


### Run custom code before Auth Handlers

Following example shows how to run custom logic before the `logout` handler:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would actually be running after the logout handler but before the response is sent to the user

Comment on lines +1298 to +1299
// "/your/login/returnTo/url" : intercept redirect after login
// "/your/logout/returnTo/url" : intercept redirect after logout
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where do these come from?

Comment on lines +1305 to +1322
// do custom stuff
console.log("Pre-logout code")

// Example: Set a cookie
authRes.cookies.set('myCustomCookie', 'cookieValue', { path: '/' });
// Example: Set another cookie with options
authRes.cookies.set({
name: 'anotherCookie',
value: 'anotherValue',
httpOnly: true,
path: '/',
});

// Example: Delete a cookie
// authRes.cookies.delete('cookieNameToDelete');

// you can also do an early return here with your own NextResponse object
// return NextResponse.redirect(new URL('/custom-logout-page'));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit (feel free to ignore): I would simplify the example to keep it concise

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.

v4: Customize auth handlers
4 participants