Create Your Brand Account

Start managing your content with Content Factory

Already have an account? Sign in here

$config = require '../config.php'; require_once '../lib/Util.php'; require_once '../lib/Crypto.php'; require_once '../lib/Db.php'; require_once '../lib/Auth.php'; // Initialize application $db = new Db($config['db']['dsn'], $config['db']['user'], $config['db']['pass']); $auth = new Auth($db); // Set timezone date_default_timezone_set($config['app']['timezone']); // Initialize crypto Crypto::init($config['app']['app_secret']); // Redirect if already logged in if ($auth->isLoggedIn()) { header('Location: /'); exit; } $error = ''; $success = ''; if ($_SERVER['REQUEST_METHOD'] === 'POST') { $brandName = trim($_POST['brand_name'] ?? ''); $brandSlug = Util::slugify($brandName); $email = trim($_POST['email'] ?? ''); $name = trim($_POST['name'] ?? ''); $password = $_POST['password'] ?? ''; $confirmPassword = $_POST['confirm_password'] ?? ''; // Validation if (empty($brandName) || empty($email) || empty($name) || empty($password)) { $error = 'All fields are required.'; } elseif (!Util::isValidEmail($email)) { $error = 'Please enter a valid email address.'; } elseif (strlen($password) < 6) { $error = 'Password must be at least 6 characters long.'; } elseif ($password !== $confirmPassword) { $error = 'Passwords do not match.'; } else { try { $db->beginTransaction(); // Check if email already exists $existingUser = $db->fetchOne("SELECT id FROM users WHERE email = ?", [$email]); if ($existingUser) { throw new Exception('Email address already exists.'); } // Ensure slug is unique $originalSlug = $brandSlug; $counter = 1; while (true) { $existingBrand = $db->fetchOne("SELECT id FROM brands WHERE slug = ?", [$brandSlug]); if (!$existingBrand) { break; } $brandSlug = $originalSlug . '-' . $counter; $counter++; } // Create user $userId = $db->insert( "INSERT INTO users (email, name, password_hash, role) VALUES (?, ?, ?, ?)", [$email, $name, password_hash($password, PASSWORD_DEFAULT), 'brand_owner'] ); // Create brand $brandId = $db->insert( "INSERT INTO brands (name, slug) VALUES (?, ?)", [$brandName, $brandSlug] ); // Link user to brand as owner $db->insert( "INSERT INTO user_brand_roles (user_id, brand_id, role) VALUES (?, ?, ?)", [$userId, $brandId, 'owner'] ); // Create default brand settings $db->insert( "INSERT INTO brand_settings (brand_id, wp_base_url, wp_username, wp_app_password_enc, openai_api_key_enc, brand_voice) VALUES (?, ?, ?, ?, ?, ?)", [ $brandId, '', '', Crypto::encrypt(''), Crypto::encrypt(''), 'You are a helpful content creator who writes engaging, informative blog posts for ' . $brandName . '.' ] ); $db->commit(); // Auto-login the user $auth->login($email, $password); // Set selected brand $_SESSION['selected_brand_id'] = $brandId; Util::logAudit($userId, $brandId, 'brand.signup', ['brand_name' => $brandName]); header('Location: /'); exit; } catch (Exception $e) { $db->rollback(); $error = $e->getMessage(); } } } $pageTitle = 'Sign Up'; include 'partials/head.php'; ?>

Create Your Brand Account

Start managing your content with Content Factory

Already have an account? Sign in here