From b79543c681aac5665920b076ceee6880cdecc1e2 Mon Sep 17 00:00:00 2001 From: chestnut90 Date: Wed, 15 Nov 2023 17:46:35 +0900 Subject: [PATCH] =?UTF-8?q?:recycle:Refact=20:=20=EC=82=AC=EC=9A=A9?= =?UTF-8?q?=EC=9E=90=20=EB=A1=9C=EA=B7=B8=EC=9D=B8=20api=EC=9D=98=20swagge?= =?UTF-8?q?r=20=EC=A0=81=EC=9A=A9=20=EB=B0=8F=20=EB=A6=AC=ED=8C=A9?= =?UTF-8?q?=ED=86=A0=EB=A7=81=20#3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/config/settings.py | 11 ++++++++++- src/users/serializers.py | 26 ++++++++++++++++++++++++-- src/users/urls.py | 5 ++--- src/users/views.py | 19 +++++++++++++++---- 4 files changed, 51 insertions(+), 10 deletions(-) diff --git a/src/config/settings.py b/src/config/settings.py index 76e8191..2f7fd98 100644 --- a/src/config/settings.py +++ b/src/config/settings.py @@ -153,4 +153,13 @@ # Rest Framework -REST_FRAMEWORK = {"DEFAULT_AUTHENTICATION_CLASSES": ("rest_framework_simplejwt.authentication.JWTAuthentication",)} +REST_FRAMEWORK = { + "DEFAULT_AUTHENTICATION_CLASSES": ("rest_framework_simplejwt.authentication.JWTAuthentication",), +} + +# Swagger +SWAGGER_SETTINGS = { + "SECURITY_DEFINITIONS": { + "Bearer": {"type": "apiKey", "name": "Authorization", "in": "header"}, + }, +} diff --git a/src/users/serializers.py b/src/users/serializers.py index f8542ff..ce5366b 100644 --- a/src/users/serializers.py +++ b/src/users/serializers.py @@ -1,9 +1,9 @@ from django.contrib.auth import get_user_model from django.contrib.auth.password_validation import validate_password -from rest_framework.serializers import ModelSerializer +from rest_framework import serializers -class UserSignUpSerializer(ModelSerializer): +class UserSignUpSerializer(serializers.ModelSerializer): class Meta: model = get_user_model() fields = ("username", "password") @@ -19,3 +19,25 @@ def create(self, validated_data): user.set_password(raw_password) user.save() return user + + +class SignInRequestSerializer(serializers.Serializer): + username = serializers.CharField() + password = serializers.CharField() + + def create(self, validated_data): + raise NotImplementedError() + + def update(self, instance, validated_data): + raise NotImplementedError() + + +class SignInResponseSerializer(serializers.Serializer): + access = serializers.CharField() + refresh = serializers.CharField() + + def create(self, validated_data): + raise NotImplementedError() + + def update(self, instance, validated_data): + raise NotImplementedError() diff --git a/src/users/urls.py b/src/users/urls.py index 4a2c781..8283408 100644 --- a/src/users/urls.py +++ b/src/users/urls.py @@ -1,9 +1,8 @@ from django.urls import path -from rest_framework_simplejwt.views import TokenObtainPairView -from users.views import SignUpAPIView +from users.views import SignInAPIView, SignUpAPIView urlpatterns = [ path("signup/", view=SignUpAPIView.as_view(), name="signup"), - path("signin/", view=TokenObtainPairView.as_view(), name="signin"), + path("signin/", view=SignInAPIView.as_view(), name="signin"), ] diff --git a/src/users/views.py b/src/users/views.py index 9828bbf..af4da48 100644 --- a/src/users/views.py +++ b/src/users/views.py @@ -2,8 +2,13 @@ from rest_framework import status from rest_framework.response import Response from rest_framework.views import APIView +from rest_framework_simplejwt.views import TokenObtainPairView -from users.serializers import UserSignUpSerializer +from users.serializers import ( + SignInRequestSerializer, + SignInResponseSerializer, + UserSignUpSerializer, +) class SignUpAPIView(APIView): @@ -26,9 +31,15 @@ def post(self, request): ) -class SignInAPIView(APIView): +class SignInAPIView(TokenObtainPairView): @swagger_auto_schema( operation_summary="사용자 로그인", + request_body=SignInRequestSerializer, + responses={ + status.HTTP_200_OK: SignInResponseSerializer, + status.HTTP_400_BAD_REQUEST: "error", + status.HTTP_401_UNAUTHORIZED: "unauthorized", + }, ) - def post(self, request): - return + def post(self, request, *args, **kwargs): + return super().post(request, *args, **kwargs)