-
-
Notifications
You must be signed in to change notification settings - Fork 579
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
Basic auth not working when using app.onError #952
Comments
Hi @JustJoostNL ! Since version 3.0.0, a basic auth middleware throws the app.onError((err, c) => {
if (err instanceof HTTPException) {
const res = err.getResponse()
return new Response(
JSON.stringify({
message: 'Unauthorized',
status: 401,
}),
res
)
}
return c.text('Internal Server Error', 500)
}) This is a bit confusing. If you can work it fine, I will put it in the documentation. Thank you. P.S: @usualoma @ThatOneBro and others If you have any smart ideas to handle the exception, please share these. But I think this way is better. Or it's trouble to make a response from the if (err instanceof HTTPException) {
const res = err.getResponse()
return c.json(
{
message: 'Unauthorized',
status: 401,
},
res
)
} But it may be unnecessary because the cord getting fat. |
Hey @yusukebe! Thanks for your answer! I assume I need to use an I did try: import { HTTPException } from "hono/dist/types/http-exception"; But I get an error when using that. |
I myself have never used Lines 36 to 40 in 64956cb
|
So using the But when using it doesn't even ask for login details. (It doesn't ask for a username and password). While when I comment the As I said earlier, this occurs since 3.0.0 and later. I hope someone can help! Thanks in advance! |
To make it ask for a username and password, we have to set the appropriate 'WWW-Authenticate': 'Basic realm="Secure Area"' The const res = err.getResponse()
return new Response(
JSON.stringify({
message: 'Unauthorized',
status: 401,
}),
res
) If you don't add |
I thought about this for a while. |
Thanks, this fixed it! Can we close this, or wait for #959? |
Good! Let's leave the open. |
@yusukebe I'm missing something here. I don't know/understand where to put import { Hono } from 'hono'
import { basicAuth } from 'hono/basic-auth'
const app = new Hono()
app.use(
'/auth/page',
basicAuth({
username: 'user',
password: 'pwd',
})
)
app.get('/auth/page', (c) => {
console.log("Inside auth/page")
c.header('WWW-Authenticate', 'Basic realm="Protected Area"'); /// ??????
return c.text('You are authorized')
})
app.get('/ai/:id', (c) => c.text('You are inside AI route'))
app.notFound(async (c) => c.redirect(`/ai/${crypto.randomUUID()}`))
app.onError((_err, c) => c.notFound())
export default app |
Hi @charnould You have to handle a case where the user is unauthorized. Your code always returns a 404 response. To handle the reauthorization error, check the app.onError((err, c) => {
if (err instanceof HTTPException) {
return err.getResponse()
}
return c.notFound()
}) Or you might not return |
Thanks a lot @yusukebe ! It works perfectly. |
Since version 3.0.0 the basic auth doesn't work anymore when also using
app.onError
Example:
In this example, when you use the /test-auth route, it just returns an "Internal Server Error" and the error it returns is: "Error: Unauthorized"
When I am not using the
app.onError
(I comment that out), it does work normally.This issue occurs since version 3.0.0
I hope someone can help 🙂
Thanks in advance!
The text was updated successfully, but these errors were encountered: