You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

93 lines
3.2 KiB

<?php
if (session_status() === PHP_SESSION_NONE) {
session_start();
}
require_once __DIR__ . '/default.php';
require_once __DIR__ . '/auth.php';
$errors = [];
$message = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$username = trim($_POST['username'] ?? '');
$password = (string) ($_POST['password'] ?? '');
if ($username === '' || $password === '') {
$errors[] = 'Please enter both username and password.';
} else {
$user = findUserByUsername($username);
if ($user && verifyPassword($password, $user['passwordHash'] ?? '')) {
$_SESSION['auth_user'] = [
'username' => $user['username'],
'role' => $user['role'] ?? 'user'
];
header('Location: index.php');
exit;
}
$errors[] = 'Invalid username or password.';
}
}
if (isLoggedIn()) {
header('Location: index.php');
exit;
}
if (!file_exists(CONFIG_FILE)) {
ensureConfigDirectory();
}
if (empty(loadUsers())) {
$created = createUser('admin', 'changeme', 'admin', 'admin@example.com');
if ($created) {
$message = 'Default admin account created. Username: admin / Password: changeme';
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Login - KaI</title>
<style>
body { font-family: Arial, sans-serif; background: #1e1e2e; color: #cdd6f4; display: flex; align-items: center; justify-content: center; min-height: 100vh; margin: 0; }
.card { background: #11111b; padding: 24px; border-radius: 10px; width: 360px; box-shadow: 0 10px 30px rgba(0,0,0,0.3); }
h1 { margin-top: 0; font-size: 1.4rem; }
label { display: block; margin-top: 12px; font-weight: bold; }
input { width: 100%; padding: 10px; margin-top: 6px; border-radius: 6px; border: 1px solid #45475a; background: #313244; color: #fff; }
button { margin-top: 16px; width: 100%; padding: 10px; border: none; border-radius: 6px; background: #89b4fa; color: #11111b; font-weight: bold; cursor: pointer; }
.error { color: #f38ba8; margin-top: 12px; }
.message { color: #a6e3a1; margin-top: 12px; }
.small { font-size: 0.9rem; color: #999; margin-top: 12px; }
</style>
</head>
<body>
<div class="card">
<h1>KaI Login</h1>
<form method="post">
<label for="username">Username</label>
<input id="username" name="username" required>
<label for="password">Password</label>
<input id="password" name="password" type="password" required>
<button type="submit">Sign in</button>
</form>
<?php if (!empty($errors)): ?>
<div class="error">
<?php echo htmlspecialchars(implode('<br>', $errors)); ?>
</div>
<?php endif; ?>
<?php if ($message !== ''): ?>
<div class="message"><?php echo htmlspecialchars($message); ?></div>
<?php endif; ?>
<div class="small">Default admin account is created on first run: admin / changeme</div>
<div class="small"><a href="forgot.php" style="color:#89b4fa;">Forgot password?</a></div>
</div>
</body>
</html>