-
-
Notifications
You must be signed in to change notification settings - Fork 923
feat(basic-auth): add context key and callback options #4645
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
Add two new options to basicAuth middleware: - `usernameContextKey`: stores authenticated username in context - `true`: uses default key 'basicAuthUsername' - `string`: uses custom key name - `onAuthSuccess`: callback invoked after successful authentication This allows route handlers to access the authenticated username without re-parsing the Authorization header.
Add test cases for: - usernameContextKey with default key 'basicAuthUsername' - usernameContextKey with custom key - usernameContextKey not set (should not store username) - usernameContextKey with verifyUser mode - onAuthSuccess callback execution - onAuthSuccess async callback support - onAuthSuccess not called on failed auth - onAuthSuccess with verifyUser mode - Both options used together
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #4645 +/- ##
=======================================
Coverage 91.45% 91.45%
=======================================
Files 173 173
Lines 11322 11342 +20
Branches 3279 3288 +9
=======================================
+ Hits 10354 10373 +19
- Misses 967 968 +1
Partials 1 1 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
| * - `true`: stores under the key `'basicAuthUsername'` | ||
| * - `string`: stores under the specified custom key | ||
| */ | ||
| usernameContextKey?: boolean | string |
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.
usernameContextKey is needed? I think it is unnecessary. You can use the following method:
basicAuth({
username: 'hono',
password: 'secret',
onAuthSuccess: (c, username) => {
c.set('basicAuthUsername', username)
},
})It's simple.
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.
usernameContextKey is indeed just a shortcut, and you are right, we should keep it simple.
Close #4192
Add two new options to basicAuth middleware:
usernameContextKey: stores authenticated username in contexttrue: uses default key 'basicAuthUsername'string: uses custom key nameonAuthSuccess: callback invoked after successful authenticationThis allows route handlers to access the authenticated username without
re-parsing the Authorization header.
The author should do the following, if applicable
bun run format:fix && bun run lint:fixto format the code