From be5208872f2b5ba4d4736e94ad1e8429cc6131f1 Mon Sep 17 00:00:00 2001 From: sahayana Date: Sat, 30 Sep 2023 19:30:36 +0900 Subject: [PATCH] =?UTF-8?q?=EC=B6=94=EA=B0=80:=20Login=20API=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80=20-=20simple-jwtd=EC=9D=98=20TokenObtainView=20?= =?UTF-8?q?=EC=83=81=EC=86=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/account/v1/apis/user_api.py | 36 ++++++++++++++++++++++++++++++-- apps/account/v1/urls.py | 3 ++- 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/apps/account/v1/apis/user_api.py b/apps/account/v1/apis/user_api.py index b4c6c87..76bf9d7 100644 --- a/apps/account/v1/apis/user_api.py +++ b/apps/account/v1/apis/user_api.py @@ -1,5 +1,7 @@ -from rest_framework import generics, permissions, renderers, status, views +from rest_framework import generics, permissions, renderers, status from rest_framework.response import Response +from rest_framework_simplejwt.exceptions import InvalidToken, TokenError +from rest_framework_simplejwt.views import TokenObtainPairView from apps.account.constants import EMAIL_DUPLICATION_MESSAGE from apps.account.models import CustomUser @@ -30,7 +32,6 @@ def get(self, request, *args, **kwargs): """ 회원가입 페이지를 렌더링합니다. """ - self.renderer_classes = [renderers.TemplateHTMLRenderer] return Response(template_name="account/signup.html", status=status.HTTP_200_OK) def post(self, request, *args, **kwargs): @@ -91,3 +92,34 @@ def get(self, request, *args, **kwargs): } return Response(data=data, status=status.HTTP_202_ACCEPTED) + + +class LoginView(TokenObtainPairView): + + permission_classes = [permissions.AllowAny] + + def get_renderers(self): + if self.request.method == "GET": + renderer_classes = [renderers.TemplateHTMLRenderer] + else: + renderer_classes = [renderers.JSONRenderer] + return [renderer() for renderer in renderer_classes] + + def get(self, request, *args, **kwargs): + """ + 로그인 페이지를 렌더링합니다. + """ + return Response(template_name="account/login.html", status=status.HTTP_200_OK) + + def post(self, request, *args, **kwargs): + """ + 이메일, 패스워드로 로그인하여 access token을 반환합니다. + """ + serializer = self.get_serializer(data=request.data) + + try: + serializer.is_valid(raise_exception=True) + except TokenError as e: + raise InvalidToken(e.args[0]) + + return Response(data=serializer.validated_data, status=status.HTTP_200_OK) diff --git a/apps/account/v1/urls.py b/apps/account/v1/urls.py index 8a1b324..640dda9 100644 --- a/apps/account/v1/urls.py +++ b/apps/account/v1/urls.py @@ -1,4 +1,4 @@ -from django.urls import include, path +from django.urls import path from apps.account.v1.apis import user_api @@ -11,4 +11,5 @@ user_api.UserActivationView.as_view(), name="user_activation", ), + path("login", user_api.LoginView.as_view(), name="login"), ]