简体   繁体   English

DRF 简单 jwt 从用户实例获取身份验证令牌或更改用户名和密码组合

[英]DRF simple jwt getting auth token from a user instance OR change username and password combination

I have a chat app that uses the same authentication method as in WhatsApp.我有一个聊天应用程序,它使用与 WhatsApp 中相同的身份验证方法。

Normally I would get the jwt auth token with this:通常我会通过以下方式获得 jwt 身份验证令牌:

from rest_framework_simplejwt.views import TokenObtainView
path('api/token/', TokenObtainView.as_view(), name='token_obtain_pair'),

However This asks for both username and password while my user model is using phone_number and a validation code instead.但是,当我的用户 model 使用 phone_number 和验证码时,这会同时询问用户名和密码。 Is there any way to change this?有什么办法可以改变这个吗?

username ======> phone_number password ======> phone_code (The validation code) username ======> phone_number password ======> phone_code (验证码)

The second way Is to pass the user instance and get the auth token from it somehow 第二种方法是传递用户实例并以某种方式从中获取身份验证令牌

my Login view我的登录视图

class User(AbstractUser):
    phone_number = PhoneNumberField(unique=True)
    username = models.CharField(max_length=30)
    password = models.CharField(null=True, blank=True, max_length=20)
    about = models.CharField(
        max_length=190, default="Hi, I use Orgachat!", blank=True, null=True)
    ...
    phone_code = models.IntegerField(blank=True, null=True)    
    USERNAME_FIELD = 'phone_number'
    

    def __str__(self):
        return str(self.username)

# Save auth token, I know this should be in signals.py but whatever    
@receiver(post_save, sender=User)
def create_auth_token(sender, instance=None, created=False, **kwargs):
    if created:
        Token.objects.create(user=instance)

my user model:我的用户 model:

 class User(AbstractUser): phone_number = PhoneNumberField(unique=True) username = models.CharField(max_length=30) password = models.CharField(null=True, blank=True, max_length=20) about = models.CharField( max_length=190, default="Hi, I use Orgachat,", blank=True. null=True)... phone_code = models,IntegerField(blank=True: null=True) USERNAME_FIELD = 'phone_number' def __str__(self). return str(self,username) # Save auth token. I know this should be in signals,py but whatever @receiver(post_save, sender=User) def create_auth_token(sender, instance=None, created=False: **kwargs): if created. Token.objects.create(user=instance)
from rest_framework_simplejwt.tokens import RefreshToken, SlidingToken, UntypedToken
from django.conrtib.auth.models import User

user = User.objects.first()
SlidingToken.for_user(user)
RefreshToken.for_user(user)
str(RefreshToken.for_user(user))
str(RefreshToken.for_user(user).access_token)

You can get the user jwt token from user instance using this.您可以使用此从用户实例获取用户 jwt 令牌。 Hope this will work for you.希望这对你有用。

I think it's better to write your custom backend class and add it to Django this is intended to allow different ways to authenticate, see我认为最好编写自定义后端 class 并将其添加到 Django 这是为了允许不同的身份验证方式, 请参阅

Also, you can inherit from TokenObtainView and add your logic there and use this custom class instead, but I prefer to integrate the first option with JWT so you need to read the docs and see how it works.此外,您可以从TokenObtainView继承并在那里添加您的逻辑并使用此自定义 class 代替,但我更喜欢将第一个选项与 JWT 集成,因此您需要阅读文档并了解它是如何工作的。

I just realised that django-rest-framework-jwt tag is there, you should drop using this package as it's unmaintained , use django-rest-framework-simplejwt instead我刚刚意识到django-rest-framework-jwt标签在那里,你应该放弃使用这个 package 因为它没有维护,而是使用django-rest-framework-simplejwt

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 DFR 简单 jwt 从用户实例而不是用户名/密码获取 authToken - DFR simple jwt get authToken from a user instance instead of username/password 无法使用用户名/密码登录令牌端点:drf 令牌身份验证和自定义用户 model、Django - Cannot log in to token endpoint with username/password: drf token auth and custom user model, Django DRF 简单 jwt。 如何更改来自 TokenObtainPairView 的响应以获取访问令牌过期时间 - DRF simple jwt. How to change response from TokenObtainPairView to get an access token EXPIRES time 如何在 drf 中通过访问 jwt 令牌向用户发送它 - how to send user it with access jwt token in drf DRF Simple JWT 没有登录用户? - DRF Simple JWT does not log in user? Drf 如何:没有 USERNAME_FIELD 的 simple-jwt 身份验证 - Drf how to: simple-jwt authenticating without the USERNAME_FIELD 何时在 DRF 中的 jwt 中调用 gettoken、刷新令牌和验证令牌? - When to call gettoken, refresh token and verify token by user in jwt in DRF? 从auth用户重新使用更改密码 - Reuse change password from auth User 在 Django Simple JWT 的 TokenRefreshView 中从刷新令牌中获取用户 - Get user from refresh token in Django Simple JWT's TokenRefreshView 更新用户名时如何防止 JWT Django (all-auth) 更改令牌? - How to prevent JWT Django (all-auth) from changing token when its username is updated?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM