access token
을 클라이언트에게 전송access token
을 첨부해서 requect를 서버에 전송bcrypt는 비밀번호 암호화에 사용되는 알고리즘을 제공하는 라이브러리
pip install bcrypt
프론트에서 비밀번호 값을 받았다면, bcrypt의
hashpw()
메소드로 비밀번호 암호화를 진행한다.
import bcrypt
password = data['password'].encode('utf-8')
bcrypt 인자는 bytes로 입력을 받기때문에 비밀번호를 먼저 byte형으로 바꿔주자
password_crypt = bcrypt.hashpw(password,bcrypt.gensalt())
.gensalt()
매소드로 소금을 생성했다.
password_crypt = password_crypt.decode('utf-8')
데이터베이서의 유저정볼르 담는 테이블을 확인해보면 Fieldtype은
VARCHAR(200)
과 같이 문자열을 받도록 되어 있다. 해쉬 비밀번호는 Bytes형 이기 때문에 DB에 저장 할 수 없다. 저장할 수 있도록 문자열로 decoing 해주어야 된다
로그인이 구현되는 과정을 알아보자
account = Accounts.objects.get(email = data['email']) if bcrypt.checkpw(data['password'].encode('utf-8'),account.password.encode('utf-8')):
bcrpt에서는 비밀번호 정보를 비교 할 수 있는
checkpw()
메소드를 제공한다.
❗️ 비교할때는 입력받은 데이터도 encoding상태로 비교를 해줘야된다!
token = jwt.encode({'id':account.id},SECRET_KEY,algorithm=ABC) token = token.decode('utf-8') return JsonResponse({'message':'로그인 성공!', 'token' :f'{token})
로그인 성공시 토큰 부여