I have created the rest api for authentication of user, and in the response I am getting the token. I also want to add whether the user has staff permission or not, I have already got the information is serializers but I am not able to pass to the view.
And I need to authentication whether user is active or not This part is not working at all.
My serializer code :
class AuthTokenSerializer(serializers.Serializer):
"""Serializer for the user authentication object"""
email = serializers.CharField()
password = serializers.CharField(
style={'input_type': 'password'},
trim_whitespace=False
)
def validate(self, attrs):
"""Validate and authenticate the user"""
email = attrs.get('email')
password = attrs.get('password')
user = authenticate(
request=self.context.get('request'),
username=email,
password=password
)
#This part I am trying to authenticate whether the account is active or not
if user is not None:
if not user.is_active:
msg = _('The password is valid, but the account has been disabled! ')
raise serializers.ValidationError(msg, code='not_active')
if not user:
msg = _('Unable to authenticate with provided credentials')
raise serializers.ValidationError(msg, code='authorization')
attrs['user'] = user
attrs['is_staff'] = user.is_staff #Here I am getting the user has permission of staff or not.
return attrs
And the views.py is :
class CreateTokenView(ObtainAuthToken):
"""Create a new auth token for the user"""
serializer_class = AuthTokenSerializer
renderer_classes = api_settings.DEFAULT_RENDERER_CLASSES
models.py
class User(AbstractBaseUser, PermissionsMixin):
"""Custom user model that supports using email instead of username"""
email = models.EmailField(max_length=255, unique=True)
name = models.CharField(max_length=255)
image = models.ImageField(null=True, upload_to=user_image_file_path)
contact_no = models.CharField(max_length=255, default='')
is_active = models.BooleanField(default=True)
is_staff = models.BooleanField(default=False)
objects = UserManager()
USERNAME_FIELD = 'email'
How i can override my views so that I can get these two information. Any information will be great help. Thank you