From 0e52537b89ceba0a9919a2a9417f36ec6df0ab22 Mon Sep 17 00:00:00 2001 From: behruz-dev Date: Sat, 30 Aug 2025 16:41:44 +0500 Subject: [PATCH] change: change loginserializer --- core/apps/accounts/models.py | 5 +++++ core/apps/accounts/serializers.py | 23 +++++++++++++++++++++-- core/apps/accounts/urls.py | 4 +--- core/apps/accounts/views.py | 7 ++++++- 4 files changed, 33 insertions(+), 6 deletions(-) diff --git a/core/apps/accounts/models.py b/core/apps/accounts/models.py index 8996696..172486c 100644 --- a/core/apps/accounts/models.py +++ b/core/apps/accounts/models.py @@ -21,6 +21,11 @@ class User(AbstractUser, BaseModel): def __str__(self): return self.email + + def save(self, *args, **kwargs): + if self.email: + self.email = self.email.lower() + return super().save(*args, **kwargs) class Meta: verbose_name = 'foydalanuvchi' diff --git a/core/apps/accounts/serializers.py b/core/apps/accounts/serializers.py index 4042714..7ccb065 100644 --- a/core/apps/accounts/serializers.py +++ b/core/apps/accounts/serializers.py @@ -1,4 +1,7 @@ from django.db import transaction +from django.contrib.auth import authenticate + +from rest_framework_simplejwt.serializers import TokenObtainPairSerializer from rest_framework import serializers @@ -35,7 +38,6 @@ class ConfirmUserSerializer(serializers.Serializer): if User.objects.filter(email=data['email']).exists(): raise serializers.ValidationError('User with this email already exists') user_data = get_user_credentials(email=data.get('email')) - print(user_data) if not user_data: raise serializers.ValidationError("User with this email not found") 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.save() return user - \ No newline at end of file + + +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) \ No newline at end of file diff --git a/core/apps/accounts/urls.py b/core/apps/accounts/urls.py index 3939359..5acd652 100644 --- a/core/apps/accounts/urls.py +++ b/core/apps/accounts/urls.py @@ -1,11 +1,9 @@ from django.urls import path, include -from rest_framework_simplejwt.views import TokenObtainPairView - from core.apps.accounts import views urlpatterns = [ - path('login/', TokenObtainPairView.as_view()), + path('login/', views.CustomTokenObtainPairView.as_view()), path('register/', views.RegisterApiView.as_view()), path('confirm_user/', views.ConfirmUserApiView.as_view()), ] \ No newline at end of file diff --git a/core/apps/accounts/views.py b/core/apps/accounts/views.py index a91e637..acbb761 100644 --- a/core/apps/accounts/views.py +++ b/core/apps/accounts/views.py @@ -4,6 +4,7 @@ from rest_framework import generics, views from rest_framework.response import Response 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 models @@ -51,4 +52,8 @@ class ConfirmUserApiView(generics.GenericAPIView): return Response( {'success': False, 'error_message': serializer.errors}, status=400 - ) \ No newline at end of file + ) + + +class CustomTokenObtainPairView(TokenObtainPairView): + serializer_class = serializers.CustomTokenObtainPairSerializer \ No newline at end of file