enabling user registration

main
Steffen Pohle 2 weeks ago
parent 7559165749
commit 381e4b8fa0

@ -384,6 +384,42 @@ function findUserByEmail($email) {
return null;
}
function generateRegistrationCode() {
return str_pad((string) random_int(0, 999999), 6, '0', STR_PAD_LEFT);
}
function savePendingRegistration($username, $email, $password, $code) {
if (session_status() === PHP_SESSION_NONE) {
session_start();
}
$_SESSION['pending_registration'] = [
'username' => trim((string) $username),
'email' => trim((string) $email),
'password' => (string) $password,
'code' => (string) $code,
'createdAt' => time(),
];
return true;
}
function getPendingRegistration() {
if (session_status() === PHP_SESSION_NONE) {
session_start();
}
return $_SESSION['pending_registration'] ?? null;
}
function clearPendingRegistration() {
if (session_status() === PHP_SESSION_NONE) {
session_start();
}
unset($_SESSION['pending_registration']);
}
function createUser($username, $password, $role = 'user', $email = '') {
$username = trim((string) $username);
$email = trim((string) $email);

@ -5,6 +5,7 @@ define('OLLAMA_URL', 'http://127.0.0.1:11434');
define('GUEST_USER_ENABLED', true);
define('GUEST_USERNAME', 'guest');
define('DEFAULT_MODEL', 'llama3.2:latest');
define('ALLOW_REGISTRATION', true);
define('MAIL_FROM', getenv('MAIL_FROM') ?: 'noreply@kai.local');
define('MAIL_FROM_NAME', getenv('MAIL_FROM_NAME') ?: 'KaI');

@ -17,30 +17,55 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
exit;
}
$username = trim($_POST['username'] ?? '');
$password = (string) ($_POST['password'] ?? '');
if (($_POST['action'] ?? '') === 'register' && ALLOW_REGISTRATION) {
$regUsername = trim((string) ($_POST['register_username'] ?? ''));
$regEmail = trim((string) ($_POST['register_email'] ?? ''));
$regPassword = (string) ($_POST['register_password'] ?? '');
$regConfirmPassword = (string) ($_POST['register_confirm_password'] ?? '');
if ($username === '' || $password === '') {
$errors[] = 'Please enter both username and password.';
} else {
$user = findUserByUsername($username);
if ($user && verifyPassword($password, $user['passwordHash'] ?? '')) {
if ($regUsername === '' || $regPassword === '' || $regConfirmPassword === '') {
$errors[] = 'Please enter a username and password for registration.';
} elseif ($regPassword !== $regConfirmPassword) {
$errors[] = 'Passwords do not match.';
} elseif (filter_var($regEmail, FILTER_VALIDATE_EMAIL) === false && $regEmail !== '') {
$errors[] = 'Please enter a valid email address.';
} elseif (createUser($regUsername, $regPassword, 'user', $regEmail)) {
$_SESSION['auth_user'] = [
'username' => $user['username'],
'role' => $user['role'] ?? 'user'
'username' => $regUsername,
'role' => 'user'
];
if (!empty($_POST['remember'])) {
setRememberMeCookie($user['username']);
} else {
clearRememberMeCookie($user['username']);
}
header('Location: index.php');
exit;
} else {
$errors[] = 'Registration failed. The username or email may already be in use.';
}
} else {
$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'
];
if (!empty($_POST['remember'])) {
setRememberMeCookie($user['username']);
} else {
clearRememberMeCookie($user['username']);
}
$errors[] = 'Invalid username or password.';
header('Location: index.php');
exit;
}
$errors[] = 'Invalid username or password.';
}
}
}
@ -87,6 +112,7 @@ if (empty(loadUsers())) {
</form>
<?php endif; ?>
<form method="post">
<input type="hidden" name="action" value="login">
<label for="username">Username</label>
<input id="username" name="username" required>
@ -100,6 +126,12 @@ if (empty(loadUsers())) {
<button type="submit">Sign in</button>
</form>
<?php if (ALLOW_REGISTRATION): ?>
<div class="small" style="margin-top:16px;">No account yet?</div>
<div style="margin-top:8px;">
<a href="register.php" style="color:#89b4fa; font-weight:bold; text-decoration:none;">Create a new account</a>
</div>
<?php endif; ?>
<?php if (!empty($errors)): ?>
<div class="error">
<?php echo htmlspecialchars(implode('<br>', $errors)); ?>

Loading…
Cancel
Save