Add JWT to enhance security
This commit is contained in:
@@ -6,10 +6,12 @@ require_once __DIR__ . '/../models/User.php';
|
||||
class UserController {
|
||||
private $db;
|
||||
private $userModel;
|
||||
private $jwt_secret;
|
||||
|
||||
public function __construct($db) {
|
||||
public function __construct($db, $jwt_secret = null) {
|
||||
$this->db = $db;
|
||||
$this->userModel = new User($this->db);
|
||||
$this->jwt_secret = $jwt_secret;
|
||||
}
|
||||
|
||||
// --- INTERCEPTE L'INSCRIPTION ---
|
||||
@@ -44,13 +46,22 @@ class UserController {
|
||||
$result = $this->userModel->login($data['email'], $data['password']);
|
||||
|
||||
if ($result['success']) {
|
||||
http_response_code(200); // 200 OK
|
||||
// TODO: Plus tard, on générera un token (JWT) ici pour sécuriser les sessions de l'API
|
||||
} else {
|
||||
http_response_code(401); // 401 Unauthorized
|
||||
}
|
||||
require_once __DIR__ . '/../utils/JWT.php';
|
||||
|
||||
// On prépare les infos à enfermer dans le token
|
||||
$token_payload = ["user_id" => $result['user']['id']];
|
||||
$token = JWT::generate($token_payload, $this->jwt_secret);
|
||||
|
||||
echo json_encode($result);
|
||||
http_response_code(200);
|
||||
echo json_encode([
|
||||
"success" => true,
|
||||
"token" => $token, // Le client stockera ce token
|
||||
"user" => $result['user']
|
||||
]);
|
||||
} else {
|
||||
http_response_code(401);
|
||||
echo json_encode($result);
|
||||
}
|
||||
}
|
||||
|
||||
// --- INTERCEPTE LA LIAISON DE COMPTES ---
|
||||
|
||||
Reference in New Issue
Block a user