Fixed login redirecting issues
This commit is contained in:
154
app/api/deps.py
154
app/api/deps.py
@@ -674,3 +674,157 @@ def get_user_permissions(
|
||||
return []
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# OPTIONAL AUTHENTICATION (For Login Page Redirects)
|
||||
# ============================================================================
|
||||
|
||||
def get_current_admin_optional(
|
||||
request: Request,
|
||||
credentials: Optional[HTTPAuthorizationCredentials] = Depends(security),
|
||||
admin_token: Optional[str] = Cookie(None),
|
||||
db: Session = Depends(get_db),
|
||||
) -> Optional[User]:
|
||||
"""
|
||||
Get current admin user from admin_token cookie or Authorization header.
|
||||
|
||||
Returns None instead of raising exceptions if not authenticated.
|
||||
Used for login pages to check if user is already authenticated.
|
||||
|
||||
Priority:
|
||||
1. Authorization header (API calls)
|
||||
2. admin_token cookie (page navigation)
|
||||
|
||||
Args:
|
||||
request: FastAPI request
|
||||
credentials: Optional Bearer token from header
|
||||
admin_token: Optional token from admin_token cookie
|
||||
db: Database session
|
||||
|
||||
Returns:
|
||||
User: Authenticated admin user if valid token exists
|
||||
None: If no token, invalid token, or user is not admin
|
||||
"""
|
||||
token, source = _get_token_from_request(
|
||||
credentials,
|
||||
admin_token,
|
||||
"admin_token",
|
||||
str(request.url.path)
|
||||
)
|
||||
|
||||
if not token:
|
||||
return None
|
||||
|
||||
try:
|
||||
# Validate token and get user
|
||||
user = _validate_user_token(token, db)
|
||||
|
||||
# Verify user is admin
|
||||
if user.role == "admin":
|
||||
return user
|
||||
except Exception:
|
||||
# Invalid token or other error
|
||||
pass
|
||||
|
||||
return None
|
||||
|
||||
|
||||
def get_current_vendor_optional(
|
||||
request: Request,
|
||||
credentials: Optional[HTTPAuthorizationCredentials] = Depends(security),
|
||||
vendor_token: Optional[str] = Cookie(None),
|
||||
db: Session = Depends(get_db),
|
||||
) -> Optional[User]:
|
||||
"""
|
||||
Get current vendor user from vendor_token cookie or Authorization header.
|
||||
|
||||
Returns None instead of raising exceptions if not authenticated.
|
||||
Used for login pages to check if user is already authenticated.
|
||||
|
||||
Priority:
|
||||
1. Authorization header (API calls)
|
||||
2. vendor_token cookie (page navigation)
|
||||
|
||||
Args:
|
||||
request: FastAPI request
|
||||
credentials: Optional Bearer token from header
|
||||
vendor_token: Optional token from vendor_token cookie
|
||||
db: Database session
|
||||
|
||||
Returns:
|
||||
User: Authenticated vendor user if valid token exists
|
||||
None: If no token, invalid token, or user is not vendor
|
||||
"""
|
||||
token, source = _get_token_from_request(
|
||||
credentials,
|
||||
vendor_token,
|
||||
"vendor_token",
|
||||
str(request.url.path)
|
||||
)
|
||||
|
||||
if not token:
|
||||
return None
|
||||
|
||||
try:
|
||||
# Validate token and get user
|
||||
user = _validate_user_token(token, db)
|
||||
|
||||
# Verify user is vendor
|
||||
if user.role == "vendor":
|
||||
return user
|
||||
except Exception:
|
||||
# Invalid token or other error
|
||||
pass
|
||||
|
||||
return None
|
||||
|
||||
|
||||
def get_current_customer_optional(
|
||||
request: Request,
|
||||
credentials: Optional[HTTPAuthorizationCredentials] = Depends(security),
|
||||
customer_token: Optional[str] = Cookie(None),
|
||||
db: Session = Depends(get_db),
|
||||
) -> Optional[User]:
|
||||
"""
|
||||
Get current customer user from customer_token cookie or Authorization header.
|
||||
|
||||
Returns None instead of raising exceptions if not authenticated.
|
||||
Used for login pages to check if user is already authenticated.
|
||||
|
||||
Priority:
|
||||
1. Authorization header (API calls)
|
||||
2. customer_token cookie (page navigation)
|
||||
|
||||
Args:
|
||||
request: FastAPI request
|
||||
credentials: Optional Bearer token from header
|
||||
customer_token: Optional token from customer_token cookie
|
||||
db: Database session
|
||||
|
||||
Returns:
|
||||
User: Authenticated customer user if valid token exists
|
||||
None: If no token, invalid token, or user is not customer
|
||||
"""
|
||||
token, source = _get_token_from_request(
|
||||
credentials,
|
||||
customer_token,
|
||||
"customer_token",
|
||||
str(request.url.path)
|
||||
)
|
||||
|
||||
if not token:
|
||||
return None
|
||||
|
||||
try:
|
||||
# Validate token and get user
|
||||
user = _validate_user_token(token, db)
|
||||
|
||||
# Verify user is customer
|
||||
if user.role == "customer":
|
||||
return user
|
||||
except Exception:
|
||||
# Invalid token or other error
|
||||
pass
|
||||
|
||||
return None
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user