Front + gestion des utilisateurs
This commit is contained in:
@@ -0,0 +1,328 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
|
||||
/*
|
||||
* =========================================================
|
||||
* SESSION
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
session_set_cookie_params([
|
||||
'lifetime' => 0,
|
||||
'path' => '/',
|
||||
'secure' => !empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off',
|
||||
'httponly' => true,
|
||||
'samesite' => 'Strict'
|
||||
]);
|
||||
|
||||
session_start();
|
||||
|
||||
|
||||
/*
|
||||
* =========================================================
|
||||
* CONFIGURATION
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
require_once __DIR__ . '/database.php';
|
||||
|
||||
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
|
||||
|
||||
/*
|
||||
* =========================================================
|
||||
* LECTURE JSON
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
$data = json_decode(
|
||||
file_get_contents('php://input'),
|
||||
true
|
||||
);
|
||||
|
||||
$action = $data['action'] ?? '';
|
||||
|
||||
|
||||
/*
|
||||
* =========================================================
|
||||
* REPONSE JSON
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
function jsonResponse(
|
||||
string $status,
|
||||
string $message = '',
|
||||
array $extra = []
|
||||
): never {
|
||||
|
||||
echo json_encode(
|
||||
array_merge(
|
||||
[
|
||||
'status' => $status,
|
||||
'message' => $message
|
||||
],
|
||||
$extra
|
||||
),
|
||||
JSON_UNESCAPED_UNICODE
|
||||
);
|
||||
|
||||
exit;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* =========================================================
|
||||
* VERIFICATION SESSION
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
if ($action === 'check') {
|
||||
|
||||
if (
|
||||
isset($_SESSION['user_id']) &&
|
||||
isset($_SESSION['username'])
|
||||
) {
|
||||
|
||||
jsonResponse(
|
||||
'success',
|
||||
'',
|
||||
[
|
||||
'authenticated' => true,
|
||||
'user_id' => $_SESSION['user_id'],
|
||||
'nom' => $_SESSION['username']
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
jsonResponse(
|
||||
'error',
|
||||
'Session inactive.',
|
||||
[
|
||||
'authenticated' => false
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* =========================================================
|
||||
* DECONNEXION
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
if ($action === 'logout') {
|
||||
|
||||
/*
|
||||
* Détruire complètement la session
|
||||
*/
|
||||
|
||||
$_SESSION = [];
|
||||
|
||||
|
||||
/*
|
||||
* Supprimer le cookie de session
|
||||
*/
|
||||
|
||||
if (
|
||||
ini_get('session.use_cookies')
|
||||
) {
|
||||
|
||||
$params = session_get_cookie_params();
|
||||
|
||||
setcookie(
|
||||
session_name(),
|
||||
'',
|
||||
time() - 42000,
|
||||
$params['path'],
|
||||
$params['domain'] ?? '',
|
||||
$params['secure'],
|
||||
$params['httponly']
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
session_destroy();
|
||||
|
||||
|
||||
jsonResponse(
|
||||
'success',
|
||||
'Déconnexion effectuée.'
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* =========================================================
|
||||
* CONNEXION
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
if ($action === 'login') {
|
||||
|
||||
$nom =
|
||||
trim(
|
||||
(string)($data['nom'] ?? '')
|
||||
);
|
||||
|
||||
$pass =
|
||||
(string)($data['pass'] ?? '');
|
||||
|
||||
|
||||
if (
|
||||
$nom === '' ||
|
||||
$pass === ''
|
||||
) {
|
||||
|
||||
jsonResponse(
|
||||
'error',
|
||||
'Identifiant et mot de passe obligatoires.'
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Recherche utilisateur
|
||||
*/
|
||||
|
||||
$sql = "
|
||||
SELECT
|
||||
id,
|
||||
nom,
|
||||
pass,
|
||||
actif
|
||||
FROM login
|
||||
WHERE nom = :nom
|
||||
LIMIT 1
|
||||
";
|
||||
|
||||
|
||||
$stmt = $pdo->prepare($sql);
|
||||
|
||||
$stmt->execute([
|
||||
':nom' => $nom
|
||||
]);
|
||||
|
||||
|
||||
$user =
|
||||
$stmt->fetch(PDO::FETCH_ASSOC);
|
||||
|
||||
|
||||
/*
|
||||
* Utilisateur inexistant
|
||||
*/
|
||||
|
||||
if (!$user) {
|
||||
|
||||
/*
|
||||
* Même traitement que mauvais mot de passe
|
||||
* afin de ne pas révéler si le compte existe.
|
||||
*/
|
||||
|
||||
jsonResponse(
|
||||
'error',
|
||||
'Identifiant ou mot de passe incorrect.'
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Compte désactivé
|
||||
*/
|
||||
|
||||
if ((int)$user['actif'] !== 1) {
|
||||
|
||||
jsonResponse(
|
||||
'error',
|
||||
'Ce compte est désactivé.'
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Vérification du mot de passe
|
||||
*/
|
||||
|
||||
if (
|
||||
!password_verify(
|
||||
$pass,
|
||||
$user['pass']
|
||||
)
|
||||
) {
|
||||
|
||||
jsonResponse(
|
||||
'error',
|
||||
'Identifiant ou mot de passe incorrect.'
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Nouveau numéro de session
|
||||
* après authentification
|
||||
*/
|
||||
|
||||
session_regenerate_id(true);
|
||||
|
||||
|
||||
/*
|
||||
* Variables de session
|
||||
*/
|
||||
|
||||
$_SESSION['user_id'] =
|
||||
(int)$user['id'];
|
||||
|
||||
$_SESSION['username'] =
|
||||
$user['nom'];
|
||||
|
||||
$_SESSION['authenticated'] =
|
||||
true;
|
||||
|
||||
|
||||
/*
|
||||
* Dernière connexion
|
||||
*/
|
||||
|
||||
$sql = "
|
||||
UPDATE login
|
||||
SET dernier_login = NOW()
|
||||
WHERE id = :id
|
||||
";
|
||||
|
||||
|
||||
$stmt = $pdo->prepare($sql);
|
||||
|
||||
$stmt->execute([
|
||||
':id' => $user['id']
|
||||
]);
|
||||
|
||||
|
||||
/*
|
||||
* Réponse
|
||||
*/
|
||||
|
||||
jsonResponse(
|
||||
'success',
|
||||
'Connexion réussie.',
|
||||
[
|
||||
'authenticated' => true,
|
||||
'user_id' => (int)$user['id'],
|
||||
'nom' => $user['nom']
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* =========================================================
|
||||
* ACTION INCONNUE
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
jsonResponse(
|
||||
'error',
|
||||
'Action inconnue.'
|
||||
);
|
||||
Reference in New Issue
Block a user