#!/usr/bin/env python3 """ Create Superadmin User Script Generates a superadmin user with hashed password for LOAF membership platform """ import bcrypt import sys import os from getpass import getpass def generate_password_hash(password: str) -> str: """Generate bcrypt hash for password""" return bcrypt.hashpw(password.encode(), bcrypt.gensalt()).decode() def generate_sql(email: str, password_hash: str, first_name: str, last_name: str) -> str: """Generate SQL INSERT statement""" return f""" -- Create Superadmin User INSERT INTO users ( id, email, password_hash, first_name, last_name, status, role, email_verified, created_at, updated_at ) VALUES ( gen_random_uuid(), '{email}', '{password_hash}', '{first_name}', '{last_name}', 'active', 'superadmin', true, NOW(), NOW() ); """ def main(): print("=" * 70) print("LOAF Membership Platform - Superadmin User Creator") print("=" * 70) print() # Get user input email = input("Email address: ").strip() if not email or '@' not in email: print("❌ Invalid email address") sys.exit(1) first_name = input("First name: ").strip() if not first_name: print("❌ First name is required") sys.exit(1) last_name = input("Last name: ").strip() if not last_name: print("❌ Last name is required") sys.exit(1) # Get password securely password = getpass("Password: ") if len(password) < 8: print("❌ Password must be at least 8 characters") sys.exit(1) password_confirm = getpass("Confirm password: ") if password != password_confirm: print("❌ Passwords do not match") sys.exit(1) print() print("Generating password hash...") password_hash = generate_password_hash(password) print("✅ Password hash generated") print() print("=" * 70) print("SQL STATEMENT") print("=" * 70) sql = generate_sql(email, password_hash, first_name, last_name) print(sql) # Save to file output_file = "create_superadmin.sql" with open(output_file, 'w') as f: f.write(sql) print("=" * 70) print(f"✅ SQL saved to: {output_file}") print() print("Run this command to create the user:") print(f" psql -U postgres -d loaf_new -f {output_file}") print() print("Or copy the SQL above and run it directly in psql") print("=" * 70) if __name__ == "__main__": try: main() except KeyboardInterrupt: print("\n\n❌ Cancelled by user") sys.exit(1) except Exception as e: print(f"\n❌ Error: {e}") sys.exit(1)