Cake\Controller\Component\AuthComponent::_unauthenticated PHP Method

_unauthenticated() protected method

Handles unauthenticated access attempt. First the unauthenticated() method of the last authenticator in the chain will be called. The authenticator can handle sending response or redirection as appropriate and return true to indicate no further action is necessary. If authenticator returns null this method redirects user to login action. If it's an AJAX request and config ajaxLogin is specified that element is rendered else a 403 HTTP status code is returned.
protected _unauthenticated ( Controller $controller ) : Response | null
$controller Cake\Controller\Controller A reference to the controller object.
return Cake\Network\Response | null Null if current action is login action else response object returned by authenticate object or Controller::redirect().
    protected function _unauthenticated(Controller $controller)
    {
        if (empty($this->_authenticateObjects)) {
            $this->constructAuthenticate();
        }
        $auth = end($this->_authenticateObjects);
        $result = $auth->unauthenticated($this->request, $this->response);
        if ($result !== null) {
            return $result;
        }
        if (!$this->storage()->redirectUrl()) {
            $this->storage()->redirectUrl($this->request->here(false));
        }
        if (!$controller->request->is('ajax')) {
            $this->flash($this->_config['authError']);
            $this->storage()->redirectUrl($controller->request->here(false));
            return $controller->redirect($this->_config['loginAction']);
        }
        if (!empty($this->_config['ajaxLogin'])) {
            $controller->viewBuilder()->templatePath('Element');
            $response = $controller->render($this->_config['ajaxLogin'], $this->RequestHandler->ajaxLayout);
            $response->statusCode(403);
            return $response;
        }
        $this->response->statusCode(403);
        return $this->response;
    }