Initial commit: struttura base Diplomacy Web App con header/footer e auth

This commit is contained in:
2025-11-21 16:04:22 +00:00
commit 052b8182aa
17 changed files with 367 additions and 0 deletions

53
auth/login.php Executable file
View File

@@ -0,0 +1,53 @@
<?php
require_once __DIR__ . '/../init.php';
if (is_logged_in()) header('Location: ../game/dashboard.php');
$error = null;
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$email = $_POST['email'] ?? '';
$pass = $_POST['password'] ?? '';
$pdo = getPDO();
$stmt = $pdo->prepare("SELECT id, password_hash FROM users WHERE email = ?");
$stmt->execute([$email]);
$row = $stmt->fetch();
if ($row && password_verify($pass, $row['password_hash'])) {
$_SESSION['user_id'] = $row['id'];
$_SESSION['user_email'] = $email;
header('Location: ../game/dashboard.php');
exit;
} else {
$error = 'Credenziali non valide.';
}
}
$page_title = 'Login';
include __DIR__ . '/../header.php';
?>
<div class="row justify-content-center">
<div class="col-md-6">
<h2>Login</h2>
<?php if($error): ?>
<div class="alert alert-danger"><?= htmlspecialchars($error) ?></div>
<?php endif; ?>
<form method="post">
<div class="mb-3">
<label>Email</label>
<input type="email" name="email" class="form-control" required>
</div>
<div class="mb-3">
<label>Password</label>
<input type="password" name="password" class="form-control" required>
</div>
<button class="btn btn-primary">Accedi</button>
<a href="<?= BASE_URL ?>/auth/register.php" class="btn btn-link">Registrati</a>
</form>
</div>
</div>
<?php include __DIR__ . '/../footer.php'; ?>

0
auth/logout.php Executable file
View File

76
auth/register.php Executable file
View File

@@ -0,0 +1,76 @@
<?php
require_once __DIR__ . '/../init.php';
require_once __DIR__ . '/../send_email.php';
if (is_logged_in()) header('Location: ../game/dashboard.php');
$errors = [];
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$email = filter_var($_POST['email'] ?? '', FILTER_VALIDATE_EMAIL);
$pass = $_POST['password'] ?? '';
$pass2 = $_POST['password_confirm'] ?? '';
if (!$email) $errors[] = 'Inserisci un indirizzo email valido.';
if (strlen($pass) < 8) $errors[] = 'La password deve essere almeno 8 caratteri.';
if ($pass !== $pass2) $errors[] = 'Le password non coincidono.';
if (empty($errors)) {
$pdo = getPDO();
$stmt = $pdo->prepare("SELECT id FROM users WHERE email = ?");
$stmt->execute([$email]);
if ($stmt->fetch()) {
$errors[] = 'Email già registrata.';
} else {
$hash = password_hash($pass, PASSWORD_DEFAULT);
$stmt = $pdo->prepare("INSERT INTO users (email, password_hash) VALUES (?, ?)");
$stmt->execute([$email, $hash]);
// Invia mail di benvenuto
$subject = "Benvenuto su Diplomacy Web App";
$body = "<p>Ciao ".htmlspecialchars($email).",</p>
<p>Grazie per esserti registrato su Diplomacy Web App. Ora puoi iniziare a giocare con i tuoi amici!</p>
<p>— Il team di Diplomacy</p>";
sendEmail($email, $subject, $body);
// Login automatico
$_SESSION['user_id'] = $pdo->lastInsertId();
$_SESSION['user_email'] = $email;
header('Location: ../game/dashboard.php');
exit;
}
}
}
$page_title = 'Registrati';
include __DIR__ . '/../header.php';
?>
<div class="row justify-content-center">
<div class="col-md-6">
<h2>Registrazione</h2>
<?php if(!empty($errors)): ?>
<div class="alert alert-danger">
<ul><?php foreach($errors as $e) echo "<li>".htmlspecialchars($e)."</li>"; ?></ul>
</div>
<?php endif; ?>
<form method="post" novalidate>
<div class="mb-3">
<label>Email</label>
<input type="email" name="email" class="form-control" required value="<?= htmlspecialchars($_POST['email'] ?? '') ?>">
</div>
<div class="mb-3">
<label>Password</label>
<input type="password" name="password" class="form-control" required>
</div>
<div class="mb-3">
<label>Conferma Password</label>
<input type="password" name="password_confirm" class="form-control" required>
</div>
<button class="btn btn-primary">Registrati</button>
</form>
</div>
</div>
<?php include __DIR__ . '/../footer.php'; ?>