-
-
Notifications
You must be signed in to change notification settings - Fork 93
feat(middleware): add customizable error handler for auth failures #265
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
Conversation
Add ErrorHandler option to customize error responses when authentication fails. The handler receives the HTTP status code (401/403) and the error, allowing custom HTML pages, redirects, or JSON responses. Related to #264
- remove duplicate default error handler logic from NewService (errResponse already handles the default case) - add ErrorHandler documentation to README.md - add progress-*.txt to .gitignore
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.
Pull request overview
This PR adds customizable error handling for authentication failures in the middleware package, addressing issue #264. The implementation allows users to customize how authentication errors are handled (e.g., returning JSON responses for API routes or redirecting to login pages for web routes) while maintaining backward compatibility with the default behavior.
Changes:
- Added
ErrorHandlerFunctype to allow custom error response handling on authentication failures - Extended
Authenticatorstruct andOptsto include optionalErrorHandlerfield - Refactored error responses in Auth, AdminOnly, and RBAC middlewares to use a centralized
errResponsemethod
Reviewed changes
Copilot reviewed 4 out of 5 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| v2/middleware/auth.go | Added ErrorHandlerFunc type, ErrorHandler field to Authenticator, and errResponse helper method; updated Auth, AdminOnly, and RBAC to use custom error handler |
| v2/auth.go | Added ErrorHandler field to Opts and wired it to Authenticator in NewService |
| v2/middleware/auth_test.go | Added comprehensive test coverage for custom error handlers in Auth, AdminOnly, and RBAC middlewares |
| README.md | Added documentation and usage example for the new ErrorHandlerFunc feature |
| .gitignore | Added entries for ralph progress logs (unrelated to this PR) |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Pull Request Test Coverage Report for Build 21151713439Details
💛 - Coveralls |
|
This is exactly what I had in mind :-) Thank you! |
|
oops, just noticed in your original ticket: "If you're open to it, I could try to implement these sorry, missed it completely and jumped on this one ;) |
No worries! I'm always eager to contribute :D. When I saw that function literal, I thought it would be great to make it customizable and didn't want to 'demand' a change that I could implement myself. |
Adds
ErrorHandlerFunctype to customize error responses on auth failures. Related to #264What changed:
ErrorHandlerFunctype in middleware packageErrorHandlerfield inAuthenticatorstruct andOptserrResponsewhich calls custom handler if setUsage example:
@fbuedding pls take a look and let me know if this covers the functionality you had in mind