Skip to content

Commit 1e2b8b5

Browse files
authored
Merge pull request #10 from SeongHunTed/backend
feat : 로그인 Token 발급
2 parents cc7a584 + 255b0d3 commit 1e2b8b5

File tree

11 files changed

+52
-61
lines changed

11 files changed

+52
-61
lines changed
5 Bytes
Binary file not shown.
268 Bytes
Binary file not shown.

Backend/momment/accounts/migrations/0002_auto_20230106_0802.py renamed to Backend/momment/accounts/migrations/0002_auto_20230111_1443.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Generated by Django 3.2.13 on 2023-01-06 08:02
1+
# Generated by Django 3.2.13 on 2023-01-11 14:43
22

33
from django.db import migrations, models
44

Backend/momment/accounts/migrations/0003_alter_user_birth.py

Lines changed: 0 additions & 18 deletions
This file was deleted.

Backend/momment/accounts/migrations/0004_alter_user_birth.py

Lines changed: 0 additions & 18 deletions
This file was deleted.

Backend/momment/accounts/models.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
from django.db import models
22
from django.contrib.auth.models import BaseUserManager, AbstractBaseUser
33

4-
class Manager(BaseUserManager):
4+
5+
class Account(BaseUserManager):
56

67
def create_user(self, email, name, birth, digit, address, password=None):
78
if not email:
@@ -47,10 +48,10 @@ class User(AbstractBaseUser):
4748
create_at = models.DateTimeField(verbose_name='date joined', auto_now_add=True)
4849
last_login = models.DateTimeField(verbose_name='last login', auto_now=True)
4950

50-
object = Manager()
51+
object = Account()
5152

5253
USERNAME_FIELD = 'email'
53-
REQUIRED_FIELDS = ['name', 'digit', 'address']
54+
REQUIRED_FIELDS = ['name', 'digit', 'address', 'birth']
5455

5556
def __str__(self):
5657
return self.email
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from rest_framework.serializers import ModelSerializer, ReadOnlyField
2+
from .models import Account
3+
4+
class LoginSerializer(ModelSerializer):
5+
6+
class Meta:
7+
model = Account
8+
fields = ['email', 'name']

Backend/momment/accounts/views.py

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
1-
from django.shortcuts import render
2-
from django.conf import settings
3-
from django.shortcuts import redirect
4-
from rest_framework.views import APIView
5-
from rest_framework.permissions import AllowAny
6-
from rest_framework.response import Response
1+
# 커스텀 로그인
72
from django.http import HttpResponse, JsonResponse
8-
93
from django.views.decorators.csrf import csrf_exempt
10-
from accounts.models import User, Manager
4+
from accounts.models import User, Account
115
import requests, json, re
12-
from django.contrib.auth import authenticate
13-
6+
from django.contrib.auth.hashers import check_password
147
from rest_framework import status
158
from json.decoder import JSONDecodeError
9+
from rest_framework.authtoken.models import Token
10+
from rest_framework.permissions import IsAuthenticated
11+
12+
13+
# 카카오 로그인
14+
from rest_framework.views import APIView
15+
from django.conf import settings
16+
from django.shortcuts import redirect
17+
from rest_framework.response import Response
1618

1719
# Create your views here.
1820

@@ -43,8 +45,10 @@ def signup(request):
4345
if not re.match(regex_password, password):
4446
return JsonResponse({'message' : 'INVALID_PASSWORD'}, status = 400)
4547

46-
User.object.create_user(email=email, password=password, name=name, digit=digit, birth=birth, address=address)
47-
return JsonResponse({'message' : 'SUCCESS'}, status=201)
48+
user = User.object.create_user(email=email, password=password, name=name, digit=digit, birth=birth, address=address)
49+
50+
token = Token.objects.create(user=user)
51+
return JsonResponse({'message' : 'SUCCESS', 'token' : token.key}, status=200)
4852

4953
except KeyError:
5054
return JsonResponse({'message' : 'KEY_ERROR'}, status=400)
@@ -57,9 +61,14 @@ def login(request):
5761
email = data['email']
5862
password = data['password']
5963

60-
User.object.filter(email=email).
64+
user = User.object.filter(email=email)
65+
66+
if not check_password(password, user.get().password):
67+
return JsonResponse({'message' : 'WRONG_PASSWORD'})
68+
69+
token = Token.objects.get(user=user.get())
6170

62-
return JsonResponse({'message' : 'LOGIN_SUCCESS'})
71+
return JsonResponse({'message' : 'LOGIN_SUCCESS', 'token' : token.key}, status=200)
6372
except:
6473
return JsonResponse({'message' : 'LOGIN_FAILED'})
6574

Backend/momment/db.sqlite3

-4 KB
Binary file not shown.
127 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)