logitech 회원가입/로그인 view 작성
1차프로젝트에서는 회원가입/로그인 기능 구현 부분 담당을 하게되어 views.py를 작성하게
됐습니다.
import json import bcrypt import jwt from django.views import View from django.http import JsonResponse, HttpResponse from account.models import Account from django.db import IntegrityError class SignUpView(View): def post(self, request): data = json.loads(request.body) try: if '@' not in data['email']: return JsonResponse( {"message":"Email_Not_Verified"}, status = 404 ) elif len(data['email']) < 3: return JsonResponse( {"message":"Email_length_short"} ) elif len(data['password']) < 5: return JsonResponse( {"message":"Password_Not_Verified"}, status = 411 ) if Account.objects.filter(email = data['email']).exists(): return JsonResponse({"message":"ACCOUNT_ALREADY_EXIST"},stauts=401) hashed_password = bcrypt.hashpw(data['password'].encode('utf-8'),bcrypt.gensalt()).decode('utf-8') Account( email = data['email'], password = hashed_password, # name = data['name'], # language = data['language'], # country = data['country'], # birthday = data['birthday'], # phone = data['phone'], ).save() return JsonResponse({"message":"SUCCESS"}, status =200) except IntegrityError: return JsonResponse( {"message":"Data_Already_Exists"}, status = 409 ) except KeyError: return JsonResponse({'message':"INVALID_KEYS"},status=400) class SignInView(View): def post(self, request): data = json.loads(request.body) try: if '@' not in data['email']: return JsonResponse( {"message":"Email_Not_Verified"}, status = 404 ) elif len(data['email']) < 3: return JsonResponse( {"message":"Email_length_short"} ) elif len(data['password']) < 5: return JsonResponse( {"message":"Password_Not_Verified"}, status = 411 ) if Account.objects.filter(email = data['email']).exists() : account = Account.objects.get(email = data['email']) if bcrypt.checkpw(data['password'].encode('utf-8'), account.password.encode('utf-8')): access_token = jwt.encode({'email' : account.email}, 'secret', algorithm = 'HS256') return JsonResponse({"access-token" : access_token.decode('utf-8')}, status=200) return JsonResponse ({"message":"UNAUTHORIZED"},status=401) # password 에러 return JsonResponse ({"message":"UNAUTHORIZED"},status=401) # email 에러 except KeyError: return JsonResponse({'message' : "INVALID_KEYS"}, status = 400)
Author And Source
이 문제에 관하여(logitech 회원가입/로그인 view 작성), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://velog.io/@goohyun1990/logitech-회원가입로그인-view-작성저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)