Hello, I'm kind of lost as a user authentication by django-rest-framework, I was following the CodingEntrepreneurs channel tutorial in Blog API 32 video ( link ) it explains how to do user authentication via token with django-rest-jwt, but I found it very confusing .. and la goes the question:
1 - How do I use the token to allow user access (login) and to be able to consume all my APIs in mobile ?? My project is the same as the tutorial (part of api)
serializers.py
class UserLoginSerializer(ModelSerializer):
token = CharField(allow_blank=True, read_only=True)
username = CharField(label='Código do Usuário',
allow_blank=True, required=False)
class Meta:
model = User
fields = ['username', 'password', 'token']
extra_kwargs = {'password':
{'write_only': True
}
}
def validate(self, data):
user_obj = None
username = data.get('username', None)
password = data['password']
if not username:
raise ValidationError('Insira o Código de Usuário!')
user = User.objects.filter(
Q(username=username)
).distinct()
if user.exists() and user.count() == 1:
user_obj = user.first()
else:
raise ValidationError('Esse Código de Usuário não é válido!')
if user_obj:
if not user_obj.check_password(password):
raise ValidationError('Credenciais Incorretas!')
data['token'] = 'Some token Here'
return data