1+ import bcrypt
2+
13from fastapi import APIRouter , Depends , HTTPException , status
24from sqlalchemy .orm import Session
3- from passlib .context import CryptContext
45
56from database import get_db
67from models import Comment
78from schemas import CommentCreate , CommentResponse , CommentDelete
89
910router = APIRouter (prefix = "/posts" , tags = ["comments" ])
1011
11- pwd_context = CryptContext (schemes = ["bcrypt" ], deprecated = "auto" )
12+
13+ def _hash_password (password : str ) -> str :
14+ return bcrypt .hashpw (password .encode (), bcrypt .gensalt ()).decode ()
15+
16+
17+ def _verify_password (password : str , hashed : str ) -> bool :
18+ return bcrypt .checkpw (password .encode (), hashed .encode ())
1219
1320
1421@router .get ("/{slug:path}/comments" , response_model = list [CommentResponse ])
@@ -28,7 +35,7 @@ def create_comment(slug: str, body: CommentCreate, db: Session = Depends(get_db)
2835 comment = Comment (
2936 post_slug = slug ,
3037 nickname = body .nickname ,
31- password_hash = pwd_context . hash (body .password ),
38+ password_hash = _hash_password (body .password ),
3239 content = body .content ,
3340 )
3441 db .add (comment )
@@ -53,7 +60,7 @@ def delete_comment(
5360 if not comment :
5461 raise HTTPException (status_code = 404 , detail = "댓글을 찾을 수 없습니다." )
5562
56- if not pwd_context . verify (body .password , comment .password_hash ):
63+ if not _verify_password (body .password , comment .password_hash ):
5764 raise HTTPException (status_code = 403 , detail = "비밀번호가 올바르지 않습니다." )
5865
5966 db .delete (comment )
0 commit comments