-
-
Notifications
You must be signed in to change notification settings - Fork 652
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
feat(basic-auth): added custom response message option #3371
feat(basic-auth): added custom response message option #3371
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## next #3371 +/- ##
=======================================
Coverage 95.77% 95.78%
=======================================
Files 152 152
Lines 9187 9202 +15
Branches 2818 2823 +5
=======================================
+ Hits 8799 8814 +15
Misses 388 388 ☔ View full report in Codecov by Sentry. |
@marceloverdijk Thank you for the PR! Regarding the I think the following is best: type CustomMessageFunction = (c: Context) => Response | Promise<Response> Returning diff --git a/src/middleware/basic-auth/index.ts b/src/middleware/basic-auth/index.ts
index b9b9f359..f69cc049 100644
--- a/src/middleware/basic-auth/index.ts
+++ b/src/middleware/basic-auth/index.ts
@@ -9,19 +9,21 @@ import type { MiddlewareHandler } from '../../types'
import { auth } from '../../utils/basic-auth'
import { timingSafeEqual } from '../../utils/buffer'
+type CustomMessageFunction = (c: Context) => Response | Promise<Response>
+
type BasicAuthOptions =
| {
username: string
password: string
realm?: string
hashFunction?: Function
- invalidUserMessage?: string | object | Function
+ invalidUserMessage?: CustomMessageFunction
}
| {
verifyUser: (username: string, password: string, c: Context) => boolean | Promise<boolean>
- invalidUserMessage?: string | object | Function
+ invalidUserMessage?: CustomMessageFunction
}
/**
@@ -73,10 +75,6 @@ export const basicAuth = (
options.realm = 'Secure Area'
}
- if (!options.invalidUserMessage) {
- options.invalidUserMessage = 'Unauthorized'
- }
-
if (usernamePasswordInOptions) {
users.unshift({ username: options.username, password: options.password })
}
@@ -104,23 +102,15 @@ export const basicAuth = (
}
// Invalid user.
const status = 401
- const headers = {
- 'WWW-Authenticate': 'Basic realm="' + options.realm?.replace(/"/g, '\\"') + '"',
+ ctx.status(401)
+ ctx.header('WWW-Authenticate', 'Basic realm="' + options.realm?.replace(/"/g, '\\"') + '"')
+
+ const defaultCustomMessage = (c: Context) => {
+ return c.text('Unauthorized')
}
- const responseMessage =
- typeof options.invalidUserMessage === 'function'
- ? await options.invalidUserMessage(ctx)
- : options.invalidUserMessage
- const res =
- typeof responseMessage === 'string'
- ? new Response(responseMessage, { status, headers })
- : new Response(JSON.stringify(responseMessage), {
- status,
- headers: {
- ...headers,
- 'content-type': 'application/json; charset=UTF-8',
- },
- })
+
+ const customResponse = options.invalidUserMessage ?? defaultCustomMessage
+ const res = await customResponse(ctx)
throw new HTTPException(status, { res })
}
} I'd like to hear other's opinions: cc @usualoma |
I think almost all are good. I've left a comment. Check it. |
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.
LGTM!
Looks good to me! Let's go with this. I'll merge this into the |
The author should do the following, if applicable
bun run format:fix && bun run lint:fix
to format the code