Skip to content

Commit b6bb5ea

Browse files
committed
add check for admin
1 parent ed8fe8e commit b6bb5ea

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

backend/app/routes/auth.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from fastapi import APIRouter, Depends, HTTPException, Request
22
from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer
33

4+
from ..middleware.auth import UserRole
45
from ..schemas.auth import AuthResponse, LoginRequest, RefreshRequest, Token
56
from ..schemas.user import UserCreateRequest, UserCreateResponse
67
from ..services.implementations.auth_service import AuthService
@@ -26,9 +27,29 @@ async def register_user(
2627

2728
@router.post("/login", response_model=AuthResponse)
2829
async 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

Comments
 (0)