99 lines
3.5 KiB
PHP
99 lines
3.5 KiB
PHP
<?php
|
|
session_start();
|
|
require_once 'db/config.php';
|
|
|
|
// If user doesn't need 2FA, redirect away
|
|
if (!isset($_SESSION['user_id_2fa'])) {
|
|
header('Location: login.php');
|
|
exit;
|
|
}
|
|
|
|
$error_message = '';
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$verification_code = $_POST['verification_code'] ?? '';
|
|
|
|
if (empty($verification_code)) {
|
|
$error_message = 'Please enter the verification code.';
|
|
} else {
|
|
try {
|
|
$pdo = db();
|
|
$stmt = $pdo->prepare('SELECT * FROM users WHERE id = ?');
|
|
$stmt->execute([$_SESSION['user_id_2fa']]);
|
|
$user = $stmt->fetch();
|
|
|
|
if ($user && $user['two_factor_secret'] === $verification_code) {
|
|
// Correct code, log the user in
|
|
$stmt = $pdo->prepare('UPDATE users SET two_factor_secret = NULL WHERE id = ?');
|
|
$stmt->execute([$_SESSION['user_id_2fa']]);
|
|
|
|
unset($_SESSION['user_id_2fa']);
|
|
|
|
$_SESSION['user_id'] = $user['id'];
|
|
$_SESSION['username'] = $user['username'];
|
|
$_SESSION['role'] = $user['role'];
|
|
|
|
header('Location: profile.php');
|
|
exit;
|
|
} else {
|
|
$error_message = 'Invalid verification code. Please try again.';
|
|
}
|
|
} catch (PDOException $e) {
|
|
$error_message = 'Database error. Please try again later.';
|
|
}
|
|
}
|
|
}
|
|
?>
|
|
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>Two-Factor Authentication - AI Recipe Generator</title>
|
|
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
|
|
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap">
|
|
<link rel="stylesheet" href="assets/css/custom.css?v=<?php echo time(); ?>">
|
|
</head>
|
|
<body>
|
|
|
|
<!-- Navbar -->
|
|
<nav class="navbar navbar-expand-lg navbar-light bg-white fixed-top">
|
|
<div class="container">
|
|
<a class="navbar-brand" href="index.php">AI Recipe Generator</a>
|
|
</div>
|
|
</nav>
|
|
|
|
<!-- Main Content -->
|
|
<main>
|
|
<div class="container py-5">
|
|
<div class="auth-container">
|
|
<h2 class="text-center">Two-Factor Authentication</h2>
|
|
<p class="text-center text-muted mb-4">A code has been sent to your phone. Please enter it below to complete your login.</p>
|
|
|
|
<?php if ($error_message): ?>
|
|
<div class="alert alert-danger"><?php echo htmlspecialchars($error_message); ?></div>
|
|
<?php endif; ?>
|
|
|
|
<form action="login_2fa.php" method="POST">
|
|
<div class="mb-3">
|
|
<label for="verification_code" class="form-label">Verification Code</label>
|
|
<input type="text" class="form-control" id="verification_code" name="verification_code" required autofocus>
|
|
</div>
|
|
<div class="d-grid">
|
|
<button type="submit" class="btn btn-primary">Login</button>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</main>
|
|
|
|
<!-- Footer -->
|
|
<footer class="footer">
|
|
<div class="container text-center">
|
|
<p>© <?php echo date("Y"); ?> AI Recipe Generator. All Rights Reserved.</p>
|
|
</div>
|
|
</footer>
|
|
|
|
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
|
|
</body>
|
|
</html>
|