fix 12
This commit is contained in:
parent
2497a13797
commit
b95f5763dd
@ -1,13 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
error_log("--- auth-check.php: Start ---");
|
|
||||||
error_log("auth-check.php: Session status: " . session_status());
|
|
||||||
error_log("auth-check.php: Session ID: " . session_id());
|
|
||||||
error_log("auth-check.php: Checking for user_id in \$_SESSION: " . print_r($_SESSION, true));
|
|
||||||
|
|
||||||
if (!isset($_SESSION['user_id'])) {
|
if (!isset($_SESSION['user_id'])) {
|
||||||
error_log("auth-check.php: user_id NOT SET. Redirecting to login.php");
|
|
||||||
header("Location: login.php");
|
header("Location: login.php");
|
||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
error_log("auth-check.php: user_id IS SET. User is authenticated.");
|
|
||||||
?>
|
?>
|
||||||
@ -23,22 +23,5 @@ function db() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Migration logic
|
|
||||||
$pdo->exec('CREATE TABLE IF NOT EXISTS migrations (migration VARCHAR(255) PRIMARY KEY)');
|
|
||||||
$ran_migrations = $pdo->query('SELECT migration FROM migrations')->fetchAll(PDO::FETCH_COLUMN);
|
|
||||||
|
|
||||||
$migration_files = glob(__DIR__ . '/migrations/*.sql');
|
|
||||||
sort($migration_files);
|
|
||||||
|
|
||||||
foreach ($migration_files as $file) {
|
|
||||||
$migration_name = basename($file);
|
|
||||||
if (!in_array($migration_name, $ran_migrations)) {
|
|
||||||
$sql = file_get_contents($file);
|
|
||||||
$pdo->exec($sql);
|
|
||||||
$stmt = $pdo->prepare('INSERT INTO migrations (migration) VALUES (?)');
|
|
||||||
$stmt->execute([$migration_name]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return $pdo;
|
return $pdo;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,9 +1,5 @@
|
|||||||
<?php
|
<?php
|
||||||
session_start();
|
session_start();
|
||||||
error_log("--- index.php: Start ---");
|
|
||||||
error_log("index.php: Session status: " . session_status());
|
|
||||||
error_log("index.php: Session ID: " . session_id());
|
|
||||||
error_log("index.php: Initial \$_SESSION: " . print_r($_SESSION, true));
|
|
||||||
require_once 'db/config.php';
|
require_once 'db/config.php';
|
||||||
require_once 'auth-check.php';
|
require_once 'auth-check.php';
|
||||||
require_once 'auth-helpers.php';
|
require_once 'auth-helpers.php';
|
||||||
|
|||||||
11
login.php
11
login.php
@ -1,28 +1,21 @@
|
|||||||
<?php
|
<?php
|
||||||
session_start();
|
session_start();
|
||||||
error_log("--- login.php: Start ---");
|
|
||||||
error_log("login.php: Session status: " . session_status());
|
|
||||||
error_log("login.php: Session ID: " . session_id());
|
|
||||||
error_log("login.php: Initial \$_SESSION: " . print_r($_SESSION, true));
|
|
||||||
require_once 'db/config.php';
|
require_once 'db/config.php';
|
||||||
|
|
||||||
$error_message = '';
|
$error_message = '';
|
||||||
|
|
||||||
// If user is already logged in, redirect to dashboard
|
// If user is already logged in, redirect to dashboard
|
||||||
if (isset($_SESSION['user_id'])) {
|
if (isset($_SESSION['user_id'])) {
|
||||||
error_log("login.php: User already logged in. Redirecting to index.php");
|
|
||||||
header("Location: index.php");
|
header("Location: index.php");
|
||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||||
error_log("login.php: POST request received.");
|
|
||||||
$email = $_POST['email'] ?? '';
|
$email = $_POST['email'] ?? '';
|
||||||
$password = $_POST['password'] ?? '';
|
$password = $_POST['password'] ?? '';
|
||||||
|
|
||||||
if (empty($email) || empty($password)) {
|
if (empty($email) || empty($password)) {
|
||||||
$error_message = 'Please enter both email and password.';
|
$error_message = 'Please enter both email and password.';
|
||||||
error_log("login.php: " . $error_message);
|
|
||||||
} else {
|
} else {
|
||||||
try {
|
try {
|
||||||
$pdo = db();
|
$pdo = db();
|
||||||
@ -31,20 +24,16 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|||||||
$user = $stmt->fetch(PDO::FETCH_ASSOC);
|
$user = $stmt->fetch(PDO::FETCH_ASSOC);
|
||||||
|
|
||||||
if ($user && password_verify($password, $user['password'])) {
|
if ($user && password_verify($password, $user['password'])) {
|
||||||
error_log("login.php: Login successful for user ID: " . $user['id']);
|
|
||||||
$_SESSION['user_id'] = $user['id'];
|
$_SESSION['user_id'] = $user['id'];
|
||||||
$_SESSION['user_name'] = $user['name'];
|
$_SESSION['user_name'] = $user['name'];
|
||||||
$_SESSION['user_role'] = $user['role'];
|
$_SESSION['user_role'] = $user['role'];
|
||||||
error_log("login.php: \$_SESSION after login: " . print_r($_SESSION, true));
|
|
||||||
header("Location: index.php");
|
header("Location: index.php");
|
||||||
exit;
|
exit;
|
||||||
} else {
|
} else {
|
||||||
$error_message = 'Invalid email or password.';
|
$error_message = 'Invalid email or password.';
|
||||||
error_log("login.php: " . $error_message);
|
|
||||||
}
|
}
|
||||||
} catch (PDOException $e) {
|
} catch (PDOException $e) {
|
||||||
$error_message = 'Database error: ' . $e->getMessage();
|
$error_message = 'Database error: ' . $e->getMessage();
|
||||||
error_log("login.php: " . $error_message);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,23 +0,0 @@
|
|||||||
<?php
|
|
||||||
require_once 'db/config.php';
|
|
||||||
|
|
||||||
// This script will run all migrations and then delete itself.
|
|
||||||
|
|
||||||
$pdo = db();
|
|
||||||
$migrations_dir = __DIR__ . '/db/migrations';
|
|
||||||
$files = glob($migrations_dir . '/*.sql');
|
|
||||||
|
|
||||||
foreach ($files as $file) {
|
|
||||||
$sql = file_get_contents($file);
|
|
||||||
try {
|
|
||||||
$pdo->exec($sql);
|
|
||||||
echo "Successfully ran migration: " . basename($file) . "<br>";
|
|
||||||
} catch (PDOException $e) {
|
|
||||||
echo "Error running migration: " . basename($file) . " - " . $e->getMessage() . "<br>";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Self-destruct
|
|
||||||
unlink(__FILE__);
|
|
||||||
|
|
||||||
echo "<br>All migrations have been processed. This script has been deleted.";
|
|
||||||
Loading…
x
Reference in New Issue
Block a user