SMART-31 created serializer for user login
This commit is contained in:
parent
6002ba94ec
commit
837709d553
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,3 +1,4 @@
|
||||
.idea/
|
||||
*.iml
|
||||
.idea
|
||||
*__pycache__/
|
||||
|
@ -1,11 +1,17 @@
|
||||
"""
|
||||
@author p.dolata
|
||||
"""
|
||||
from django.contrib.auth import authenticate
|
||||
from django.contrib.auth.models import update_last_login
|
||||
from rest_framework import serializers
|
||||
from rest_framework_jwt.settings import api_settings
|
||||
|
||||
from smartpicasso.app.user.models import User
|
||||
from smartpicasso.app.user_profile.models import UserProfile
|
||||
|
||||
JWT_PAYLOAD_HANDLER = api_settings.JWT_PAYLOAD_HANDLER
|
||||
JWT_ENCODE_HANDLER = api_settings.JWT_ENCODE_HANDLER
|
||||
|
||||
|
||||
class UserSerializer(serializers.ModelSerializer):
|
||||
"""
|
||||
@ -38,3 +44,27 @@ class UserRegistrationSerializer(serializers.ModelSerializer):
|
||||
phone_number=profile_data['phone_number'],
|
||||
)
|
||||
return user
|
||||
|
||||
|
||||
class UserLoginSerializer(serializers.Serializer):
|
||||
"""
|
||||
Class to manage serializing user during singing in
|
||||
"""
|
||||
email = serializers.CharField(max_length=255)
|
||||
password = serializers.CharField(max_length=128, write_only=True)
|
||||
token = serializers.CharField(max_length=255, read_only=True)
|
||||
|
||||
def validate(self, data):
|
||||
email = data.get('email', None)
|
||||
password = data.get('password', None)
|
||||
user = authenticate(email=email, password=password)
|
||||
if user is None:
|
||||
raise serializers.ValidationError('An user with provided email and password is not found')
|
||||
try:
|
||||
payload = JWT_PAYLOAD_HANDLER(user)
|
||||
jwt_token = JWT_ENCODE_HANDLER(payload)
|
||||
update_last_login(None, user)
|
||||
except User.DoesNotExist:
|
||||
raise serializers.ValidationError('User with given email and password does not exist')
|
||||
|
||||
return {'email': user.email, 'token': jwt_token}
|
||||
|
Loading…
Reference in New Issue
Block a user