change: change loginserializer
This commit is contained in:
@@ -21,6 +21,11 @@ class User(AbstractUser, BaseModel):
|
|||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return self.email
|
return self.email
|
||||||
|
|
||||||
|
def save(self, *args, **kwargs):
|
||||||
|
if self.email:
|
||||||
|
self.email = self.email.lower()
|
||||||
|
return super().save(*args, **kwargs)
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
verbose_name = 'foydalanuvchi'
|
verbose_name = 'foydalanuvchi'
|
||||||
|
|||||||
@@ -1,4 +1,7 @@
|
|||||||
from django.db import transaction
|
from django.db import transaction
|
||||||
|
from django.contrib.auth import authenticate
|
||||||
|
|
||||||
|
from rest_framework_simplejwt.serializers import TokenObtainPairSerializer
|
||||||
|
|
||||||
from rest_framework import serializers
|
from rest_framework import serializers
|
||||||
|
|
||||||
@@ -35,7 +38,6 @@ class ConfirmUserSerializer(serializers.Serializer):
|
|||||||
if User.objects.filter(email=data['email']).exists():
|
if User.objects.filter(email=data['email']).exists():
|
||||||
raise serializers.ValidationError('User with this email already exists')
|
raise serializers.ValidationError('User with this email already exists')
|
||||||
user_data = get_user_credentials(email=data.get('email'))
|
user_data = get_user_credentials(email=data.get('email'))
|
||||||
print(user_data)
|
|
||||||
if not user_data:
|
if not user_data:
|
||||||
raise serializers.ValidationError("User with this email not found")
|
raise serializers.ValidationError("User with this email not found")
|
||||||
confirm_data = get_user_confirmation_code(data['email'], data['code'])
|
confirm_data = get_user_confirmation_code(data['email'], data['code'])
|
||||||
@@ -55,4 +57,21 @@ class ConfirmUserSerializer(serializers.Serializer):
|
|||||||
user.set_password(user_data.get('password'))
|
user.set_password(user_data.get('password'))
|
||||||
user.save()
|
user.save()
|
||||||
return user
|
return user
|
||||||
|
|
||||||
|
|
||||||
|
class CustomTokenObtainPairSerializer(TokenObtainPairSerializer):
|
||||||
|
username_field = "email"
|
||||||
|
|
||||||
|
def validate(self, attrs):
|
||||||
|
email = attrs.get("email", "").lower()
|
||||||
|
password = attrs.get("password")
|
||||||
|
user = authenticate(
|
||||||
|
request=self.context.get("request"),
|
||||||
|
email=email,
|
||||||
|
password=password
|
||||||
|
)
|
||||||
|
if not user:
|
||||||
|
raise serializers.ValidationError("Invalid email or password")
|
||||||
|
|
||||||
|
attrs["email"] = email
|
||||||
|
return super().validate(attrs)
|
||||||
@@ -1,11 +1,9 @@
|
|||||||
from django.urls import path, include
|
from django.urls import path, include
|
||||||
|
|
||||||
from rest_framework_simplejwt.views import TokenObtainPairView
|
|
||||||
|
|
||||||
from core.apps.accounts import views
|
from core.apps.accounts import views
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path('login/', TokenObtainPairView.as_view()),
|
path('login/', views.CustomTokenObtainPairView.as_view()),
|
||||||
path('register/', views.RegisterApiView.as_view()),
|
path('register/', views.RegisterApiView.as_view()),
|
||||||
path('confirm_user/', views.ConfirmUserApiView.as_view()),
|
path('confirm_user/', views.ConfirmUserApiView.as_view()),
|
||||||
]
|
]
|
||||||
@@ -4,6 +4,7 @@ from rest_framework import generics, views
|
|||||||
from rest_framework.response import Response
|
from rest_framework.response import Response
|
||||||
|
|
||||||
from rest_framework_simplejwt.tokens import RefreshToken
|
from rest_framework_simplejwt.tokens import RefreshToken
|
||||||
|
from rest_framework_simplejwt.views import TokenObtainPairView
|
||||||
|
|
||||||
from core.apps.accounts import serializers
|
from core.apps.accounts import serializers
|
||||||
from core.apps.accounts import models
|
from core.apps.accounts import models
|
||||||
@@ -51,4 +52,8 @@ class ConfirmUserApiView(generics.GenericAPIView):
|
|||||||
return Response(
|
return Response(
|
||||||
{'success': False, 'error_message': serializer.errors},
|
{'success': False, 'error_message': serializer.errors},
|
||||||
status=400
|
status=400
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class CustomTokenObtainPairView(TokenObtainPairView):
|
||||||
|
serializer_class = serializers.CustomTokenObtainPairSerializer
|
||||||
Reference in New Issue
Block a user