jeremyevans / rodauth

Ruby's Most Advanced Authentication Framework
http://rodauth.jeremyevans.net
MIT License
1.65k stars 95 forks source link

`return_json_response` is not defined when returning an error #391

Closed renchap closed 5 months ago

renchap commented 5 months ago

I am using return_json_response to reformat Rodauth's responses so they match our general API patterns.

Unfortunately, it seems that _return_json_response is used in some places when returning an error, for example when a JWT format is invalid (_return_json_response is called directly), or when rescue_jwt_payload from jwt_refresh.rb is called (not sure why return_json_response is not called here?).

I think it would make sense to either have all JSON responses go through the return_json_response method, or have a return_json_error you can customise.

In our case, our JSON responses from the app are expected to have this format

{
  "status": "error",
  "errors:": [
    "error_code"
  ]
}

I would like to remove the need for each app consuming the API to also handle the form used by Rodauth ({ "error": "error_message" ))

janko commented 5 months ago

Did you consider overriding json_response_body instead? You would need to serialize the hash to JSON yourself, but I think that would catch all scenarios.

jeremyevans commented 5 months ago

@janko is correct, you want to use the json_response_body configuration method to override the formatting of the response body.