Skip to content

Commit

Permalink
추가: Login API 추가
Browse files Browse the repository at this point in the history
- simple-jwtd의 TokenObtainView 상속
  • Loading branch information
Sahayana committed Sep 30, 2023
1 parent 359b7fd commit be52088
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
36 changes: 34 additions & 2 deletions apps/account/v1/apis/user_api.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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)
3 changes: 2 additions & 1 deletion apps/account/v1/urls.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from django.urls import include, path
from django.urls import path

from apps.account.v1.apis import user_api

Expand All @@ -11,4 +11,5 @@
user_api.UserActivationView.as_view(),
name="user_activation",
),
path("login", user_api.LoginView.as_view(), name="login"),
]

0 comments on commit be52088

Please sign in to comment.