ā Back to File Tree
create_admin.py
Language: python |
Path: scripts/create_admin.py |
Lines: 112
#!/usr/bin/env python3
"""Script to create an admin user."""
import sys
from pathlib import Path
# Add backend to path
backend_path = Path(__file__).parent.parent / "backend"
sys.path.insert(0, str(backend_path))
from src.core.user_memory import get_user_memory_store
from src.core.logging import setup_logging, get_logger
setup_logging()
logger = get_logger(__name__)
def create_admin_user(email: str, password: str, full_name: str = "Admin User"):
"""Create an admin user.
Args:
email: Admin email
password: Admin password
full_name: Admin's full name
"""
print(f"\nš§ Creating admin user: {email}")
try:
memory_store = get_user_memory_store()
# Check if user already exists
existing = memory_store.get_user_by_email(email)
if existing:
print(f"ā User with email {email} already exists!")
# Ask if they want to make them admin
response = input("Make this user an admin? (y/n): ").strip().lower()
if response == 'y':
success = memory_store.update_user(existing["user_id"], {"is_admin": True})
if success:
print(f"ā
User {email} is now an admin!")
else:
print(f"ā Failed to update user")
return
else:
print("Cancelled.")
return
# Create new admin user
user = memory_store.create_user(
email=email,
password=password,
full_name=full_name,
is_admin=True,
)
print(f"\nā
Admin user created successfully!")
print(f" Email: {user['email']}")
print(f" User ID: {user['user_id']}")
print(f" Is Admin: {user['is_admin']}")
print(f"\nš You can now login with:")
print(f" curl -X POST http://localhost:8000/auth/login \\")
print(f" -H 'Content-Type: application/json' \\")
print(f" -d '{{\"email\":\"{email}\",\"password\":\"{password}\"}}'")
except Exception as e:
print(f"\nā Error creating admin user: {e}")
logger.error("admin_creation_error", error=str(e))
sys.exit(1)
def main():
"""Main function."""
print("\n" + "="*60)
print(" Tech Stack Advisor - Create Admin User")
print("="*60)
# Get user input
email = input("\nAdmin Email: ").strip()
if not email or "@" not in email:
print("ā Invalid email address")
sys.exit(1)
password = input("Admin Password (min 8 chars): ").strip()
if len(password) < 8:
print("ā Password must be at least 8 characters")
sys.exit(1)
full_name = input("Full Name (optional, press Enter to skip): ").strip()
if not full_name:
full_name = "Admin User"
# Confirm
print(f"\nš Summary:")
print(f" Email: {email}")
print(f" Name: {full_name}")
print(f" Role: Admin")
confirm = input("\nCreate this admin user? (y/n): ").strip().lower()
if confirm != 'y':
print("Cancelled.")
sys.exit(0)
# Create admin
create_admin_user(email, password, full_name)
print("\n" + "="*60)
print(" Admin user creation complete!")
print("="*60 + "\n")
if __name__ == "__main__":
main()