diff --git a/src/myfasthtml/auth/routes.py b/src/myfasthtml/auth/routes.py index db92c18..38cf3e9 100644 --- a/src/myfasthtml/auth/routes.py +++ b/src/myfasthtml/auth/routes.py @@ -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. @@ -27,6 +27,7 @@ def setup_auth_routes(app, rt, mount_auth_app=True, sqlite_db_path="Users.db"): rt: Route decorator from FastHTML mount_auth_app: Whether to mount the auth FastApi API routes 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 """ # Attempt login - auth_data = login_user(email, password) + auth_data = login_user(email, password, base_url=base_url) if auth_data: # Login successful - store tokens in session diff --git a/src/myfasthtml/auth/utils.py b/src/myfasthtml/auth/utils.py index dc7d369..5b4c892 100644 --- a/src/myfasthtml/auth/utils.py +++ b/src/myfasthtml/auth/utils.py @@ -163,13 +163,14 @@ def check_token_expiry(token: str) -> Optional[float]: 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. Args: email: User email address password: User password + base_url: Returns: 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: response = http_client.post( - f"{API_BASE_URL}/auth/login", + f"{base_url or API_BASE_URL}/auth/login", data={"username": email, "password": password}, headers={"Content-Type": "application/x-www-form-urlencoded"}, timeout=10.0 @@ -196,7 +197,7 @@ def login_user(email: str, password: str) -> Optional[Dict[str, Any]]: 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. @@ -204,14 +205,14 @@ def register_user(email: str, username: str, password: str) -> Optional[Dict[str email: User email address username: User name password: User password - + base_url: Returns: Dictionary containing success message if registration succeeds, None if registration fails """ try: 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}, timeout=10.0 ) @@ -224,20 +225,20 @@ def register_user(email: str, username: str, password: str) -> Optional[Dict[str 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. Args: refresh_token: Valid refresh token - + base_url: Returns: Dictionary containing new access_token and refresh_token if successful, None if refresh fails """ try: response = http_client.post( - f"{API_BASE_URL}/auth/refresh", + f"{base_url or API_BASE_URL}/auth/refresh", json={"refresh_token": refresh_token}, timeout=10.0 ) @@ -254,20 +255,20 @@ def refresh_access_token(refresh_token: str) -> Optional[Dict[str, Any]]: 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. Args: access_token: Valid access token - + base_url: Returns: Dictionary containing user information if successful, None if request fails """ try: 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}"}, timeout=10.0 ) @@ -280,19 +281,19 @@ def get_user_info(access_token: str) -> Optional[Dict[str, Any]]: 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. Args: refresh_token: Refresh token to revoke - + base_url: Returns: True if logout successful, False otherwise """ try: response = http_client.post( - f"{API_BASE_URL}/auth/logout", + f"{base_url or API_BASE_URL}/auth/logout", json={"refresh_token": refresh_token}, timeout=10.0 )