在 Django 中记录 JWT 刷新时间

问题描述 投票:0回答:0

我想记录用户在 Django 中点击我的

/jwt/auth
端点的日期时间。我正在使用 Djoser 和
django.contrib.auth.backends.ModelBackend
后端。

我试图通过像这样扩展身份验证后端来做到这一点:

class CustomAuthenticationBackend(ModelBackend):
    def __init__(self, *args, **kwargs):
        print("hi mom")
        super().__init__(*args, **kwargs)

    def authenticate(self, request, username=None, password=None):
        user = super().authenticate(request, username, password)
        try:    
            if user and user.is_authenticated:
                user.last_login = datetime.now()
                user.save()
            return user

        except User.DoesNotExist:
            return None

发生的事情是从

user
返回的
authenticate(...)
None
- 所以我当然不能输入我的 if 语句。

我相信这是因为我提出的请求本身是未经身份验证的,因为它是一个身份验证请求,即使在获得响应的过程中,我们确实对用户进行了身份验证,或者至少验证了他们的 JWT。

在此调用刷新期间,用户实际引用在哪里?我可以利用它来进行我想要的用户更新吗?

django django-rest-framework-simplejwt djoser
© www.soinside.com 2019 - 2024. All rights reserved.