diff --git a/auth.php b/auth.php new file mode 100644 index 0000000..72517ac --- /dev/null +++ b/auth.php @@ -0,0 +1,169 @@ +prepare("SELECT id FROM users WHERE username = ? OR email = ?"); + $stmt->execute([$username, $email]); + if ($stmt->fetch()) { + $error = 'Ce nom d\'utilisateur ou cet email est déjà utilisé.'; + } else { + $hashed_password = password_hash($password, PASSWORD_DEFAULT); + $stmt = $db->prepare("INSERT INTO users (username, email, password) VALUES (?, ?, ?)"); + try { + $stmt->execute([$username, $email, $hashed_password]); + $success = 'Compte créé avec succès ! Vous pouvez maintenant vous connecter.'; + } catch (Exception $e) { + $error = 'Erreur lors de la création du compte.'; + } + } + } + } elseif ($action === 'login') { + $username = trim($_POST['username'] ?? ''); + $password = $_POST['password'] ?? ''; + + if (empty($username) || empty($password)) { + $error = 'Tous les champs sont obligatoires.'; + } else { + $db = db(); + $stmt = $db->prepare("SELECT * FROM users WHERE username = ?"); + $stmt->execute([$username]); + $user = $stmt->fetch(); + + if ($user && password_verify($password, $user['password'])) { + $_SESSION['user_id'] = $user['id']; + $_SESSION['username'] = $user['username']; + $_SESSION['role'] = $user['role']; + + $db->prepare("UPDATE users SET last_login = CURRENT_TIMESTAMP WHERE id = ?")->execute([$user['id']]); + + header('Location: index.php'); + exit; + } else { + $error = 'Identifiants incorrects.'; + } + } + } +} + +if (isset($_GET['logout'])) { + session_destroy(); + header('Location: index.php'); + exit; +} + +$page = $_GET['page'] ?? 'login'; +?> + + + + + + <?php echo $page === 'register' ? 'Création de compte' : 'Connexion'; ?> + + + + +
+ +

Créer un compte

+
+
+
+ +
+ + +
+
+ + +
+
+ + +
+
+ + +
+ +
+ + +

Connexion

+
+
+
+ +
+ + +
+
+ + +
+ +
+ + + Retour à la galaxie +
+ + diff --git a/index.php b/index.php index 35a9c41..ae011fc 100644 --- a/index.php +++ b/index.php @@ -1,5 +1,6 @@
+
+ + Bienvenue, @ + Profil + Déconnexion + + Connexion + S'inscrire + +
$res): ?>
@@ -389,4 +416,4 @@ function getStatusColor($status, $type = 'planet') { Console MJ - \ No newline at end of file + diff --git a/profile.php b/profile.php new file mode 100644 index 0000000..86ffd69 --- /dev/null +++ b/profile.php @@ -0,0 +1,151 @@ +prepare("SELECT * FROM users WHERE id = ?"); +$stmt->execute([$user_id]); +$user = $stmt->fetch(); + +if ($_SERVER['REQUEST_METHOD'] === 'POST') { + $action = $_POST['action'] ?? ''; + + if ($action === 'update_profile') { + $email = trim($_POST['email'] ?? ''); + $current_password = $_POST['current_password'] ?? ''; + $new_password = $_POST['new_password'] ?? ''; + $confirm_password = $_POST['confirm_password'] ?? ''; + + if (empty($email)) { + $error = 'L\'email ne peut pas être vide.'; + } elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) { + $error = 'Email invalide.'; + } elseif (!password_verify($current_password, $user['password'])) { + $error = 'Mot de passe actuel incorrect.'; + } else { + // Update password if provided + if (!empty($new_password)) { + if ($new_password !== $confirm_password) { + $error = 'Les nouveaux mots de passe ne correspondent pas.'; + } else { + $hashed_password = password_hash($new_password, PASSWORD_DEFAULT); + $stmt = $db->prepare("UPDATE users SET email = ?, password = ? WHERE id = ?"); + $stmt->execute([$email, $hashed_password, $user_id]); + $success = 'Profil et mot de passe mis à jour avec succès.'; + } + } else { + $stmt = $db->prepare("UPDATE users SET email = ? WHERE id = ?"); + $stmt->execute([$email, $user_id]); + $success = 'Profil mis à jour avec succès.'; + } + + // Refresh user data + $stmt = $db->prepare("SELECT * FROM users WHERE id = ?"); + $stmt->execute([$user_id]); + $user = $stmt->fetch(); + } + } +} +?> + + + + + + Mon Profil - Nexus + + + + +
+

Gestion du Compte

+ +
+ @ +
+ +
+
+ +
+ + +
+ + +
+ +
+

Changer le mot de passe (Optionnel)

+
+ + +
+
+ + +
+
+ +
+ + +
+ + +
+ + +
+ +