kop narsalar qoshildi
This commit is contained in:
0
core/apps/authentication/__init__.py
Normal file
0
core/apps/authentication/__init__.py
Normal file
6
core/apps/authentication/apps.py
Normal file
6
core/apps/authentication/apps.py
Normal file
@@ -0,0 +1,6 @@
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class AuthenticationConfig(AppConfig):
|
||||
default_auto_field = 'django.db.models.BigAutoField'
|
||||
name = 'core.apps.authentication'
|
||||
0
core/apps/authentication/migrations/__init__.py
Normal file
0
core/apps/authentication/migrations/__init__.py
Normal file
5
core/apps/authentication/serializers/login.py
Normal file
5
core/apps/authentication/serializers/login.py
Normal file
@@ -0,0 +1,5 @@
|
||||
from rest_framework import serializers
|
||||
|
||||
|
||||
class LoginSerializer(serializers.Serializer):
|
||||
telegram_id = serializers.CharField()
|
||||
5
core/apps/authentication/serializers/response.py
Normal file
5
core/apps/authentication/serializers/response.py
Normal file
@@ -0,0 +1,5 @@
|
||||
from rest_framework import serializers
|
||||
|
||||
|
||||
class LoginResponseSerializer(serializers.Serializer):
|
||||
token = serializers.CharField(required=False, allow_null=True)
|
||||
8
core/apps/authentication/urls.py
Normal file
8
core/apps/authentication/urls.py
Normal file
@@ -0,0 +1,8 @@
|
||||
from django.urls import path
|
||||
|
||||
from core.apps.authentication.views import login
|
||||
|
||||
|
||||
urlpatterns = [
|
||||
path('login/', login.LoginApiView.as_view(), name='login-api'),
|
||||
]
|
||||
45
core/apps/authentication/views/login.py
Normal file
45
core/apps/authentication/views/login.py
Normal file
@@ -0,0 +1,45 @@
|
||||
# rest framework
|
||||
from rest_framework import generics
|
||||
# rest framework simple jwt
|
||||
from rest_framework_simplejwt.tokens import RefreshToken
|
||||
# drf yasg
|
||||
from drf_yasg.utils import swagger_auto_schema
|
||||
|
||||
# shared
|
||||
from core.apps.shared.utils.response_mixin import ResponseMixin
|
||||
from core.apps.shared.serializers.base import BaseResponseSerializer, SuccessResponseSerializer
|
||||
# accounts
|
||||
from core.apps.accounts.models import User
|
||||
# authentication
|
||||
from core.apps.authentication.serializers.login import LoginSerializer
|
||||
from core.apps.authentication.serializers import response as response_serializers
|
||||
|
||||
|
||||
class LoginApiView(generics.GenericAPIView, ResponseMixin):
|
||||
serializer_class = LoginSerializer
|
||||
queryset = User.objects.all()
|
||||
|
||||
@swagger_auto_schema(
|
||||
responses={
|
||||
200: SuccessResponseSerializer(data_serializer=response_serializers.LoginResponseSerializer()),
|
||||
400: BaseResponseSerializer(),
|
||||
500: BaseResponseSerializer(),
|
||||
}
|
||||
)
|
||||
def post(self, request):
|
||||
try:
|
||||
serializer = self.serializer_class(data=request.data)
|
||||
if serializer.is_valid(raise_exception=True):
|
||||
telegram_id = serializer.validated_data.get('telegram_id')
|
||||
user = User.objects.filter(telegram_id=telegram_id).first()
|
||||
if not user:
|
||||
return self.failure_response(message="User topilmadi")
|
||||
if not user.is_active:
|
||||
return self.failure_response(message="User tasdiqlanmagan")
|
||||
|
||||
token = RefreshToken.for_user(user)
|
||||
return self.success_response(data={'token': str(token.access_token)})
|
||||
|
||||
return self.failure_response(data=serializer.errors, message='siz tarafdan xatolik')
|
||||
except Exception as e:
|
||||
return self.error_response(data=str(e), message='xatolik')
|
||||
Reference in New Issue
Block a user