<?php

namespace App\Filters;

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

class AuthFilter implements FilterInterface
{
    public function before(RequestInterface $request, $arguments = null)
    {
        \helper(['basic', 'cookie']);
        if( !is_logged() ){
            if (method_exists($request, 'isAJAX') && $request->isAJAX()) {
                return Services::response()
                    ->setStatusCode(401)
                    ->setBody('Unauthorized');
            }

            return redirect()->to(url('auth/otp'));
        }
    }

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