Files
Fociz/auth/invite.php
T

188 lines
8.7 KiB
PHP

<?php
/**
* Fociz Uitnodiging Accepteren & Account Registratie met 2FA
*/
require_once __DIR__ . '/../includes/auth.php';
start_secure_session();
$token = trim($_GET['token'] ?? ($_POST['token'] ?? ''));
$invite = validate_invite($token);
if (!$invite) {
die("
<!DOCTYPE html>
<html lang='nl' class='dark'>
<head><script src='https://cdn.tailwindcss.com'></script></head>
<body class='bg-[#090d16] text-white min-h-screen flex items-center justify-center p-4'>
<div class='max-w-md bg-slate-900 border border-slate-800 rounded-2xl p-8 text-center'>
<h2 class='text-xl font-bold text-red-400 mb-2'>Ongeldige Uitnodiging</h2>
<p class='text-sm text-slate-400 mb-6'>Deze uitnodigingslink is ongeldig, verlopen of al eerder gebruikt.</p>
<a href='/auth/login.php' class='px-4 py-2 bg-emerald-500 text-slate-950 font-bold rounded-xl text-xs'>Naar Inloggen</a>
</div>
</body>
</html>
");
}
$error = null;
if (empty($_SESSION['invite_totp_secret'])) {
$_SESSION['invite_totp_secret'] = TOTP::generateSecret(16);
}
$secret = $_SESSION['invite_totp_secret'];
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$name = trim($_POST['name'] ?? '');
$password = $_POST['password'] ?? '';
$confirmPassword = $_POST['confirm_password'] ?? '';
$totpCode = trim($_POST['totp_code'] ?? '');
if (empty($name) || empty($password) || empty($totpCode)) {
$error = "Alle velden (inclusief 2FA verificatiecode) zijn verplicht.";
} elseif (strlen($password) < 8) {
$error = "Het wachtwoord moet minimaal 8 tekens lang zijn.";
} elseif ($password !== $confirmPassword) {
$error = "Wachtwoorden komen niet overeen.";
} elseif (!TOTP::verifyCode($secret, $totpCode)) {
$error = "Ongeldige 2FA verificatiecode. Controleer de tijd op uw smartphone.";
} else {
try {
$db = get_db_connection();
$passwordHash = password_hash($password, PASSWORD_DEFAULT);
$backupCodes = TOTP::generateBackupCodes(8);
$db->beginTransaction();
// 1. Maak gebruiker aan
$stmt = $db->prepare("
INSERT INTO users (name, email, password_hash, role, two_factor_secret, two_factor_enabled, backup_codes)
VALUES (?, ?, ?, ?, ?, 1, ?)
");
$stmt->execute([
$name,
$invite['email'],
$passwordHash,
$invite['role'],
$secret,
json_encode($backupCodes)
]);
$newUserId = (int)$db->lastInsertId();
// 2. Markeer invite als gebruikt
$stmtInvite = $db->prepare("UPDATE invites SET used_at = NOW() WHERE id = ?");
$stmtInvite->execute([$invite['id']]);
$db->commit();
// Start sessie
$_SESSION['user_id'] = $newUserId;
$_SESSION['user_name'] = $name;
$_SESSION['user_email'] = $invite['email'];
$_SESSION['user_role'] = $invite['role'];
$_SESSION['is_2fa_verified'] = true;
unset($_SESSION['invite_totp_secret']);
header('Location: /planner.php');
exit;
} catch (Exception $e) {
if ($db->inTransaction()) $db->rollBack();
$error = "Fout bij registreren: " . $e->getMessage();
}
}
}
$otpAuthUrl = TOTP::getOtpAuthUrl($invite['email'], $secret, 'Fociz');
$qrCodeUrl = TOTP::getQrCodeUrl($otpAuthUrl, 180);
$pageTitle = "Fociz — Uitnodiging Accepteren";
?>
<!DOCTYPE html>
<html lang="nl" class="dark">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><?= htmlspecialchars($pageTitle) ?></title>
<script src="https://cdn.tailwindcss.com"></script>
<script src="https://unpkg.com/lucide@latest"></script>
<style>body { background-color: #090d16; color: #f1f5f9; }</style>
</head>
<body class="min-h-screen flex items-center justify-center p-4 bg-gradient-to-br from-[#090d16] via-[#0f172a] to-[#090d16]">
<?php require_once __DIR__ . '/../includes/env_badge.php'; ?>
<div class="w-full max-w-lg bg-slate-900/90 border border-slate-800 rounded-2xl shadow-2xl backdrop-blur-xl p-8">
<div class="text-center mb-8">
<div class="inline-flex items-center justify-center w-14 h-14 rounded-2xl bg-emerald-500/10 border border-emerald-500/30 text-emerald-400 mb-3 shadow-lg shadow-emerald-500/10">
<i data-lucide="shield-check" class="w-8 h-8"></i>
</div>
<h1 class="text-2xl font-bold text-white">Welkom bij Fociz</h1>
<p class="text-slate-400 text-sm mt-1">
Uitgenodigd door <strong class="text-slate-200"><?= htmlspecialchars($invite['inviter_name']) ?></strong> voor <span class="text-emerald-400 font-mono text-xs bg-emerald-500/10 px-2 py-0.5 rounded"><?= htmlspecialchars($invite['email']) ?></span>
</p>
</div>
<?php if ($error): ?>
<div class="mb-6 p-4 rounded-xl bg-red-500/10 border border-red-500/30 flex items-start space-x-3 text-red-400 text-sm">
<i data-lucide="alert-triangle" class="w-5 h-5 flex-shrink-0 mt-0.5"></i>
<span><?= htmlspecialchars($error) ?></span>
</div>
<?php endif; ?>
<form method="POST" action="/auth/invite.php" class="space-y-4">
<input type="hidden" name="token" value="<?= htmlspecialchars($token) ?>">
<div>
<label class="block text-xs font-medium text-slate-300 mb-1">Uw naam</label>
<input type="text" name="name" required placeholder="Bijv. Sarah de Vries" value="<?= htmlspecialchars($_POST['name'] ?? '') ?>" class="w-full bg-slate-800/80 border border-slate-700 rounded-xl px-4 py-2.5 text-white placeholder-slate-500 focus:outline-none focus:ring-2 focus:ring-emerald-500/50 text-sm transition">
</div>
<div class="grid grid-cols-1 sm:grid-cols-2 gap-3">
<div>
<label class="block text-xs font-medium text-slate-300 mb-1">Wachtwoord (min. 8 tekens)</label>
<input type="password" name="password" required placeholder="••••••••" class="w-full bg-slate-800/80 border border-slate-700 rounded-xl px-4 py-2.5 text-white placeholder-slate-500 focus:outline-none focus:ring-2 focus:ring-emerald-500/50 text-sm transition">
</div>
<div>
<label class="block text-xs font-medium text-slate-300 mb-1">Bevestig wachtwoord</label>
<input type="password" name="confirm_password" required placeholder="••••••••" class="w-full bg-slate-800/80 border border-slate-700 rounded-xl px-4 py-2.5 text-white placeholder-slate-500 focus:outline-none focus:ring-2 focus:ring-emerald-500/50 text-sm transition">
</div>
</div>
<!-- 2FA Verplichting -->
<div class="mt-6 pt-6 border-t border-slate-800 text-center space-y-4">
<h3 class="text-sm font-bold text-white uppercase tracking-wider text-emerald-400">
Verplichte Tweestapsverificatie (2FA)
</h3>
<p class="text-xs text-slate-400">
Scan deze QR-code in uw authenticator app (Google Authenticator, Microsoft Authenticator of 1Password):
</p>
<div class="inline-block p-3 bg-white rounded-2xl shadow-xl">
<img src="<?= htmlspecialchars($qrCodeUrl) ?>" alt="2FA QR Code" class="w-40 h-40 mx-auto">
</div>
<div class="bg-slate-800/60 border border-slate-700/80 rounded-xl p-2.5 text-xs text-slate-400 max-w-sm mx-auto font-mono">
Sleutel: <strong class="text-slate-200"><?= htmlspecialchars($secret) ?></strong>
</div>
<div>
<label class="block text-xs font-medium text-slate-300 mb-1.5">
Voer de 6-cijferige code uit uw app in:
</label>
<input type="text" name="totp_code" required maxlength="6" placeholder="123456" class="w-48 mx-auto block text-center tracking-[0.5em] font-mono text-xl bg-slate-800 border border-slate-700 rounded-xl py-2 text-white focus:outline-none focus:ring-2 focus:ring-emerald-500/50 transition">
</div>
</div>
<button type="submit" class="w-full mt-6 bg-emerald-500 hover:bg-emerald-600 text-slate-950 font-bold py-3 px-4 rounded-xl transition shadow-lg shadow-emerald-500/20 flex items-center justify-center space-x-2">
<span>Account Activeren & Inloggen</span>
<i data-lucide="check-circle-2" class="w-4 h-4"></i>
</button>
</form>
</div>
<script>
if (typeof lucide !== 'undefined') lucide.createIcons();
</script>
</body>
</html>