<?php

namespace App\Http\Middleware;

use App\Core\App;
use App\Core\Request;
use App\Core\Response;

/**
 * ITSWEBER CMS – Authentication Middleware
 *
 * Redirects to login page if user is not authenticated.
 */
class AuthMiddleware
{
    public function handle(Request $request, App $app): bool
    {
        if (!$app->hasService('auth')) {
            Response::redirect(admin_url('login'));
            return false;
        }

        $auth = $app->service('auth');
        if (!$auth->check()) {
            Response::redirect(admin_url('login'));
            return false;
        }

        return true;
    }
}
