Showing
2 changed files
with
29 additions
and
1 deletions
| 1 | import json | 1 | import json |
| 2 | +import jwt | ||
| 2 | import uuid | 3 | import uuid |
| 4 | +from django.conf import settings | ||
| 3 | from django.contrib.auth.hashers import make_password, check_password | 5 | from django.contrib.auth.hashers import make_password, check_password |
| 4 | from django.core.exceptions import ValidationError | 6 | from django.core.exceptions import ValidationError |
| 5 | from django.core.validators import validate_email | 7 | from django.core.validators import validate_email |
| ... | @@ -63,7 +65,32 @@ def create(request): | ... | @@ -63,7 +65,32 @@ def create(request): |
| 63 | 65 | ||
| 64 | # 로그인 | 66 | # 로그인 |
| 65 | def login(request): | 67 | def login(request): |
| 66 | - return {'result': True} | 68 | + # Load |
| 69 | + try: | ||
| 70 | + received = json.loads(request.body.decode('utf-8')) | ||
| 71 | + except json.decoder.JSONDecodeError: | ||
| 72 | + return {'result': False, 'error': '입력이 잘못되었습니다.'} | ||
| 73 | + | ||
| 74 | + # Validate | ||
| 75 | + if 'email' not in received \ | ||
| 76 | + or 'password' not in received: | ||
| 77 | + return {'result': False, 'error': '입력이 누락되었습니다.'} | ||
| 78 | + | ||
| 79 | + # Select | ||
| 80 | + user = User.objects.filter(email=received['email']) | ||
| 81 | + | ||
| 82 | + # Not Exists | ||
| 83 | + if len(user) != 1: | ||
| 84 | + return {'result': False, 'error': '로그인에 실패하였습니다.'} | ||
| 85 | + | ||
| 86 | + # Check | ||
| 87 | + if check_password(received['password'], user[0].password) is False: | ||
| 88 | + return {'result': False, 'error': '로그인에 실패하였습니다.'} | ||
| 89 | + | ||
| 90 | + # Token Generate | ||
| 91 | + token = jwt.encode({'id': user[0].id}, key=settings.SECRET_KEY, algorithm='HS256') | ||
| 92 | + | ||
| 93 | + return {'result': True, 'token': token.decode('utf-8')} | ||
| 67 | 94 | ||
| 68 | 95 | ||
| 69 | # 회원정보 조회 | 96 | # 회원정보 조회 | ... | ... |
| ... | @@ -20,6 +20,7 @@ jmespath==0.10.0 | ... | @@ -20,6 +20,7 @@ jmespath==0.10.0 |
| 20 | pip-tools==5.1.2 | 20 | pip-tools==5.1.2 |
| 21 | placebo==0.9.0 | 21 | placebo==0.9.0 |
| 22 | pycparser==2.20 | 22 | pycparser==2.20 |
| 23 | +PyJWT==1.7.1 | ||
| 23 | PyMySQL==0.9.3 | 24 | PyMySQL==0.9.3 |
| 24 | python-dateutil==2.6.1 | 25 | python-dateutil==2.6.1 |
| 25 | python-slugify==4.0.0 | 26 | python-slugify==4.0.0 | ... | ... |
-
Please register or login to post a comment