Skip to content

Commit

Permalink
feat: 로그인 기능 추가
Browse files Browse the repository at this point in the history
로그인 기능 추가
입력한 이메일, 비밀번호와 일치하는 유저가 존재하면 해당 유저의 토큰 반환
  • Loading branch information
ybkang1108 committed Jul 27, 2024
1 parent f8d84b8 commit bf67c6c
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 8 deletions.
7 changes: 5 additions & 2 deletions accounts/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
7 changes: 4 additions & 3 deletions accounts/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]:
Expand All @@ -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)
Expand All @@ -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)

Expand Down
3 changes: 2 additions & 1 deletion accounts/urls.py
Original file line number Diff line number Diff line change
@@ -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()),
]
15 changes: 13 additions & 2 deletions accounts/views.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit bf67c6c

Please sign in to comment.