Refactor Auth filter: streamline token validation and session handling, enhance response for unauthenticated requests with JSON support, and improve code readability. Update default layout to ensure lucide icons are created only if the function is available.
This commit is contained in:
@ -13,19 +13,11 @@ class Auth implements FilterInterface
|
||||
{
|
||||
$token = $_COOKIE['auth_token'] ?? null;
|
||||
|
||||
// Aucun token → logout CI4 et redirection
|
||||
if (!$token) {
|
||||
|
||||
if (! $token) {
|
||||
session()->destroy();
|
||||
setcookie('derniere_page', $_SERVER['REQUEST_URI'] ?? '/', time() + 3600, '/');
|
||||
|
||||
setcookie(
|
||||
'derniere_page',
|
||||
$_SERVER['REQUEST_URI'],
|
||||
time() + 3600,
|
||||
'/'
|
||||
);
|
||||
|
||||
return redirect()->to($this->loginUrl());
|
||||
return $this->deny($request);
|
||||
}
|
||||
|
||||
$db = Database::connect();
|
||||
@ -36,58 +28,61 @@ class Auth implements FilterInterface
|
||||
->get()
|
||||
->getRow();
|
||||
|
||||
// Token invalide → logout CI4
|
||||
if (!$row) {
|
||||
|
||||
if (! $row) {
|
||||
session()->destroy();
|
||||
setcookie('derniere_page', $_SERVER['REQUEST_URI'] ?? '/', time() + 3600, '/');
|
||||
|
||||
setcookie(
|
||||
'derniere_page',
|
||||
$_SERVER['REQUEST_URI'],
|
||||
time() + 3600,
|
||||
'/'
|
||||
);
|
||||
|
||||
return redirect()->to($this->loginUrl());
|
||||
return $this->deny($request);
|
||||
}
|
||||
|
||||
// Charger utilisateur
|
||||
$user = $db->table('usa_usagers')
|
||||
->where('usa_id', $row->user_id)
|
||||
->get()
|
||||
->getRow();
|
||||
|
||||
if (!$user) {
|
||||
|
||||
if (! $user) {
|
||||
session()->destroy();
|
||||
|
||||
return redirect()->to($this->loginUrl());
|
||||
return $this->deny($request);
|
||||
}
|
||||
|
||||
// Si session CI4 inexistante → la créer
|
||||
if (!session()->get('usa_id')) {
|
||||
|
||||
if (! session()->get('usa_id')) {
|
||||
session()->set([
|
||||
'usa_id' => $user->usa_id,
|
||||
'usa_nom' => $user->usa_nom
|
||||
'usa_nom' => $user->usa_nom,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public function after(RequestInterface $request, ResponseInterface $response, $arguments = null)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Toujours le host courant (évite baseURL figé → prod / autre site).
|
||||
* Fetch/JSON → 401 JSON (évite Unexpected token '<' sur du HTML login).
|
||||
* Navigation normale → redirect login.
|
||||
*/
|
||||
private function deny(RequestInterface $request)
|
||||
{
|
||||
$wantsJson = $request->isAJAX()
|
||||
|| $request->getGet('json') !== null
|
||||
|| str_contains((string) $request->getHeaderLine('Accept'), 'application/json');
|
||||
|
||||
if ($wantsJson) {
|
||||
return service('response')
|
||||
->setStatusCode(401)
|
||||
->setJSON(['ok' => false, 'error' => 'non authentifié']);
|
||||
}
|
||||
|
||||
return redirect()->to($this->loginUrl());
|
||||
}
|
||||
|
||||
private function loginUrl(): string
|
||||
{
|
||||
$https = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off')
|
||||
$https = (! empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off')
|
||||
|| (($_SERVER['HTTP_X_FORWARDED_PROTO'] ?? '') === 'https');
|
||||
$host = $_SERVER['HTTP_HOST'] ?? 'localhost';
|
||||
|
||||
return ($https ? 'https' : 'http') . '://' . $host . '/login';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -64,7 +64,7 @@
|
||||
<script src="<?= base_url('v4/assets/raven/vendor/js/quill.js') ?>"></script>
|
||||
<script src="<?= base_url('v4/assets/raven/js/theme.js') ?>"></script>
|
||||
<script src="https://unpkg.com/lucide@latest"></script>
|
||||
<script>lucide.createIcons();</script>
|
||||
<script>if (window.lucide && typeof lucide.createIcons === 'function') { lucide.createIcons(); }</script>
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user