# Authentication Usage Example # This file demonstrates how to use the authentication endpoints import requests import json # API Base URL BASE_URL = "http://localhost:8000" def register_user(email, username, password): """Register a new user""" response = requests.post(f"{BASE_URL}/register", json={ "email": email, "username": username, "password": password }) return response.json() def login_user(username, password): """Login and get JWT token""" response = requests.post(f"{BASE_URL}/login", json={ "username": username, "password": password }) if response.status_code == 200: data = response.json() return data["access_token"] else: print(f"Login failed: {response.json()}") return None def get_user_info(token): """Get current user info""" headers = {"Authorization": f"Bearer {token}"} response = requests.get(f"{BASE_URL}/me", headers=headers) return response.json() def get_products(token, skip=0, limit=10): """Get products (requires authentication)""" headers = {"Authorization": f"Bearer {token}"} response = requests.get(f"{BASE_URL}/products?skip={skip}&limit={limit}", headers=headers) return response.json() def create_product(token, product_data): """Create a new product (requires authentication)""" headers = {"Authorization": f"Bearer {token}"} response = requests.post(f"{BASE_URL}/products", json=product_data, headers=headers) return response.json() # Example usage if __name__ == "__main__": # 1. Register a new user print("1. Registering new user...") try: user_result = register_user("test@example.com", "testuser", "password123") print(f"User registered: {user_result}") except Exception as e: print(f"Registration failed: {e}") # 2. Login with default admin user print("\n2. Logging in as admin...") admin_token = login_user("admin", "admin123") if admin_token: print(f"Admin login successful! Token: {admin_token[:50]}...") # 3. Get user info print("\n3. Getting admin user info...") user_info = get_user_info(admin_token) print(f"User info: {user_info}") # 4. Create a sample product print("\n4. Creating a sample product...") sample_product = { "product_id": "TEST001", "title": "Test Product", "description": "A test product for demonstration", "price": "19.99", "brand": "Test Brand", "availability": "in stock" } product_result = create_product(admin_token, sample_product) print(f"Product created: {product_result}") # 5. Get products list print("\n5. Getting products list...") products = get_products(admin_token) print(f"Products: {products}") # 6. Login with regular user print("\n6. Logging in as regular user...") user_token = login_user("testuser", "password123") if user_token: print(f"User login successful! Token: {user_token[:50]}...") # Regular users can also access protected endpoints user_info = get_user_info(user_token) print(f"Regular user info: {user_info}") products = get_products(user_token, limit=5) print(f"Products accessible to regular user: {len(products.get('products', []))} products") print("\nAuthentication example completed!") # Example cURL commands: """ # Register a new user curl -X POST "http://localhost:8000/register" \ -H "Content-Type: application/json" \ -d '{"email": "user@example.com", "username": "newuser", "password": "password123"}' # Login (get JWT token) curl -X POST "http://localhost:8000/login" \ -H "Content-Type: application/json" \ -d '{"username": "admin", "password": "admin123"}' # Use token to access protected endpoint curl -X GET "http://localhost:8000/me" \ -H "Authorization: Bearer YOUR_JWT_TOKEN_HERE" # Get products (protected) curl -X GET "http://localhost:8000/products" \ -H "Authorization: Bearer YOUR_JWT_TOKEN_HERE" # Create product (protected) curl -X POST "http://localhost:8000/products" \ -H "Authorization: Bearer YOUR_JWT_TOKEN_HERE" \ -H "Content-Type: application/json" \ -d '{"product_id": "TEST001", "title": "Test Product", "price": "19.99"}' """