pythondjangodjango-allauth

Return http status code 401 when login with invalid credentials


How would i go about changing an django application that uses allauth so that it returns 401 response when invalid login credentials are provided?

I have tried to put custom logic in a custom ModelBackend but found no way to actually modify the response status code there.

I have also tried to put custom logic in a CustomAccountAdapter.authentication_failed but same issue there i found no way to change the status code.


Solution

  • I managed to change the http status code by adding the following middleware last

    class Auth401Middleware:
        def __init__(self, get_response):
            self.get_response = get_response
    
        def __call__(self, request):
            response = self.get_response(request)        
            if request.path == "/accounts/login/" and request.method == "POST" and not request.user.is_authenticated:
                response.status_code = 401
            return response