diff --git a/accounts/models.py b/accounts/models.py index 2ad4885..669e810 100644 --- a/accounts/models.py +++ b/accounts/models.py @@ -5,7 +5,7 @@ class User(AbstractUser): email = models.EmailField(unique=True, null=False, blank=False) - nickname = models.CharField( + name = models.CharField( max_length=20, unique=True, null=False, @@ -18,5 +18,8 @@ class User(AbstractUser): reliability = models.IntegerField(default=100) + USERNAME_FIELD = "email" + REQUIRED_FIELDS = [] + def __str__(self): - return self.nickname + return self.name diff --git a/accounts/serializers.py b/accounts/serializers.py index 3cdfc6a..0f9f7ab 100644 --- a/accounts/serializers.py +++ b/accounts/serializers.py @@ -25,7 +25,7 @@ class RegisterSerializer(serializers.ModelSerializer): class Meta: model = User - fields = ("id", "nickname", "email", "password", "checkPassword") + fields = ("id", "name", "email", "password", "checkPassword") def validate(self, data): # pw와 checkPw 확인 if data["password"] != data["checkPassword"]: @@ -35,10 +35,11 @@ def validate(self, data): # pw와 checkPw 확인 def create(self, validated_data): # CREATE 요청이 들어오면 create 매서드를 오버라이딩하여 유저와 토큰 생성 user = User.objects.create_user( - username=validated_data["nickname"], + username=validated_data["email"], email=validated_data["email"], ) + user.name = validated_data["name"] user.set_password(validated_data["password"]) user.save() Token.objects.create(user=user) @@ -47,7 +48,7 @@ def create(self, validated_data): class LoginSerializer(serializers.Serializer): - email = serializers.EmailField(required=True) + username = serializers.EmailField(required=True) # write_only=True 를 통해 클라이언트->서버 만 가능하도록 설정 password = serializers.CharField(required=True, write_only=True) diff --git a/accounts/urls.py b/accounts/urls.py index d03cf68..53295cc 100644 --- a/accounts/urls.py +++ b/accounts/urls.py @@ -1,6 +1,7 @@ from django.urls import path -from .views import RegisterView +from .views import RegisterView, LoginView urlpatterns = [ path("register/", RegisterView.as_view()), + path("login/", LoginView.as_view()), ] diff --git a/accounts/views.py b/accounts/views.py index 32ec125..521b38d 100644 --- a/accounts/views.py +++ b/accounts/views.py @@ -1,10 +1,21 @@ # from django.shortcuts import render from .models import User -from rest_framework import generics +from rest_framework import generics, status +from rest_framework.response import Response -from .serializers import RegisterSerializer +from .serializers import RegisterSerializer, LoginSerializer class RegisterView(generics.CreateAPIView): queryset = User.objects.all() serializer_class = RegisterSerializer + + +class LoginView(generics.GenericAPIView): + serializer_class = LoginSerializer + + def post(self, request): + serializer = self.get_serializer(data=request.data) + serializer.is_valid(raise_exception=True) + token = serializer.validated_data + return Response({"token": token.key}, status=status.HTTP_200_OK)