-
Notifications
You must be signed in to change notification settings - Fork 426
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
base: main
Are you sure you want to change the base?
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
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. 🚀 New features to boost your workflow:
|
@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. |
There was a problem hiding this comment.
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: |
There was a problem hiding this comment.
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
// "/your/login/returnTo/url" : intercept redirect after login | ||
// "/your/logout/returnTo/url" : intercept redirect after logout |
There was a problem hiding this comment.
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?
// 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')); |
There was a problem hiding this comment.
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
Fixes: #1895
Adds documentation about customizing auth handlers at various parts of authentication:
Includes example and prose