这是indexloc提供的服务,不要输入任何密码
Skip to content

Conversation

@x-user
Copy link
Contributor

@x-user x-user commented Feb 2, 2024

Fix minor errors in OAuth2 how to, but huma.WriteErr requires huma.API as first argument and there no way to obtain it in middleware. I was thinking about replacing it with http.Error, but it requires http.ResponseWriter and huma.Context doesn't implement it. So i'm stuck there.

@codecov
Copy link

codecov bot commented Feb 2, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (9570636) 93.37% compared to head (95ad62c) 93.37%.

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #225   +/-   ##
=======================================
  Coverage   93.37%   93.37%           
=======================================
  Files          17       17           
  Lines        2189     2189           
=======================================
  Hits         2044     2044           
  Misses        106      106           
  Partials       39       39           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@x-user
Copy link
Contributor Author

x-user commented Feb 2, 2024

I can use

ctx.SetStatus(http.StatusUnauthorized)
ctx.BodyWriter().Write([]byte(http.StatusText(http.StatusUnauthorized)))

But it looks kinda ugly.

@x-user x-user changed the title Oauth2 how to Errors in OAuth2 how to examples Feb 2, 2024
@danielgtaylor
Copy link
Owner

danielgtaylor commented Feb 2, 2024

@x-user thanks for fixing this up! You could maybe pass the api in when creating the middleware, then you can use huma.WriteErr and get content negotiation and the normal error model in the error response:

api.UseMiddleware(NewAuthMiddleware(api, "https://example.com/.well-known/jwks.json"))

@x-user
Copy link
Contributor Author

x-user commented Feb 2, 2024

Yes closures can solve this. Then i will make it this way.

@x-user x-user marked this pull request as ready for review February 2, 2024 17:57
Copy link
Owner

@danielgtaylor danielgtaylor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, thank you! 🚀

@danielgtaylor danielgtaylor merged commit 456aafa into danielgtaylor:main Feb 2, 2024
@x-user x-user deleted the oauth2_how_to branch February 3, 2024 05:38
@x-user x-user restored the oauth2_how_to branch February 3, 2024 05:41
@x-user x-user deleted the oauth2_how_to branch February 3, 2024 05:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants