Skip to content

Commit

Permalink
♻️Refact : 사용자 로그인 api의 swagger 적용 및 리팩토링 #3
Browse files Browse the repository at this point in the history
  • Loading branch information
Chestnut90 committed Nov 15, 2023
1 parent c9a3616 commit b79543c
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 10 deletions.
11 changes: 10 additions & 1 deletion src/config/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
},
}
26 changes: 24 additions & 2 deletions src/users/serializers.py
Original file line number Diff line number Diff line change
@@ -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")
Expand All @@ -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()
5 changes: 2 additions & 3 deletions src/users/urls.py
Original file line number Diff line number Diff line change
@@ -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"),
]
19 changes: 15 additions & 4 deletions src/users/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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)

0 comments on commit b79543c

Please sign in to comment.