Tech Stack Advisor - Code Viewer

← Back to File Tree

update_admin_password.py

Language: python | Path: scripts/update_admin_password.py | Lines: 75
#!/usr/bin/env python3
"""Update admin user password."""
import sys
import argparse
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
import hashlib

setup_logging()
logger = get_logger(__name__)

# Admin email
ADMIN_EMAIL = "admin@techstackadvisor.com"


def update_admin_password(new_password: str):
    """Update the admin user password."""
    print("\n" + "="*60)
    print("  Updating Admin Password")
    print("="*60)

    try:
        memory_store = get_user_memory_store()

        # Get existing admin user
        user = memory_store.get_user_by_email(ADMIN_EMAIL)
        if not user:
            print(f"\nāŒ Admin user not found: {ADMIN_EMAIL}")
            sys.exit(1)

        # Hash new password
        hashed_password = hashlib.sha256(new_password.encode()).hexdigest()

        # Update password
        success = memory_store.update_user(
            user["user_id"],
            {"hashed_password": hashed_password}
        )

        if success:
            print(f"\nāœ… Admin password updated successfully!")
            print(f"   Email: {ADMIN_EMAIL}")
            print(f"   User ID: {user['user_id']}")

            print(f"\nšŸ” You can now login with the new password")
        else:
            print(f"\nāŒ Failed to update password")
            sys.exit(1)

        print("\n" + "="*60 + "\n")

    except Exception as e:
        print(f"\nāŒ Error: {e}")
        logger.error("password_update_error", error=str(e))
        import traceback
        traceback.print_exc()
        sys.exit(1)


if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Update admin user password")
    parser.add_argument(
        "--password",
        required=True,
        help="New password for admin user"
    )

    args = parser.parse_args()
    update_admin_password(args.password)