Spaces:
Sleeping
Sleeping
File size: 2,013 Bytes
5d54b3c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | #!/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()
|