> For the complete documentation index, see [llms.txt](https://support.morozovpimnev.ru/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://support.morozovpimnev.ru/poleznosti/udobnaya-avtorizaciya-v-ca.md).

# Удобная авторизация в ЦА

Разместите код хука ([подробнее](https://www.hostcms.ru/documentation/modules/core/events/)) в файле **bootstrap.php**

{% code lineNumbers="true" fullWidth="true" %}

```php
class My_Auth
{
    static public function onBeforeSendHeaders($response)
    {
        if(in_array($response->getStatus(), array(401, 403)) && Core_Array::getSession('HOSTCMS_HTTP_AUTH_FLAG') === TRUE && strpos(Core_Array::get($_SERVER, 'REQUEST_URI'), '/admin/') !== FALSE)
        {
            $_SESSION['HOSTCMS_HTTP_AUTH_FLAG'] = FALSE;
            unset($_SESSION['HOSTCMS_HTTP_AUTH_FLAG']);

            $_SESSION['HOSTCMS_LOCATION_ADMIN'] = Core_Array::get($_SERVER, 'REQUEST_URI');

            header('Location: /admin/');
            exit();
        }
    }

    static public function onAfterLogin($null, $args)
    {
        if(!is_null(Core_Array::getSession('HOSTCMS_LOCATION_ADMIN')))
        {
            $location = $_SESSION['HOSTCMS_LOCATION_ADMIN'];

            unset($_SESSION['HOSTCMS_LOCATION_ADMIN']);

            header('Location: ' . $location);
            exit();
        }
    }
}

Core_Event::attach('Core_Response.onBeforeSendHeaders', array('My_Auth', 'onBeforeSendHeaders'));
Core_Event::attach('Core_Auth.onAfterLogin', array('My_Auth', 'onAfterLogin'));
```

{% endcode %}
