Fixed wrong base url used by the auth API

This commit is contained in:
2025-11-10 16:08:29 +01:00
parent 3d46e092aa
commit a547b2b882
2 changed files with 18 additions and 16 deletions

View File

@@ -18,7 +18,7 @@ from ..auth.utils import (
) )
def setup_auth_routes(app, rt, mount_auth_app=True, sqlite_db_path="Users.db"): def setup_auth_routes(app, rt, mount_auth_app=True, sqlite_db_path="Users.db", base_url=None):
""" """
Setup all authentication and protected routes. Setup all authentication and protected routes.
@@ -27,6 +27,7 @@ def setup_auth_routes(app, rt, mount_auth_app=True, sqlite_db_path="Users.db"):
rt: Route decorator from FastHTML rt: Route decorator from FastHTML
mount_auth_app: Whether to mount the auth FastApi API routes mount_auth_app: Whether to mount the auth FastApi API routes
sqlite_db_path: by default, create a new SQLite database at this path sqlite_db_path: by default, create a new SQLite database at this path
base_url: Base URL for the application (default to localhost:5001 if not provided)
""" """
# ============================================================================ # ============================================================================
@@ -61,7 +62,7 @@ def setup_auth_routes(app, rt, mount_auth_app=True, sqlite_db_path="Users.db"):
RedirectResponse on success, or LoginPage with error on failure RedirectResponse on success, or LoginPage with error on failure
""" """
# Attempt login # Attempt login
auth_data = login_user(email, password) auth_data = login_user(email, password, base_url=base_url)
if auth_data: if auth_data:
# Login successful - store tokens in session # Login successful - store tokens in session

View File

@@ -163,13 +163,14 @@ def check_token_expiry(token: str) -> Optional[float]:
return None return None
def login_user(email: str, password: str) -> Optional[Dict[str, Any]]: def login_user(email: str, password: str, base_url: str = None) -> Optional[Dict[str, Any]]:
""" """
Authenticate user with email and password. Authenticate user with email and password.
Args: Args:
email: User email address email: User email address
password: User password password: User password
base_url:
Returns: Returns:
Dictionary containing access_token, refresh_token, and user_info if successful, Dictionary containing access_token, refresh_token, and user_info if successful,
@@ -177,7 +178,7 @@ def login_user(email: str, password: str) -> Optional[Dict[str, Any]]:
""" """
try: try:
response = http_client.post( response = http_client.post(
f"{API_BASE_URL}/auth/login", f"{base_url or API_BASE_URL}/auth/login",
data={"username": email, "password": password}, data={"username": email, "password": password},
headers={"Content-Type": "application/x-www-form-urlencoded"}, headers={"Content-Type": "application/x-www-form-urlencoded"},
timeout=10.0 timeout=10.0
@@ -196,7 +197,7 @@ def login_user(email: str, password: str) -> Optional[Dict[str, Any]]:
return None return None
def register_user(email: str, username: str, password: str) -> Optional[Dict[str, Any]]: def register_user(email: str, username: str, password: str, base_url: str = None) -> Optional[Dict[str, Any]]:
""" """
Register a new user. Register a new user.
@@ -204,14 +205,14 @@ def register_user(email: str, username: str, password: str) -> Optional[Dict[str
email: User email address email: User email address
username: User name username: User name
password: User password password: User password
base_url:
Returns: Returns:
Dictionary containing success message if registration succeeds, Dictionary containing success message if registration succeeds,
None if registration fails None if registration fails
""" """
try: try:
response = http_client.post( response = http_client.post(
f"{API_BASE_URL}/auth/register", f"{base_url or API_BASE_URL}/auth/register",
json={"email": email, "username": username, "password": password}, json={"email": email, "username": username, "password": password},
timeout=10.0 timeout=10.0
) )
@@ -224,20 +225,20 @@ def register_user(email: str, username: str, password: str) -> Optional[Dict[str
return None return None
def refresh_access_token(refresh_token: str) -> Optional[Dict[str, Any]]: def refresh_access_token(refresh_token: str, base_url: str = None) -> Optional[Dict[str, Any]]:
""" """
Refresh the access token using a refresh token. Refresh the access token using a refresh token.
Args: Args:
refresh_token: Valid refresh token refresh_token: Valid refresh token
base_url:
Returns: Returns:
Dictionary containing new access_token and refresh_token if successful, Dictionary containing new access_token and refresh_token if successful,
None if refresh fails None if refresh fails
""" """
try: try:
response = http_client.post( response = http_client.post(
f"{API_BASE_URL}/auth/refresh", f"{base_url or API_BASE_URL}/auth/refresh",
json={"refresh_token": refresh_token}, json={"refresh_token": refresh_token},
timeout=10.0 timeout=10.0
) )
@@ -254,20 +255,20 @@ def refresh_access_token(refresh_token: str) -> Optional[Dict[str, Any]]:
return None return None
def get_user_info(access_token: str) -> Optional[Dict[str, Any]]: def get_user_info(access_token: str, base_url: str = None) -> Optional[Dict[str, Any]]:
""" """
Get current user information using access token. Get current user information using access token.
Args: Args:
access_token: Valid access token access_token: Valid access token
base_url:
Returns: Returns:
Dictionary containing user information if successful, Dictionary containing user information if successful,
None if request fails None if request fails
""" """
try: try:
response = http_client.get( response = http_client.get(
f"{API_BASE_URL}/auth/me", f"{base_url or API_BASE_URL}/auth/me",
headers={"Authorization": f"Bearer {access_token}"}, headers={"Authorization": f"Bearer {access_token}"},
timeout=10.0 timeout=10.0
) )
@@ -280,19 +281,19 @@ def get_user_info(access_token: str) -> Optional[Dict[str, Any]]:
return None return None
def logout_user(refresh_token: str) -> bool: def logout_user(refresh_token: str, base_url: str = None) -> bool:
""" """
Logout user by revoking the refresh token. Logout user by revoking the refresh token.
Args: Args:
refresh_token: Refresh token to revoke refresh_token: Refresh token to revoke
base_url:
Returns: Returns:
True if logout successful, False otherwise True if logout successful, False otherwise
""" """
try: try:
response = http_client.post( response = http_client.post(
f"{API_BASE_URL}/auth/logout", f"{base_url or API_BASE_URL}/auth/logout",
json={"refresh_token": refresh_token}, json={"refresh_token": refresh_token},
timeout=10.0 timeout=10.0
) )