Spaces:
Paused
Paused
Update app/services/auth_service.py
Browse files
app/services/auth_service.py
CHANGED
|
@@ -7,13 +7,12 @@ from passlib.context import CryptContext
|
|
| 7 |
from ..models.auth import UserProfile, LoginResponse
|
| 8 |
from ..core.config import settings
|
| 9 |
from ..db.crud import get_user_by_email
|
| 10 |
-
from ..db.database import get_db
|
| 11 |
-
from sqlalchemy.ext.asyncio import AsyncSession
|
| 12 |
|
| 13 |
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
|
| 14 |
-
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="api/auth/login")
|
| 15 |
|
| 16 |
-
async def authenticate_user(email: str, password: str, db: AsyncSession
|
| 17 |
"""Authenticate a user and return their profile if credentials are valid."""
|
| 18 |
user = await get_user_by_email(db, email)
|
| 19 |
if not user or not verify_password(password, user.hashed_password):
|
|
@@ -52,7 +51,7 @@ def create_refresh_token(data: dict) -> str:
|
|
| 52 |
to_encode.update({"exp": expire, "refresh": True})
|
| 53 |
return jwt.encode(to_encode, settings.SECRET_KEY, algorithm=settings.ALGORITHM)
|
| 54 |
|
| 55 |
-
async def refresh_access_token(refresh_token: str) -> LoginResponse:
|
| 56 |
"""Create new access token using refresh token."""
|
| 57 |
try:
|
| 58 |
payload = jwt.decode(
|
|
@@ -71,7 +70,7 @@ async def refresh_access_token(refresh_token: str) -> LoginResponse:
|
|
| 71 |
status_code=status.HTTP_401_UNAUTHORIZED,
|
| 72 |
detail="Invalid refresh token"
|
| 73 |
)
|
| 74 |
-
user = await get_user_by_email(email)
|
| 75 |
if not user:
|
| 76 |
raise HTTPException(
|
| 77 |
status_code=status.HTTP_401_UNAUTHORIZED,
|
|
|
|
| 7 |
from ..models.auth import UserProfile, LoginResponse
|
| 8 |
from ..core.config import settings
|
| 9 |
from ..db.crud import get_user_by_email
|
| 10 |
+
from ..db.database import get_db, AsyncSession
|
|
|
|
| 11 |
|
| 12 |
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
|
| 13 |
+
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="api/v1/auth/login")
|
| 14 |
|
| 15 |
+
async def authenticate_user(email: str, password: str, db: AsyncSession) -> Optional[UserProfile]:
|
| 16 |
"""Authenticate a user and return their profile if credentials are valid."""
|
| 17 |
user = await get_user_by_email(db, email)
|
| 18 |
if not user or not verify_password(password, user.hashed_password):
|
|
|
|
| 51 |
to_encode.update({"exp": expire, "refresh": True})
|
| 52 |
return jwt.encode(to_encode, settings.SECRET_KEY, algorithm=settings.ALGORITHM)
|
| 53 |
|
| 54 |
+
async def refresh_access_token(refresh_token: str, db: AsyncSession) -> LoginResponse:
|
| 55 |
"""Create new access token using refresh token."""
|
| 56 |
try:
|
| 57 |
payload = jwt.decode(
|
|
|
|
| 70 |
status_code=status.HTTP_401_UNAUTHORIZED,
|
| 71 |
detail="Invalid refresh token"
|
| 72 |
)
|
| 73 |
+
user = await get_user_by_email(db, email)
|
| 74 |
if not user:
|
| 75 |
raise HTTPException(
|
| 76 |
status_code=status.HTTP_401_UNAUTHORIZED,
|