From fbb96027c54d23735c482d701defda67dfdc681b Mon Sep 17 00:00:00 2001 From: Ou7law007 <64309267+Ou7law007@users.noreply.github.com> Date: Fri, 15 Sep 2023 00:52:32 +0200 Subject: [PATCH 1/5] user_logged_in instead of update_last_login --- rest_framework_simplejwt/serializers.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/rest_framework_simplejwt/serializers.py b/rest_framework_simplejwt/serializers.py index 3dc318687..55bc40be8 100644 --- a/rest_framework_simplejwt/serializers.py +++ b/rest_framework_simplejwt/serializers.py @@ -2,7 +2,7 @@ from django.conf import settings from django.contrib.auth import authenticate, get_user_model -from django.contrib.auth.models import AbstractBaseUser, update_last_login +from django.contrib.auth.models import AbstractBaseUser from django.utils.translation import gettext_lazy as _ from rest_framework import exceptions, serializers from rest_framework.exceptions import ValidationError @@ -10,6 +10,7 @@ from .models import TokenUser from .settings import api_settings from .tokens import RefreshToken, SlidingToken, Token, UntypedToken +from django.contrib.auth.signals import user_logged_in AuthUser = TypeVar("AuthUser", AbstractBaseUser, TokenUser) @@ -78,7 +79,7 @@ def validate(self, attrs: Dict[str, Any]) -> Dict[str, str]: data["access"] = str(refresh.access_token) if api_settings.UPDATE_LAST_LOGIN: - update_last_login(None, self.user) + user_logged_in.send(sender=self.user.__class__, user=self.user) return data @@ -94,7 +95,7 @@ def validate(self, attrs: Dict[str, Any]) -> Dict[str, str]: data["token"] = str(token) if api_settings.UPDATE_LAST_LOGIN: - update_last_login(None, self.user) + user_logged_in.send(sender=self.user.__class__, user=self.user) return data From ee2410ec5515bd00211225f9e41c7f1522885c06 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 14 Sep 2023 22:57:55 +0000 Subject: [PATCH 2/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- rest_framework_simplejwt/serializers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rest_framework_simplejwt/serializers.py b/rest_framework_simplejwt/serializers.py index 55bc40be8..ec0e0d3fe 100644 --- a/rest_framework_simplejwt/serializers.py +++ b/rest_framework_simplejwt/serializers.py @@ -3,6 +3,7 @@ from django.conf import settings from django.contrib.auth import authenticate, get_user_model from django.contrib.auth.models import AbstractBaseUser +from django.contrib.auth.signals import user_logged_in from django.utils.translation import gettext_lazy as _ from rest_framework import exceptions, serializers from rest_framework.exceptions import ValidationError @@ -10,7 +11,6 @@ from .models import TokenUser from .settings import api_settings from .tokens import RefreshToken, SlidingToken, Token, UntypedToken -from django.contrib.auth.signals import user_logged_in AuthUser = TypeVar("AuthUser", AbstractBaseUser, TokenUser) From 8ed0c8f836b934da64892b2e3ed113f5352437cb Mon Sep 17 00:00:00 2001 From: Ou7law007 <64309267+Ou7law007@users.noreply.github.com> Date: Thu, 21 Sep 2023 13:34:55 +0200 Subject: [PATCH 3/5] Update serializers.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixed compatibility with `django-axes` error. ``` TypeError: AxesProxyHandler.user_logged_in() missing 1 required positional argument: 'request' ``` According to `django-axes`: ``` Authenticating usersĀ¶ Axes needs a request attribute to be supplied to the stock Django authenticate method in the django.contrib.auth module in order to function correctly. ``` https://django-axes.readthedocs.io/en/latest/3_usage.html --- rest_framework_simplejwt/serializers.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rest_framework_simplejwt/serializers.py b/rest_framework_simplejwt/serializers.py index ec0e0d3fe..0e5addaa2 100644 --- a/rest_framework_simplejwt/serializers.py +++ b/rest_framework_simplejwt/serializers.py @@ -79,7 +79,7 @@ def validate(self, attrs: Dict[str, Any]) -> Dict[str, str]: data["access"] = str(refresh.access_token) if api_settings.UPDATE_LAST_LOGIN: - user_logged_in.send(sender=self.user.__class__, user=self.user) + user_logged_in.send(sender=self.user.__class__, user=self.user, data=data, request=self.context.get('request')) return data @@ -95,7 +95,7 @@ def validate(self, attrs: Dict[str, Any]) -> Dict[str, str]: data["token"] = str(token) if api_settings.UPDATE_LAST_LOGIN: - user_logged_in.send(sender=self.user.__class__, user=self.user) + user_logged_in.send(sender=self.user.__class__, user=self.user, data=data, request=self.context.get('request')) return data From 19d8936dc74e11f3c2b86d365e2be341fd99de9e Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 21 Sep 2023 11:35:04 +0000 Subject: [PATCH 4/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- rest_framework_simplejwt/serializers.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/rest_framework_simplejwt/serializers.py b/rest_framework_simplejwt/serializers.py index 0e5addaa2..5d3d21124 100644 --- a/rest_framework_simplejwt/serializers.py +++ b/rest_framework_simplejwt/serializers.py @@ -79,7 +79,12 @@ def validate(self, attrs: Dict[str, Any]) -> Dict[str, str]: data["access"] = str(refresh.access_token) if api_settings.UPDATE_LAST_LOGIN: - user_logged_in.send(sender=self.user.__class__, user=self.user, data=data, request=self.context.get('request')) + user_logged_in.send( + sender=self.user.__class__, + user=self.user, + data=data, + request=self.context.get("request"), + ) return data @@ -95,7 +100,12 @@ def validate(self, attrs: Dict[str, Any]) -> Dict[str, str]: data["token"] = str(token) if api_settings.UPDATE_LAST_LOGIN: - user_logged_in.send(sender=self.user.__class__, user=self.user, data=data, request=self.context.get('request')) + user_logged_in.send( + sender=self.user.__class__, + user=self.user, + data=data, + request=self.context.get("request"), + ) return data From baf076d0014f12a70b6bcac8989859d311c19f62 Mon Sep 17 00:00:00 2001 From: Ou7law007 <64309267+Ou7law007@users.noreply.github.com> Date: Thu, 21 Sep 2023 15:03:34 +0200 Subject: [PATCH 5/5] Update serializers.py self.__class__ is more accurate. --- rest_framework_simplejwt/serializers.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rest_framework_simplejwt/serializers.py b/rest_framework_simplejwt/serializers.py index 5d3d21124..94bfce626 100644 --- a/rest_framework_simplejwt/serializers.py +++ b/rest_framework_simplejwt/serializers.py @@ -80,7 +80,7 @@ def validate(self, attrs: Dict[str, Any]) -> Dict[str, str]: if api_settings.UPDATE_LAST_LOGIN: user_logged_in.send( - sender=self.user.__class__, + sender=self.__class__, user=self.user, data=data, request=self.context.get("request"), @@ -101,7 +101,7 @@ def validate(self, attrs: Dict[str, Any]) -> Dict[str, str]: if api_settings.UPDATE_LAST_LOGIN: user_logged_in.send( - sender=self.user.__class__, + sender=self.__class__, user=self.user, data=data, request=self.context.get("request"),