miguelgrinberg / Flask-HTTPAuth

Simple extension that provides Basic, Digest and Token HTTP authentication for Flask routes
MIT License
1.27k stars 228 forks source link

Custom return response on unauthorized #150

Closed sicenul closed 2 years ago

sicenul commented 2 years ago

Hi, I use HTTPBasicAuth, and when unauthorized, it will return "Unauthorized Access" response. Is it possible to change the response? and please show me how? for example, I want json response like this:

{
    "status": false,
    "code": 401,
    "message": "some message"
}

Thank you.

miguelgrinberg commented 2 years ago

Use the error_handler decorator:

@auth.error_handler
def auth_error(status):
    return {
        "status": False,
        "code": 401,
        "message": "some message"
    }, 401
sicenul commented 2 years ago

Nice, thanks.