From 1fe35ca189cddec68fba23dfc8c88692a26e63f1 Mon Sep 17 00:00:00 2001 From: Joe Butler Date: Wed, 4 Sep 2019 08:11:52 +0100 Subject: [PATCH] Adding request to user mthod --- README.rst | 7 +++++++ src/django_cognito_jwt/backend.py | 6 +++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/README.rst b/README.rst index 01565b3..95ee32b 100644 --- a/README.rst +++ b/README.rst @@ -71,3 +71,10 @@ you can use the ``COGNITO_USER_MODEL`` setting. .. code-block:: python COGNITO_USER_MODEL = "myproject.AppUser" + +(Optional) If you want to send the request object to the custom user model then set the ``COGNITO_USER_MODEL_ADD_REQUEST_OBJ`` to true. + +.. code-block:: python + + COGNITO_USER_MODEL_ADD_REQUEST_OBJ = True + diff --git a/src/django_cognito_jwt/backend.py b/src/django_cognito_jwt/backend.py index 2a86351..34ef9a1 100644 --- a/src/django_cognito_jwt/backend.py +++ b/src/django_cognito_jwt/backend.py @@ -28,8 +28,12 @@ def authenticate(self, request): except TokenError: raise exceptions.AuthenticationFailed() + add_request = getattr(settings, "COGNITO_USER_MODEL_ADD_REQUEST_OBJ", False) USER_MODEL = self.get_user_model() - user = USER_MODEL.objects.get_or_create_for_cognito(jwt_payload) + if add_request: + user = USER_MODEL.objects.get_or_create_for_cognito(jwt_payload, request) + else: + user = USER_MODEL.objects.get_or_create_for_cognito(jwt_payload) return (user, jwt_token) def get_user_model(self):