📄 Source: login.php
<?php
require_once 'includes/config.php';
require_once 'includes/db.php';
require_once 'includes/auth.php';
require_once 'includes/functions.php';
$error = '';
$success = '';
$redirect = $_GET['redirect'] ?? 'index.php';
$action = $_GET['action'] ?? 'login'; // 'login' or 'link'
// If already logged in
if (isLoggedIn()) {
if ($action === 'link') {
// Handle account linking
$user = getCurrentUser();
} else {
header('Location: ' . $redirect);
exit();
}
}
// Handle login
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$username = sanitize($_POST['username'] ?? '');
$password = $_POST['password'] ?? '';
if (empty($username) || empty($password)) {
$error = 'Please enter both username and password';
} else {
$user = getUserByUsername($username);
if ($user && password_verify($password, $user['password'])) {
loginUser($user['id']);
header('Location: ' . $redirect);
exit();
} else {
$error = 'Invalid username or password';
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><?php echo $action === 'link' ? 'Link Account' : 'Login'; ?> - <?php echo SITE_NAME; ?></title>
<link href="https://fonts.googleapis.com/css2?family=Inter:opsz,wght@14..32,300;400;500;600;700;800&display=swap" rel="stylesheet">
<link rel="stylesheet" href="assets/style.css">
</head>
<body>
<div class="theme-switcher">
<button class="theme-btn" data-theme="light" title="Light mode">☀️</button>
<button class="theme-btn" data-theme="dark" title="Dark mode">🌙</button>
</div>
<div class="container">
<div class="auth-container">
<div class="auth-card">
<h2><?php echo $action === 'link' ? '🔗 Link Account' : '🔐 Login'; ?></h2>
<p class="auth-sub">
<?php if ($action === 'link'): ?>
Link your Discord or Google account to your local account
<?php else: ?>
Sign in to access application forums
<?php endif; ?>
</p>
<?php if ($error): ?>
<div class="error-message">⚠️ <?php echo htmlspecialchars($error); ?></div>
<?php endif; ?>
<?php if ($success): ?>
<div class="success-message">✅ <?php echo htmlspecialchars($success); ?></div>
<?php endif; ?>
<?php if ($action === 'login'): ?>
<form method="POST" action="">
<div class="input-field">
<label>Username</label>
<input type="text" name="username" placeholder="Enter your username" required>
</div>
<div class="input-field">
<label>Password</label>
<input type="password" name="password" placeholder="Enter your password" required>
</div>
<button type="submit" class="btn-primary" style="width: 100%;">Login</button>
</form>
<div class="auth-divider">
<span>or</span>
</div>
<?php endif; ?>
<div class="oauth-buttons">
<?php if ($action === 'link'): ?>
<p style="text-align: center; color: var(--text-secondary); font-size: 0.9rem; margin-bottom: 0.5rem;">
Link your accounts to use them for applications
</p>
<?php endif; ?>
<a href="https://discord.com/api/oauth2/authorize?client_id=<?php echo DISCORD_CLIENT_ID; ?>&redirect_uri=<?php echo urlencode(DISCORD_REDIRECT_URI); ?>&response_type=code&scope=identify%20email%20guilds<?php echo $action === 'link' ? '&state=link' : ''; ?>" class="oauth-btn discord-btn">
🟣 <?php echo $action === 'link' ? 'Link' : 'Login with'; ?> Discord
</a>
<a href="https://accounts.google.com/o/oauth2/v2/auth?client_id=<?php echo GOOGLE_CLIENT_ID; ?>&redirect_uri=<?php echo urlencode(GOOGLE_REDIRECT_URI); ?>&response_type=code&scope=email%20profile%20openid&access_type=online<?php echo $action === 'link' ? '&state=link' : ''; ?>" class="oauth-btn google-btn">
🔴 <?php echo $action === 'link' ? 'Link' : 'Login with'; ?> Google
</a>
</div>
<?php if ($action === 'login'): ?>
<p class="auth-footer">
Don't have an account? <a href="register.php">Register here</a>
</p>
<?php endif; ?>
</div>
</div>
<footer class="footer">
<p>📋 <?php echo SITE_NAME; ?> v1.0</p>
</footer>
</div>
<script src="assets/script.js"></script>
</body>
</html>
← Back