class SignInView(View):
def post(self, request):
try:
data = json.loads(request.body)
user_email = data['email']
user_password = data['password']
user_id = User.objects.get(email=user_email)
access_token = jwt.encode({'id':user_id.id}, SECRET_KEY, algorithm=ALGORITHM)
#if not User.objects.filter(email=user_email).exists():
#return JsonResponse({'message':'INVALID_USER'}, status=401)
if bcrypt.checkpw(user_password.encode('utf-8'), user_id.password.encode('utf-8')) == False:
return JsonResponse({'message':'Password is incorrect'}, status=401)
return JsonResponse({'Access_token':access_token.decode('utf-8')}, status=200)
except KeyError:
return JsonResponse({'message':'KEY_ERROR'}, status=400)
except User.DoesNotExist:
return JsonResponse({'message':'Invalid Email Format'}, status=401)
class LogInView(View):
def post(self, request):
try:
data = json.loads(request.body)
user = User.objects.get(email=data["email"])
hashed_password = user.password.encode("utf-8")
if not bcrypt.checkpw(data["password"].encode("utf-8"), hashed_password):
return JsonResponse({"MESSAGE":"INVALID_USER"}, status=401)
access_token = jwt.encode({"id" : user.id}, SECRET_KEY, algorithm = ALGORITHM).decode('utf-8')
return JsonResponse({"MESSAGE":"SUCCESS", "ACCESS_TOKEN": access_token}, status=200)
except json.JSONDecodeError:
return JsonResponse({"MESSAGE": "JSONDecodeError"}, status=404)
except User.DoesNotExist:
return JsonResponse({"MESSAGE": "INVALID_USER"}, status=404)
except KeyError:
return JsonResponse({"MESSAGE" : "KEY_ERROR"}, status=400)
json.loads 를 쓸 때 발생하는데, 받는 값이 없거나 deserialize되고 있는 값이 json 형식이 아닐때 에러가 난다.
-> json 형식을 통과할 때 읽을 수 없기 때문에 발생하는 error 다(라고 해석가능?)