Download backend/utils/database.py from Zelyanoth/Lin: direct link, hf CLI and curl.
- Browser
- Download file 3.02 kB
-
https://huggingface.co/spaces/Zelyanoth/Lin/resolve/2f9f089a2766b29cc75650ca36b9cdd9e615cfb0/backend/utils/database.py
- Command line
-
hf download hf://spaces/Zelyanoth/Lin@2f9f089a2766b29cc75650ca36b9cdd9e615cfb0/backend/utils/database.py
-
curl -L -o database.py https://huggingface.co/spaces/Zelyanoth/Lin/resolve/2f9f089a2766b29cc75650ca36b9cdd9e615cfb0/backend/utils/database.py
3.02 kB
| from supabase import create_client, Client | |
| import os | |
| import logging | |
| def init_supabase(url: str, key: str) -> Client: | |
| """ | |
| Initialize Supabase client. | |
| Args: | |
| url (str): Supabase project URL | |
| key (str): Supabase API key | |
| Returns: | |
| Client: Supabase client instance | |
| """ | |
| if not url or not key: | |
| raise ValueError("Supabase URL and key must be provided") | |
| try: | |
| client = create_client(url, key) | |
| logging.info("Supabase client initialized successfully") | |
| return client | |
| except Exception as e: | |
| logging.error(f"Failed to initialize Supabase client: {str(e)}") | |
| raise e | |
| def get_user_by_email(supabase: Client, email: str): | |
| """ | |
| Get user by email from Supabase Auth. | |
| Args: | |
| supabase (Client): Supabase client instance | |
| email (str): User email | |
| Returns: | |
| dict: User data or None if not found | |
| """ | |
| try: | |
| response = supabase.auth.sign_in_with_password({ | |
| "email": email, | |
| "password": "temp" # We're not actually signing in, just checking if user exists | |
| }) | |
| return response.user | |
| except Exception: | |
| return None | |
| def create_user(supabase: Client, email: str, password: str): | |
| """ | |
| Create a new user in Supabase Auth. | |
| Args: | |
| supabase (Client): Supabase client instance | |
| email (str): User email | |
| password (str): User password | |
| Returns: | |
| dict: User creation response | |
| """ | |
| try: | |
| response = supabase.auth.sign_up({ | |
| "email": email, | |
| "password": password | |
| }) | |
| return response | |
| except Exception as e: | |
| raise e | |
| def authenticate_user(supabase: Client, email: str, password: str): | |
| """ | |
| Authenticate user with email and password. | |
| Args: | |
| supabase (Client): Supabase client instance | |
| email (str): User email | |
| password (str): User password | |
| Returns: | |
| dict: Authentication response | |
| """ | |
| try: | |
| response = supabase.auth.sign_in_with_password({ | |
| "email": email, | |
| "password": password | |
| }) | |
| return response | |
| except Exception as e: | |
| logging.error(f"Authentication error for user {email}: {str(e)}") | |
| raise e | |
| def check_database_connection(supabase: Client) -> bool: | |
| """ | |
| Check if the database connection is working. | |
| Args: | |
| supabase (Client): Supabase client instance | |
| Returns: | |
| bool: True if connection is working, False otherwise | |
| """ | |
| try: | |
| response = supabase.auth.get_user() | |
| if response.user: | |
| logging.info("Database connection check: SUCCESS") | |
| return True | |
| else: | |
| logging.error("Database connection check: FAILED - No user returned") | |
| return False | |
| except Exception as e: | |
| logging.error(f"Database connection check: FAILED - {str(e)}") | |
| return False |