简体   繁体   中英

How to skip or remove password field in simplejwt token authentication in django rest framework?

My requirement is, I don't wanted to enter password in simplejwt token authentication. I have added one extra field in the authentication by inheriting the init() method of TokenObtainPairSerializer as per my requrements.

Currently, I am passing None as in password field but still its showing to user (djnago admin portal). What I want is, I don't wanted to show the password field to user while authentication using simplejwt.

below is my code,

from rest_framework_simplejwt.serializers import TokenObtainPairSerializer

class CustomSerializer(TokenObtainPairSerializer):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields[self.username_field] = serializers.CharField()
        self.fields['password'] = PasswordField(default=None)
        self.fields['extra'] = serializers.CharField()

    def validate(self, attrs):
        pass

Is there any ways to set PasswordField as unusable so it wont show to user?

I have followed the below mentioned process to solve the problem,

  1. Override the TokenObtainPairSerializer class __init__ method like below,

  2. Use del self.fields['password'] , so It wont ask you the password and add whatever fields you want.

class CustomSerializer(TokenObtainPairSerializer): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.fields[self.username_field] = serializers.CharField() del self.fields['password']

This works really well. There is a almost same question I have answered, You can check it here for more knowledge.

Let me know if anyone have better solution of this problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM