728x90
Django Backend Westargram 3장(로그인 구현)
2장에서 회원가입을 구현했으면 이제 로그인을 구현해보자.
1. views.py 및 urls.py내용 추가
로그인은 어떤 기능들을 설정해야하는지 우선 생각을 해보자.
- 인스타는 Email 또는 핸드폰 번호를 사용해서 ID로 사용이 가능하다.
- ID를 입력했을때 아이디가 맞는지 확인
- ID가 맞으면 Password가 맞는지 확인
위 3가지를 코드로 작성 해보자.
아래와 같은 내용을 추가해주자
최상단 import my_settings를 추가
import json, re, traceback, bcrypt, jwt, my_settings
class LoginView(View):
def post(self, request):
data = json.loads(request.body)
try :
email = data.get('email')
phone = data.get('phone')
password = data.get('password')
# 입력한 값이 Email인지 핸드폰 번호인지 검사
if Account.objects.filter(Q(email=email) | Q(phone=phone)).exists():
# 입력한 값이 Email 또는 핸드폰이면 DB에서 확인을 하여 해당 계정의 아이디 값을 account에 저장
account = Account.objects.get(Q(email=email) | Q(phone=phone))
# Password 검사 및 암호화
if bcrypt.checkpw(password.encode('utf-8'), account.password.encode('utf-8')):
token = jwt.encode({'email' : email}, my_settings.SECRET['secret'], algorithm = 'HS256')
# 성공시 200 return
return JsonResponse({'message' : 'SUCCESS'}, status=200)
# Password 틀렸을시 return
return JsonResponse({"message": "INVALID_PASSWORD"}, status=401)
# ID 틀렸을시 return
return JsonResponse({"message": "INVALID_USER"}, status=401)
# 다른 값을 입력했을시 return
except KeyError:
return JsonResponse({"message": "KEY_ERROR"}, status=400)
위 와 같이 작성을 했으면 이제 urls.py에서 LoginView를 찾을수 있도록 등록을 해주자.
from django.urls import path
from users.views import SignUpView, LoginView
urlpatterns = [
path('signup', SignUpView.as_view()),
path('login', LoginView.as_view()),
]
여기까지 하면 모든 준비는 끝이 난다.
확인을 해보자.
http POST 127.0.0.1:8000/users/login email="test@gmail.com" password="123456781"
HTTP/1.1 200 OK
Content-Length: 22
Content-Type: application/json
Date: Thu, 04 Feb 2021 11:06:55 GMT
Referrer-Policy: same-origin
Server: WSGIServer/0.2 CPython/3.8.5
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
{
"message": "SUCCESS"
}
기존에 만들어 놓은것으로 확인해보니 잘 된다.
핸드폰 번호로도 해보자.
http POST 127.0.0.1:8000/users/login phone="01020304050" password="123456781"
HTTP/1.1 200 OK
Content-Length: 22
Content-Type: application/json
Date: Thu, 04 Feb 2021 11:07:55 GMT
Referrer-Policy: same-origin
Server: WSGIServer/0.2 CPython/3.8.5
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
{
"message": "SUCCESS"
}
잘 된다.
이제 실패도 해보자.
비밀번호를 틀려보자.
http POST 127.0.0.1:8000/users/login phone="01020304050" password="12345678"
HTTP/1.1 401 Unauthorized
Content-Length: 31
Content-Type: application/json
Date: Thu, 04 Feb 2021 11:08:39 GMT
Referrer-Policy: same-origin
Server: WSGIServer/0.2 CPython/3.8.5
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
{
"message": "INVALID_PASSWORD"
}
아이디를 틀려보자.
http POST 127.0.0.1:8000/users/login phone="0102030405" password="12345678"
HTTP/1.1 401 Unauthorized
Content-Length: 27
Content-Type: application/json
Date: Thu, 04 Feb 2021 11:09:35 GMT
Referrer-Policy: same-origin
Server: WSGIServer/0.2 CPython/3.8.5
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
{
"message": "INVALID_USER"
}
잘된다.
728x90
'Python > Django기초' 카테고리의 다른 글
Python Module & Packages (0) | 2021.09.09 |
---|---|
Django의 코딩 스타일 (0) | 2021.09.09 |
Django Backend Westargram 2장 (Mysql설정 및 회원가입 구현) (0) | 2021.09.09 |
Django Backend Westargram 1장 (기초 설정) (0) | 2021.09.09 |
Django의 기초 (0) | 2021.09.09 |