From f951e647a1b2d33e3ee014361fb75e7e385f8076 Mon Sep 17 00:00:00 2001 From: Saivarun Date: Sun, 16 Aug 2026 17:21:46 +0530 Subject: [PATCH] Implement login API for issue 143 --- backend/apps/authentication/serializers.py | 19 ++++++++++++++ backend/apps/authentication/urls.py | 1 + backend/apps/authentication/views.py | 29 +++++++++++++++++++++- 3 files changed, 48 insertions(+), 1 deletion(-) diff --git a/backend/apps/authentication/serializers.py b/backend/apps/authentication/serializers.py index dafc277..c371e8b 100644 --- a/backend/apps/authentication/serializers.py +++ b/backend/apps/authentication/serializers.py @@ -1,5 +1,7 @@ +from django.contrib.auth import authenticate from django.contrib.auth.models import User from rest_framework import serializers +from rest_framework_simplejwt.tokens import RefreshToken class RegisterSerializer(serializers.ModelSerializer): @@ -27,3 +29,20 @@ def create(self, validated_data): password=validated_data["password"], ) return user + + +class LoginSerializer(serializers.Serializer): + username = serializers.CharField() + password = serializers.CharField(write_only=True) + + def validate(self, attrs): + username = attrs.get("username") + password = attrs.get("password") + user = authenticate(username=username, password=password) + if user is None: + raise serializers.ValidationError("Invalid username or password") + refresh = RefreshToken.for_user(user) + attrs["user"] = user + attrs["access_token"] = str(refresh.access_token) + attrs["refresh_token"] = str(refresh) + return attrs diff --git a/backend/apps/authentication/urls.py b/backend/apps/authentication/urls.py index b82aca0..fa61965 100644 --- a/backend/apps/authentication/urls.py +++ b/backend/apps/authentication/urls.py @@ -4,4 +4,5 @@ urlpatterns = [ path("register/", views.RegisterationView.as_view(), name="register"), + path("login/", views.LoginView.as_view(), name="login"), ] diff --git a/backend/apps/authentication/views.py b/backend/apps/authentication/views.py index c2bd20b..9375529 100644 --- a/backend/apps/authentication/views.py +++ b/backend/apps/authentication/views.py @@ -4,7 +4,7 @@ from rest_framework_simplejwt.tokens import RefreshToken from .models import UserProfile -from .serializers import RegisterSerializer +from .serializers import LoginSerializer, RegisterSerializer class RegisterationView(generics.CreateAPIView): @@ -35,3 +35,30 @@ def create(self, request, *args, **kwargs): }, status=status.HTTP_201_CREATED, ) + + +class LoginView(generics.CreateAPIView): + serializer_class = LoginSerializer + + def create(self, request, *args, **kwags): + serializer = self.get_serializer(data=request.data) + serializer.is_valid(raise_exception=True) + user = serializer.validated_data["user"] + profile = UserProfile.objects.get(user=user) + return Response( + { + "success": True, + "data": { + "tokens": { + "access_token": serializer.validated_data["access_token"], + "refresh_token": serializer.validated_data["refresh_token"], + }, + "user": { + "id": str(profile.id), + "username": user.username, + "created_at": user.date_joined, + }, + }, + }, + status=status.HTTP_200_OK, + )