RestFul API

JWT(μ–‘λ°©ν–₯) μ•”ν˜Έν™” 둜그인 API λ§Œλ“€κΈ°

567Rabbit 2024. 5. 22. 16:35

둜그인 API λ§Œλ“€κΈ°

 

 

 

 

 

app.py에 μ‚½μž…

 

bash
λ‹«κΈ°
from resources.user import UserLoginResource api.add_resource( UserLoginResource, '/users/login')

 

 

app.py νŒŒμΌμ— μΆ”κ°€

 

bash
λ‹«κΈ°
from flask_jwt_extended import JWTManager from config import Config # app = Flask(__name__) μ•„λž˜μ— μΆ”κ°€ # ν™˜κ²½λ³€μˆ˜ μ…‹νŒ… app.config.from_object(Config) # JWT λ§€λ‹ˆμ € μ΄ˆκΈ°ν™” jwt = JWTManager(app) # api = Api(app) μœ„μ— μΆ”κ°€

 

 

 

 

 

 

user.py에 μ‚½μž…

 

bash
λ‹«κΈ°
from utils import check_password #4μ—μ„œ μ‚½μž… from flask_jwt_extended import create_access_token class UserLoginResource(Resource) : ​​​​ ​​​​def post(self) : ​​​​​​​​ ​​​​​​​​# 1. ν΄λΌμ΄μ–ΈνŠΈλ‘œλΆ€ν„° 데이터λ₯Ό λ°›λŠ”λ‹€. ​​​​​​​​data = request.get_json() ​​​​​​​​if 'email' not in data or 'password' not in data: ​​​​​​​​​​​​return {'result' : 'fail'}, 400 ​​​​​​​​if data['email'].strip() == '' or data['password'].strip() == '': ​​​​​​​​​​​​return {'result' : 'fail'}, 400 ​​​​​​​​ ​​​​​​​​# 2. DBλ‘œλΆ€ν„° 이메일에 ν•΄λ‹Ήν•˜λŠ” μœ μ € 정보λ₯Ό κ°€μ Έμ˜¨λ‹€. ​​​​​​​​try : ​​​​​​​​​​​​connection = get_connection() ​​​​​​​​​​​​query = '''select * ​​​​​​​​​​​​​​​​​​​​​​​​from user ​​​​​​​​​​​​​​​​​​​​​​​​where email = %s ;''' ​​​​​​​​​​​​record = ( data['email'] , ) ​​​​​​​​​​​​cursor = connection.cursor(dictionary=True) ​​​​​​​​​​​​cursor.execute(query, record) ​​​​​​​​​​​​result_list = cursor.fetchall() ​​​​​​​​​​​​print(result_list) ​​​​​​​​​​​​cursor.close() ​​​​​​​​​​​​connection.close() ​​​​​​​​except Error as e: ​​​​​​​​​​​​if cursor is not None: ​​​​​​​​​​​​​​​​cursor.close() ​​​​​​​​​​​​if connection is not None: ​​​​​​​​​​​​​​​​connection.close() ​​​​​​​​​​​​return {'result':'fail', 'error':str(e)},500 ​​​​​​​​# 3. νšŒμ›μΈμ§€ ν™•μΈν•œλ‹€. ​​​​​​​​if result_list == [] : ​​​​​​​​​​​​return {'result' : 'fail'} , 401 ​​​​​​​​ ​​​​​​​​# 4. λΉ„λ°€λ²ˆν˜Έλ₯Ό μ²΄ν¬ν•œλ‹€. ​​​​​​​​ ​​​​​​​​# μœ μ €κ°€ μž…λ ₯ν•œ λΉ„λ²ˆ data['password'] ​​​​​​​​# DB에 μ•”ν˜Έν™”λœ λΉ„λ²ˆ result_list[0]['password'] ​​​​​​​​isCorrect = check_password(data['password'] , result_list[0]['password']) ​​​​​​​​if isCorrect == False : ​​​​​​​​​​​​return {'result' : 'fail'} , 401 ​​​​​​​ ​​​​​​​# 5. μœ μ €μ•„μ΄λ””λ₯Ό κ°€μ Έμ˜¨λ‹€. ​​​​​​​​user_id = result_list[0]['id'] ​​​​​​​​# 6. JWT 토큰을 λ§Œλ“ λ‹€. ​​​​​​​​access_token = create_access_token(user_id) ​​​​​​​​# 7. ν΄λΌμ΄μ–ΈνŠΈμ— μ‘λ‹΅ν•œλ‹€. ​​​​​​​​return {'result' : 'success', 'access_token':access_token}

 

 

 

 

 

 

둜그인 ν•œ μœ μ €λ§Œ μ‚¬μš©ν•  수 μžˆλ„λ‘ API에 토큰 μ μš©ν•˜κΈ°

 

 

recipe.py에 μ‚½μž…

 

bash
λ‹«κΈ°
from flask_jwt_extended import get_jwt_identity, jwt_required

 

 

 

(1). Class RecipeListResource 의 def post  : 데이터 μƒμ„±ν•˜κΈ°

 

 

 

 

 

 

(2). Class RecipeListResource 의 def get   : 데이터 λͺ¨λ‘ κ°€μ Έμ˜€κΈ°

 

 

 

 

 

 

(3). class RecipeResource의 def get : νŠΉμ • id 데이터 κ°€μ Έμ˜€κΈ°

 

 

# 2-2 μ‚½μž…ν•˜κ³  , # 3 ~ μ‚­μ œν•˜κΈ°

 

 

 

 

 

(4). class RecipeResource의 def put : νŠΉμ • id 데이터 μˆ˜μ •ν•˜κΈ°

 

 

 

 

 

 

(5). class RecipeResource의 def delete : νŠΉμ • id 데이터 μ‚­μ œν•˜κΈ°

 

 

 

 

 

 


(6). class RecipePublishResource의 def put : λ ˆμ‹œν”Ό λ°œν–‰ (publish =1)

 

 

 

 

 

 

 

(7). class RecipePublishResource의 def delete :  λ ˆμ‹œν”Ό λ°œν–‰ μ·¨μ†Œ (publish =0) : μž„μ‹œμ €μž₯

 

 

 

 

 

μ €μž₯ν•˜κ³  flask run

 

 

 

 

 

 

 

 

>>>> μ‚¬μš©ν•  API의 Headersλ₯Ό λ³€κ²½ν•΄μ€€λ‹€.


Body에 Valueλ₯Ό Bearer + μ•”ν˜Έν‚€ λ„£κ³  sendν•˜λ©΄ success λœλ‹€. (μ•”ν˜Έν‚€λŠ” λ‘œκ·ΈμΈμ—μ„œ κ°€μ Έμ˜¨ μ•”ν˜Έν‚€λ₯Ό λ³΅λΆ™ν•œλ‹€)

 

 

>>> λ‚˜λ¨Έμ§€λ„ λ³€κ²½ν•΄μ€€λ‹€