22from jose import JWTError , jwt
33from sqlalchemy .orm import Session
44from models .user_model import User
5- from fastapi .security import OAuth2PasswordBearer
65from datetime import datetime , timedelta , timezone
76from fastapi import Depends , HTTPException , status
87from schemas .users .CRUD .read import get_user_by_email
8+ from fastapi .security import HTTPAuthorizationCredentials , HTTPBearer
99from modules .config import SECRET_KEY , ALGORITHM , ACCESS_TOKEN_EXPIRE_MINUTES
1010
11- oauth2_scheme = OAuth2PasswordBearer ( tokenUrl = "/auth/login" )
11+ security_scheme = HTTPBearer ( )
1212
1313
14- def create_access_token (data : dict ):
14+ def create_access_token (data : dict ) -> str :
1515 """
1616 Create a JWT access token.
1717
@@ -29,17 +29,19 @@ def create_access_token(data: dict):
2929
3030
3131def get_current_user (
32- db : Session = Depends (get_db ), token : str = Depends (oauth2_scheme )
32+ db : Session = Depends (get_db ),
33+ token : HTTPAuthorizationCredentials = Depends (security_scheme ),
3334) -> User :
3435 """
3536 Decode the JWT token to get the current authenticated user.
3637
3738 Acts as a dependency for protected routes: validates the token,
38- extracts the subject (email), and fetches the user from the DB .
39+ extracts the subject (email), and fetches the user from the database .
3940
4041 Args:
4142 db (Session): Database session dependency.
42- token (str): JWT token passed in the Authorization header.
43+ token (HTTPAuthorizationCredentials): JWT token passed in the
44+ Authorization header.
4345
4446 Raises:
4547 HTTPException: Raised if the token is invalid, expired,
@@ -54,7 +56,7 @@ def get_current_user(
5456 headers = {"WWW-Authenticate" : "Bearer" },
5557 )
5658 try :
57- payload = jwt .decode (token , SECRET_KEY , algorithms = [ALGORITHM ])
59+ payload = jwt .decode (token . credentials , SECRET_KEY , algorithms = [ALGORITHM ])
5860 email : str = payload .get ("sub" )
5961 if email is None :
6062 raise credentials_exception
0 commit comments