I'm working on creating a custom login API with phon number using django-rest-auth package. I am just using rest_auth.views.LoginView in my code to generate token for token authentication.
this is my serializer:
class LoginUserSerializer(serializers.Serializer):
phone = serializers.CharField()
password = serializers.CharField(
style={'input_type': 'password'}, trim_whitespace=False)
def validate(self, attrs):
phone = attrs.get('phone')
password = attrs.get('password')
if phone and password:
if User.objects.filter(phone=phone).exists():
user = authenticate(request=self.context.get('request'),
phone=phone, password=password)
else:
msg = {'detail': 'Phone number is not registered.',
'register': False}
raise serializers.ValidationError(msg)
if not user:
msg = {
'detail': 'Unable to log in with provided credentials.', 'register': True}
raise serializers.ValidationError(msg, code='authorization')
else:
msg = 'Must include "username" and "password".'
raise serializers.ValidationError(msg, code='authorization')
attrs['user'] = user
return attrs
and this is my view:
from rest_auth.views import LoginView as RestLoginView
class Login(RestLoginView):
permission_classes = (permissions.AllowAny,)
def post(self, request, *args, **kwargs):
serializer = LoginUserSerializer(data=request.data)
serializer.is_valid(raise_exception=True)
user = serializer.validated_data['user']
login(request, user)
return super().post(request, format=None)
when I run the server I have this page and I don't want to have username and email fields. instead of these, I want phone number. how do I can fix this????
