Actualiser api/auth.php
This commit is contained in:
@@ -2,7 +2,6 @@
|
|||||||
ini_set('display_errors', 1);
|
ini_set('display_errors', 1);
|
||||||
ini_set('display_startup_errors', 1);
|
ini_set('display_startup_errors', 1);
|
||||||
error_reporting(E_ALL);
|
error_reporting(E_ALL);
|
||||||
|
|
||||||
require_once 'config.php';
|
require_once 'config.php';
|
||||||
|
|
||||||
$method = $_SERVER['REQUEST_METHOD'];
|
$method = $_SERVER['REQUEST_METHOD'];
|
||||||
@@ -16,56 +15,44 @@ switch ($method) {
|
|||||||
login();
|
login();
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'GET':
|
case 'GET':
|
||||||
if ($action === 'me') {
|
if ($action === 'me') {
|
||||||
getCurrentUserInfo();
|
getCurrentUserInfo();
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
jsonResponse(['error' => 'Méthode non autorisée'], 405);
|
jsonResponse(['error' => 'Méthode non autorisée'], 405);
|
||||||
}
|
}
|
||||||
|
|
||||||
function register() {
|
function register() {
|
||||||
$data = getJsonInput();
|
$data = getJsonInput();
|
||||||
|
|
||||||
if (!isset($data['username']) || !isset($data['email']) || !isset($data['password'])) {
|
if (!isset($data['username']) || !isset($data['email']) || !isset($data['password'])) {
|
||||||
jsonResponse(['error' => 'Tous les champs sont requis'], 400);
|
jsonResponse(['error' => 'Tous les champs sont requis'], 400);
|
||||||
}
|
}
|
||||||
|
|
||||||
$username = trim($data['username']);
|
$username = trim($data['username']);
|
||||||
$email = trim($data['email']);
|
$email = trim($data['email']);
|
||||||
$password = $data['password'];
|
$password = $data['password'];
|
||||||
|
|
||||||
// Validation
|
|
||||||
if (strlen($username) < 3) {
|
if (strlen($username) < 3) {
|
||||||
jsonResponse(['error' => 'Le nom d\'utilisateur doit contenir au moins 3 caractères'], 400);
|
jsonResponse(['error' => 'Le nom d\'utilisateur doit contenir au moins 3 caractères'], 400);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
|
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
|
||||||
jsonResponse(['error' => 'Email invalide'], 400);
|
jsonResponse(['error' => 'Email invalide'], 400);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (strlen($password) < 6) {
|
if (strlen($password) < 6) {
|
||||||
jsonResponse(['error' => 'Le mot de passe doit contenir au moins 6 caractères'], 400);
|
jsonResponse(['error' => 'Le mot de passe doit contenir au moins 6 caractères'], 400);
|
||||||
}
|
}
|
||||||
|
|
||||||
$db = getDB();
|
$db = getDB();
|
||||||
|
|
||||||
// Vérifier si l'utilisateur existe déjà
|
|
||||||
$stmt = $db->prepare("SELECT id FROM users WHERE username = ? OR email = ?");
|
$stmt = $db->prepare("SELECT id FROM users WHERE username = ? OR email = ?");
|
||||||
$stmt->execute([$username, $email]);
|
$stmt->execute([$username, $email]);
|
||||||
|
|
||||||
if ($stmt->fetch()) {
|
if ($stmt->fetch()) {
|
||||||
jsonResponse(['error' => 'Ce nom d\'utilisateur ou cet email existe déjà'], 409);
|
jsonResponse(['error' => 'Ce nom d\'utilisateur ou cet email existe déjà'], 409);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Créer l'utilisateur
|
|
||||||
$hashedPassword = password_hash($password, PASSWORD_DEFAULT);
|
$hashedPassword = password_hash($password, PASSWORD_DEFAULT);
|
||||||
$stmt = $db->prepare("INSERT INTO users (username, email, password, role) VALUES (?, ?, ?, 'user')");
|
$stmt = $db->prepare("INSERT INTO users (username, email, password, role) VALUES (?, ?, ?, 'user')");
|
||||||
$stmt->execute([$username, $email, $hashedPassword]);
|
$stmt->execute([$username, $email, $hashedPassword]);
|
||||||
|
|
||||||
$userId = $db->lastInsertId();
|
$userId = $db->lastInsertId();
|
||||||
$token = generateToken($userId);
|
$token = generateToken($userId);
|
||||||
|
|
||||||
@@ -85,11 +72,9 @@ function register() {
|
|||||||
|
|
||||||
function login() {
|
function login() {
|
||||||
$data = getJsonInput();
|
$data = getJsonInput();
|
||||||
|
|
||||||
if (!isset($data['username']) || !isset($data['password'])) {
|
if (!isset($data['username']) || !isset($data['password'])) {
|
||||||
jsonResponse(['error' => 'Nom d\'utilisateur et mot de passe requis'], 400);
|
jsonResponse(['error' => 'Nom d\'utilisateur et mot de passe requis'], 400);
|
||||||
}
|
}
|
||||||
|
|
||||||
$username = trim($data['username']);
|
$username = trim($data['username']);
|
||||||
$password = $data['password'];
|
$password = $data['password'];
|
||||||
|
|
||||||
@@ -101,7 +86,6 @@ function login() {
|
|||||||
if (!$user || !password_verify($password, $user['password'])) {
|
if (!$user || !password_verify($password, $user['password'])) {
|
||||||
jsonResponse(['error' => 'Identifiants incorrects'], 401);
|
jsonResponse(['error' => 'Identifiants incorrects'], 401);
|
||||||
}
|
}
|
||||||
|
|
||||||
$token = generateToken($user['id']);
|
$token = generateToken($user['id']);
|
||||||
|
|
||||||
jsonResponse([
|
jsonResponse([
|
||||||
@@ -120,7 +104,6 @@ function login() {
|
|||||||
|
|
||||||
function getCurrentUserInfo() {
|
function getCurrentUserInfo() {
|
||||||
$user = requireAuth();
|
$user = requireAuth();
|
||||||
|
|
||||||
jsonResponse([
|
jsonResponse([
|
||||||
'success' => true,
|
'success' => true,
|
||||||
'user' => $user
|
'user' => $user
|
||||||
|
|||||||
Reference in New Issue
Block a user