11from fastapi import APIRouter , Depends , HTTPException , Request
22from fastapi .security import HTTPAuthorizationCredentials , HTTPBearer
33
4+ from ..middleware .auth import UserRole
45from ..schemas .auth import AuthResponse , LoginRequest , RefreshRequest , Token
56from ..schemas .user import UserCreateRequest , UserCreateResponse
67from ..services .implementations .auth_service import AuthService
@@ -26,9 +27,29 @@ async def register_user(
2627
2728@router .post ("/login" , response_model = AuthResponse )
2829async def login (
29- credentials : LoginRequest , auth_service : AuthService = Depends (get_auth_service )
30+ request : Request ,
31+ credentials : LoginRequest ,
32+ auth_service : AuthService = Depends (get_auth_service ),
3033):
31- return auth_service .generate_token (credentials .email , credentials .password )
34+ try :
35+ is_admin_portal = request .headers .get ("X-Admin-Portal" ) == "true"
36+ auth_response = auth_service .generate_token (
37+ credentials .email , credentials .password
38+ )
39+ if is_admin_portal and not auth_service .is_authorized_by_role (
40+ auth_response .access_token , {UserRole .ADMIN }
41+ ):
42+ raise HTTPException (
43+ status_code = 403 ,
44+ detail = "Access denied. Admin privileges required for admin portal" ,
45+ )
46+
47+ return auth_response
48+
49+ except HTTPException as http_ex :
50+ raise http_ex
51+ except Exception as e :
52+ raise HTTPException (status_code = 500 , detail = str (e ))
3253
3354
3455@router .post ("/logout" )
0 commit comments