backend-01

ref: master

app/helpers/SessionMiddleware.php


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
<?php

function sessionMiddleware($currentRoute) {
  $publicRoutes = ['/login', '/register', '/'];

  if (session_status() === PHP_SESSION_NONE) {
    session_start();
  }

  if (isset($_SESSION['user_id']) && in_array($currentRoute, $publicRoutes)) {
    header('Location: /dashboard');
    exit;
  }

  if (!isset($_SESSION['user_id']) && !in_array($currentRoute, $publicRoutes)) {
    header('Location: /login');
    exit;
  }
}