[๐ŸคœMiniproject]Westagram-์•”ํ˜ธํ•ด์‰ฌํ™”/ํ† ํฐ๋ฐœํ–‰

์—ฌ์ฃผ๋งยท2020๋…„ 12์›” 17์ผ
0

Project

๋ชฉ๋ก ๋ณด๊ธฐ
3/6

๊ฐœ์ธ์ •๋ณด ๋ณดํ˜ธ๋ฅผ ์œ„ํ•ด ์•”ํ˜ธ๋ฅผ ๋‹จ๋ฐฉํ–ฅ ํ•ด์‰ฌํ™” ํ•œ๋’ค ๋™์‹œ์— ๋กœ๊ทธ์ธํ•˜๋ฉด ํ† ํฐ์„ ๋ฐœ๊ธ‰ํ•ด์ฃผ๋Š” ๋‹จ๊ณ„์ด๋‹ค.
ํ•ด๋‹น ๋‚ด์šฉ์— ๋Œ€ํ•œ ์ž์„ธํ•œ๋‚ด์šฉ์€ ์—ฌ๊ธฐ๋ฅผ ์ฐธ์กฐ!

์ค‘์š”ํ•œ ๋‘๊ฐ€์ง€ ํฌ์ธํŠธ!

  1. ํ•ด์‰ฌํ™” ํ• ๋•Œ ์ด์šฉํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ์กด์žฌํ•˜๋Š”๋ฐ ์ด ์กด์žฌ๋ฅผ git์— ์˜ฌ๋ฆด๋•Œ ๋ฐ˜์˜์ด ์•ˆ๋˜๊ฒŒ ํ•˜๋Š” ์ž‘์—…์„ ํ•ด์ฃผ์ž
#my_settings.py

 ALGORITHM = 'HS256'
  1. decode
    decodeํ™” ์•ˆํ•œ ์ฝ”๋“œ->b๋กœ ์‹œ์ž‘๋˜๋Š”๋‹จ์–ด๋“ค์ด decode๋ฅผ ํ•˜์ง€ ์•Š์•„ byteํƒ€์ž…์œผ๋กœ ์ €์žฅ๋œ ๋น„๋ฐ€๋ฒˆํ˜ธ๋“ค์ด๋‹ค.
password  = bcrypt.hashpw(data['password'].   encode('utf-8'),bcrypt.gensalt()).decode())

bcrypt๊ณผ์ •์—์„œ ํ•ด์‰ฌํ™”๋œ ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ db์— ์ €์žฅํ• ๋•Œ ๋ฐ˜๋“œ์‹œ decode๋ฅผ ๋ถ™์—ฌ strํƒ€์ž…์ธ ์ฑ„๋กœ ์ €์žฅ๋˜๊ฒŒ ํ•ด์ฃผ์ž!(์•ˆ๊ทธ๋Ÿฌ๋ฉด ๋‚˜์ค‘์— decodeํ•ด์ค˜์•ผํ•˜๋‹ˆ ๋‚˜์ค‘์„์œ„ํ•ด..)

-ํšŒ์›๊ฐ€์ž…์‹œ ๋น„๋ฐ€๋ฒˆํ˜ธ ํ•ด์‰ฌ์ฒ˜๋ฆฌ

import json, bcrypt, jwt, re

 from django.http import JsonResponse
 from django.views import View

 from user.models import Account
 from my_settings import SECRET,ALGORITHM

 class AccountView(View):
     def post(self, request):
         data = json.loads(request.body)
         REGAX_EMAIL = '^[a-zA-Z0-9+-_.]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.  ]+$'
         REGEX_PASSWORD = '^[A-Za-z0-9@#$%^&+=]{8,}$'

         try:
             if not re.match(email_validation, data['email']):
                 return JsonResponse(
                     {"message":"INVALID_MAIL"},status=401
                 )

             if not re.match(pw_validation, data['password']):
                 return JsonResponse(
                     {"message":"INVALID_PW"},status=401
                 )
             if Account.objects.filter(email=data['email']).exists():
               return JsonResponse(
                     {"message":"USER_EXIST"},status=400
                 )

             else:
                 Account.objects.create(
                         email = data['email'],
                         password  = bcrypt.hashpw(data['password'].   encode('utf-8'),bcrypt.gensalt()).decode()
                 )
                 return JsonResponse({"message":"SUCCESS"},status=200)

         except:
             return JsonResponse({"message":"KEY_ERROR"},status=401)

bcrypt, jwt๋ฅผ importํ•ด์„œ ์ธ์ฆ&์ธ๊ฐ€์— ์‚ฌ์šฉํ•œ๋‹ค.

- ๋กœ๊ทธ์ธ ๋น„๋ฐ€๋ฒˆํ˜ธ ํ™•์ธ ๋ฐ ํ† ํฐ ๋ฐœํ–‰

 class LoginView(View):
     def post(self, request):
         data = json.loads(request.body)
         user_pw = Account.objects.get(email=data['email']).password
         user_id = Account.objects.get(email=data['email']).id
         password_check = bcrypt.checkpw(data['password'].encode('utf- 8'),user_pw.encode('utf-8'))

         try:
             if Account.objects.all().filter(email=data['email']).     exists():
             
                 if bcrypt.checkpw(data['password'].encode('utf-8').   user_pw.encode('utf-8')):
                     access_token = jwt.encode({'id' : user_id},       SECRET,ALGORITHM).decode('utf-8')

                     return JsonResponse({"message":"SUCCESS",         "TOKEN" : access_token},status=200)

                 return JsonResponse({"message":"INVALID_PASSWORD"},   status=402)

         except KeyError:
             return JsonResponse({"message":"KEY_ERROR"},status=401)

         except ValueError:
             return JsonResponse({"message":"INVALID_USER"},           status=401)

์น˜๊ณ ๋ณด๋ฉด ๊ฐ„๋‹จํ•œ๋ฐ ์ด๊ฑธ ์ดํ•ดํ•˜๊ณ  ๋‚ฉ๋“ํ•˜๋Š”๋ฐ ํ•œ์ฃผ๋ฅผ ๋ณด๋ƒˆ๋‹ค.

ํ˜„์žฌ ํ”„๋กœ์ ํŠธ ์ง„ํ–‰์ค‘์ธ๋ฐ ๋ชจ๋“  ์‚ฌ์ดํŠธ๋Š” ์ผ๋‹จ ๋กœ๊ทธ์ธ๊ณผ ํšŒ์›๊ฐ€์ž…์ด ํ•„์ˆ˜์ ์œผ๋กœ ์žˆ์–ด์„œ, ํ•œ๋ฒˆ ์ •๋ฆฌํ•ด๋ณด์•˜๋‹ค! ํ”„๋กœ์ ํŠธ ํ™”์ดํŒ…!!!

profile
๐ŸŒฑBackend Developer๐Ÿ‘ฉโ€๐Ÿ’ป

0๊ฐœ์˜ ๋Œ“๊ธ€