Advertisement
Guest User

Untitled

a guest
Apr 6th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. class CustomJWTSerializer(JSONWebTokenSerializer):
  2.  
  3. def validate(self, attrs):
  4.  
  5. password = attrs.get("password")
  6. user_obj = User.objects.filter(phone_number=attrs.get("phone_number")).first()
  7. if user_obj is not None:
  8. credentials = {
  9. 'phone_number': user_obj.phone_number,
  10. 'password': password
  11. }
  12. if all(credentials.values()):
  13. user = authenticate(**credentials)
  14. if user:
  15. if not user.is_active:
  16. msg = _('User account is disabled.')
  17. raise serializers.ValidationError(msg)
  18.  
  19. payload = jwt_payload_handler(user)
  20.  
  21. return {
  22. 'token': jwt_encode_handler(payload),
  23. 'user': user
  24. }
  25. else:
  26. msg = _('Unable to log in with provided credentials.')
  27. raise serializers.ValidationError(msg)
  28.  
  29. else:
  30. msg = _('Not enough data')
  31. raise serializers.ValidationError(msg)
  32.  
  33. else:
  34. msg = _('Account with this phone number does not exists')
  35. raise serializers.ValidationError(msg)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement