<?php namespace App\Filters;

use CodeIgniter\Filters\FilterInterface;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;

class AuthFilter implements FilterInterface
{
    public function before(RequestInterface $request, $arguments = null)
    {
        // Jika user belum login, lempar kembali ke halaman login
        if (!session()->get('logged_in')) {
            return redirect()->to('/login')->with('error', 'Silakan login terlebih dahulu.');
        }

        // Jika rute dikunci untuk role tertentu, cocokkan dengan sesi
        if ($arguments && !in_array(session()->get('role'), $arguments)) {
            return redirect()->to('/login')->with('error', 'Akses ditolak: Area Terbatas.');
        }
    }

    public function after(RequestInterface $request, ResponseInterface $response, $arguments = null) {}
}