Spaces:
Sleeping
Sleeping
| #!/usr/bin/env python3 | |
| """ | |
| Create a new user for Hawbeez Ad Creative Studio. | |
| Run from repo root: python backend/scripts/create_user.py --username admin --password yourpassword | |
| Or from backend: python scripts/create_user.py --username admin --password yourpassword | |
| """ | |
| import argparse | |
| import json | |
| import sys | |
| from pathlib import Path | |
| # Add backend to path so app.auth is importable | |
| backend_dir = Path(__file__).resolve().parent.parent | |
| sys.path.insert(0, str(backend_dir)) | |
| from app.auth import DATA_DIR, USERS_PATH, get_user_by_username, hash_password | |
| def main(): | |
| parser = argparse.ArgumentParser(description="Create a new Hawbeez user") | |
| parser.add_argument("--username", "-u", required=True, help="Username") | |
| parser.add_argument("--password", "-p", required=True, help="Password") | |
| args = parser.parse_args() | |
| username = (args.username or "").strip() | |
| password = (args.password or "").strip() | |
| if not username: | |
| print("Error: username is required and cannot be empty.", file=sys.stderr) | |
| sys.exit(1) | |
| if not password: | |
| print("Error: password is required and cannot be empty.", file=sys.stderr) | |
| sys.exit(1) | |
| if get_user_by_username(username): | |
| print(f"Error: User '{username}' already exists.", file=sys.stderr) | |
| sys.exit(1) | |
| DATA_DIR.mkdir(parents=True, exist_ok=True) | |
| users = [] | |
| if USERS_PATH.exists(): | |
| try: | |
| with open(USERS_PATH, "r") as f: | |
| users = json.load(f) | |
| except (json.JSONDecodeError, OSError): | |
| users = [] | |
| next_id = max((u.get("id") or 0) for u in users) + 1 if users else 1 | |
| new_user = { | |
| "id": next_id, | |
| "username": username, | |
| "password_hash": hash_password(password), | |
| } | |
| users.append(new_user) | |
| with open(USERS_PATH, "w") as f: | |
| json.dump(users, f, indent=2) | |
| print(f"User '{username}' created successfully (id={next_id}).") | |
| print(f"Users file: {USERS_PATH}") | |
| if __name__ == "__main__": | |
| main() | |