php session_start(); require_once 'config/database.php'; $database = new Database(); $pdo = $database->getConnection(); $login_error = ''; $signup_error = ''; $signup_success = ''; $showOtpInput = false; // ------------------- LOGIN PROCESS ------------------- if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'login') { $email = $_POST['email'] ?? ''; $password = $_POST['password'] ?? ''; try { $stmt = $pdo->prepare(" SELECT u.*, c.status AS company_status FROM users u JOIN companies c ON c.id = u.company_id WHERE u.email = ? AND u.status = 'active' "); $stmt->execute([$email]); $user = $stmt->fetch(PDO::FETCH_ASSOC); if (!$user || $password !== $user['password']) { $login_error = "Invalid email or password!"; } elseif ($user['company_status'] != 1) { $login_error = "Company not approved. Please verify OTP."; } else { $_SESSION['user_id'] = $user['id']; $_SESSION['company_id'] = $user['company_id']; $_SESSION['user_type'] = $user['user_type']; $_SESSION['user_name'] = $user['name']; switch ($user['user_type']) { case 'company_admin': header("Location: admin/dashboard.php"); break; case 'staff': header("Location: employee/dashboard.php"); break; case 'partner': header("Location: partner/dashboard.php"); break; case 'customer': header("Location: customer/dashboard.php"); break; default: header("Location: dashboard.php"); break; } exit(); } } catch (PDOException $e) { $login_error = "Database error. Please try again."; } } // ------------------- SIGNUP / OTP PROCESS ------------------- if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'signup') { // OTP verification block if (isset($_POST['otp_verify'])) { $otp = trim($_POST['otp']); if (!isset($_SESSION['signup_data'])) { $signup_error = "Session expired. Please signup again."; } else { $data = $_SESSION['signup_data']; if ($otp == $data['otp'] && time() <= $data['otp_expires']) { try { $pdo->beginTransaction(); $stmtCompany = $pdo->prepare(" INSERT INTO companies (company_name, contact_person, email, mobile, status, created_at) VALUES (?, ?, ?, ?, 1, NOW()) "); $stmtCompany->execute([ $data['company_name'], $data['name'], $data['email'], $data['phone'] ]); $company_id = $pdo->lastInsertId(); $stmtUser = $pdo->prepare(" INSERT INTO users (company_id, name, email, phone, password, user_type, registration_status, status, created_at) VALUES (?, ?, ?, ?, ?, 'admin', 'approved', 'active', NOW()) "); $stmtUser->execute([ $company_id, $data['name'], $data['email'], $data['phone'], $data['password'] ]); $pdo->commit(); unset($_SESSION['signup_data']); $signup_success = "Account created successfully! You can now login."; $showOtpInput = false; } catch (Exception $e) { $pdo->rollBack(); $signup_error = "Failed to create account: " . $e->getMessage(); $showOtpInput = true; } } else { $signup_error = "Invalid or expired OTP."; $showOtpInput = true; } } } else { // Signup form submitted $company_name = trim($_POST['company_name']); $name = trim($_POST['name']); $email = trim($_POST['email']); $phone = trim($_POST['phone']); $password = trim($_POST['password']); if (!$company_name || !$name || !$email || !$password) { $signup_error = "All fields marked with * are required."; } else { // Check if email exists $check = $pdo->prepare("SELECT id FROM companies WHERE email = ?"); $check->execute([$email]); if ($check->rowCount() > 0) { $signup_error = "Company already registered with this email."; } else { // Generate OTP $otp = rand(100000, 999999); $mailStmt = $pdo->prepare("SELECT * FROM mail_settings WHERE status='active' ORDER BY id DESC LIMIT 1"); $mailStmt->execute(); $mail = $mailStmt->fetch(PDO::FETCH_ASSOC); if (!$mail) { $signup_error = "Mail settings not configured."; } else { $headers = "From: {$mail['from_name']} <{$mail['from_email']}>\r\n"; $headers .= "Content-Type: text/html\r\n"; $subject = "Verify Your Company Account (OTP)"; $message = "
Hello {$name},
Your OTP for company verification is:
This OTP is valid for 10 minutes.
"; mail($email, $subject, $message, $headers); $_SESSION['signup_data'] = [ 'company_name' => $company_name, 'name' => $name, 'email' => $email, 'phone' => $phone, 'password' => $password, 'otp' => $otp, 'otp_expires' => time() + (10 * 60) ]; $signup_success = "OTP sent! Enter it below to verify."; $showOtpInput = true; } } } } } ?>Your Digital Business Profiles and Customer Connection Solutions. Join thousands of businesses growing with BeezQ.
Sign in to your BeezQ account
Start your journey with BeezQ