diff --git a/backend/app/auth.py b/backend/app/auth.py index b250747..09d0770 100644 --- a/backend/app/auth.py +++ b/backend/app/auth.py @@ -90,11 +90,19 @@ def get_user_from_token(token: str) -> Optional[dict]: def ensure_admin_exists(): - """Called on startup — creates the admin user if no users exist.""" + """Called on startup — creates the admin user if no users exist, + and ensures the 'default' campaign is owned by the admin.""" conn = db.get_conn() row = conn.execute("SELECT COUNT(*) as cnt FROM users").fetchone() - if row["cnt"] > 0: - return - pw = ADMIN_PASSWORD or os.urandom(8).hex() - db.create_user(ADMIN_USERNAME, hash_password(pw), is_admin=True) - print(f"[auth] Created admin user '{ADMIN_USERNAME}' (password: {pw})") + if row["cnt"] == 0: + pw = ADMIN_PASSWORD or os.urandom(8).hex() + db.create_user(ADMIN_USERNAME, hash_password(pw), is_admin=True) + print(f"[auth] Created admin user '{ADMIN_USERNAME}' (password: {pw})") + + # Claim the "default" campaign for the admin so setup wizard works + admin = db.get_user_by_username(ADMIN_USERNAME) + if admin: + conn.execute( + "UPDATE campaigns SET created_by = ? WHERE id = 'default' AND created_by IS NULL", + (admin["id"],), + )