0
Hello, I’m a little lost when authenticating a user by Django-Rest-framework, I was following the tutorial of the channel Codingentrepreneurs in the video 32 of the Blog API (https://www.youtube.com/watch?v=jEXQqNtjNJc) it explains how to authenticate the user via token with Django-Rest-jwt, but I found it very confusing.. and there goes the question:
1 - How do I use the token to allow user access (login) and so that it can consume all my Apis on mobile?? My project is the same as the tutorial (part of the api)
py serializers.
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
How do I customize my token, to pass the information I want, for example by default it passes (user_id,username,email,Exp..) how do I pass to the token for example the user image and other user registration information?? so I can decode it later
– Guilherme Henrique
I edited it, see if it helps you!
– mcmartin