2026-03-20 15:54:29 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Domain\Utilisateurs;
|
|
|
|
|
|
|
|
|
|
use App\Domain\Controller;
|
|
|
|
|
use App\Helpers\Authentification;
|
|
|
|
|
use App\Http\JSONResponse;
|
|
|
|
|
use App\Http\Request;
|
2026-04-02 16:33:32 +02:00
|
|
|
use App\Infrastructure\View;
|
2026-03-20 15:54:29 +01:00
|
|
|
|
|
|
|
|
class AuthentificationController extends Controller {
|
|
|
|
|
|
|
|
|
|
public static function defineRoutes(): array {
|
|
|
|
|
|
|
|
|
|
return [
|
|
|
|
|
|
|
|
|
|
// Public routes.
|
|
|
|
|
self::Route( routeUrl: '/login', routeName: 'login', routeAction: 'loginForm', pageHeadTitle: 'Connexion' ),
|
2026-04-02 16:33:32 +02:00
|
|
|
self::Route( routeUrl: '/logout', routeName: 'logout', routeAction: 'logoutPage', pageHeadTitle: 'Déconnexion' ),
|
2026-03-20 15:54:29 +01:00
|
|
|
|
|
|
|
|
// API Routes.
|
|
|
|
|
self::Route( routeUrl: '/api/auth', routeName: 'api->auth', routeAction: 'auth', routeMethods: ['POST'] ),
|
2026-04-02 16:33:32 +02:00
|
|
|
// self::Route( routeUrl: '/api/auth/logout', routeName: 'api->auth->logout', routeAction: 'logout', routeMethods: ['POST'] ),
|
2026-03-20 15:54:29 +01:00
|
|
|
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-02 16:33:32 +02:00
|
|
|
public function loginForm(): View {
|
2026-03-20 15:54:29 +01:00
|
|
|
return new View( 'login' );
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-02 16:33:32 +02:00
|
|
|
public function logoutPage(){
|
|
|
|
|
if( !Authentification::isLoggedIn() ) {
|
|
|
|
|
Request::redirectTo( 'home' );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Authentification::destroySession();
|
|
|
|
|
Request::redirectTo( 'home' );
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-20 15:54:29 +01:00
|
|
|
public function auth(): JSONResponse {
|
|
|
|
|
|
|
|
|
|
Request::setCORS();
|
|
|
|
|
|
2026-04-07 12:10:39 +02:00
|
|
|
$username = Request::post( 'username' );
|
|
|
|
|
$password = Request::post( 'password' );
|
2026-03-20 15:54:29 +01:00
|
|
|
|
2026-04-07 12:10:39 +02:00
|
|
|
$userId = new UtilisateurRepository()->login( $username, $password );
|
|
|
|
|
if( !$userId ) {
|
|
|
|
|
return JSONResponse::sendError();
|
|
|
|
|
}
|
2026-03-20 15:54:29 +01:00
|
|
|
|
|
|
|
|
Authentification::loginUser( $userId );
|
2026-04-02 16:33:32 +02:00
|
|
|
return JSONResponse::sendSuccess( [ 'user_id' => $userId ] );
|
2026-03-20 15:54:29 +01:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function logout(): JSONResponse {
|
|
|
|
|
|
|
|
|
|
if( !Authentification::isLoggedIn() ) {
|
2026-04-02 16:33:32 +02:00
|
|
|
return JSONResponse::sendError( [ 'message' => 'Already disconnected' ] );
|
2026-03-20 15:54:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Authentification::destroySession();
|
|
|
|
|
return JSONResponse::sendSuccess( [ 'message' => 'Logged out' ] );
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|