Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class CustomJWTSerializer(JSONWebTokenSerializer):
- def validate(self, attrs):
- password = attrs.get("password")
- user_obj = User.objects.filter(phone_number=attrs.get("phone_number")).first()
- if user_obj is not None:
- credentials = {
- 'phone_number': user_obj.phone_number,
- 'password': password
- }
- if all(credentials.values()):
- user = authenticate(**credentials)
- if user:
- if not user.is_active:
- msg = _('User account is disabled.')
- raise serializers.ValidationError(msg)
- payload = jwt_payload_handler(user)
- return {
- 'token': jwt_encode_handler(payload),
- 'user': user
- }
- else:
- msg = _('Unable to log in with provided credentials.')
- raise serializers.ValidationError(msg)
- else:
- msg = _('Not enough data')
- raise serializers.ValidationError(msg)
- else:
- msg = _('Account with this phone number does not exists')
- raise serializers.ValidationError(msg)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement