widimulcing

AuthController.php

Jul 29th, 2025 (edited)
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.24 KB | None | 0 0
  1. <?php
  2. // 4o
  3. /**
  4.  * @file        AuthController.php
  5.  * @description Handles HTTP requests for authentication. It processes user input,
  6.  * manages CSRF tokens, and interacts with the AuthService to perform
  7.  * the actual login logic. It is responsible for redirects and rendering views.
  8.  *
  9.  * @version     1.0.0
  10.  * @since       2025-07-25
  11.  * @author      Barrac0de
  12.  */
  13. namespace App\Controllers;
  14.  
  15. use App\Services\AuthService;
  16.  
  17. class AuthController
  18. {
  19.     public static function handle(): void
  20.     {
  21.         session_start();
  22.  
  23.         if ($_SERVER['REQUEST_METHOD'] === 'GET') {
  24.             if (isset($_SESSION['user_id'])) {
  25.                 header('Location: ' . BASE_PATH . 'index');
  26.                 exit;
  27.             }
  28.  
  29.             $error       = $_SESSION['login_error']  ?? '';
  30.             $oldUsername = $_SESSION['old_username'] ?? '';
  31.             unset($_SESSION['login_error'], $_SESSION['old_username']);
  32.  
  33.             if (empty($_SESSION['csrf_token'])) {
  34.                 $_SESSION['csrf_token'] = bin2hex(random_bytes(32));
  35.             }
  36.  
  37.             require __DIR__ . '/../../login.php';
  38.             exit;
  39.         }
  40.  
  41.         // POST request
  42.         $auth = new AuthService();
  43.         $auth->processLogin();
  44.     }
  45. }
  46.  
Advertisement
Add Comment
Please, Sign In to add comment