# How to check Auth-\>user() is exit or not in cakephp 4.x

**URL:** <https://discourse.cakephp.org/t/how-to-check-auth-user-is-exit-or-not-in-cakephp-4-x/10380>\
**Category:** Need Help\
**Tags:** help\
**Created:** [May 8, 2022, 9:16am UTC](https://discourse.cakephp.org/t/how-to-check-auth-user-is-exit-or-not-in-cakephp-4-x/10380 "2022-05-08T09:16:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![yogeshsaroya](https://avatars.discourse-cdn.com/v4/letter/y/f05b48/32.png) [@yogeshsaroya](https://discourse.cakephp.org/u/yogeshsaroya)\
**Post date:** [May 8, 2022, 9:16am UTC](https://discourse.cakephp.org/t/how-to-check-auth-user-is-exit-or-not-in-cakephp-4-x/10380/1 "2022-05-08T09:16:48Z")

</div>

Hi

In AppController I am using this code

```auto
    function beforeRender(\Cake\Event\EventInterface $event) {

        if($this->request->isAjax())
        {
           /* coe here */

        }
        

        if( $this->Auth->user()){ /* Line no 100 */
                 /*code here */
        }
    }

```

when i try to use wrong url or any other error (missing controller name or action etc )  
templates \> error \> error4004.php and error4004.php is calling but that time i am getting this error even user is logged in

```auto
Notice (1024): Undefined property: ErrorController::$Auth in /Applications/MAMP/htdocs/cake/my_app/src/Controller/AppController.php on line 100 [CORE/src/Controller/Controller.php, line 321]

```

What i am missing here ?

---

<div class="post-metadata">

**Author:** ![MarekDM](https://avatars.discourse-cdn.com/v4/letter/m/82dd89/32.png) [@MarekDM](https://discourse.cakephp.org/u/MarekDM)\
**Post date:** [May 8, 2022, 12:52pm UTC](https://discourse.cakephp.org/t/how-to-check-auth-user-is-exit-or-not-in-cakephp-4-x/10380/2 "2022-05-08T12:52:27Z")

</div>

How is your Auth component loaded? Maybe the code which loads it is not run when you take care of error?

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [May 8, 2022, 3:18pm UTC](https://discourse.cakephp.org/t/how-to-check-auth-user-is-exit-or-not-in-cakephp-4-x/10380/3 "2022-05-08T15:18:35Z")

</div>

The ErrorController does not load components, in case it’s the component itself that’s causing the error.

---

<div class="post-metadata">

**Author:** ![dreamingmind](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/dreamingmind/32/1857_2.png) [@dreamingmind](https://discourse.cakephp.org/u/dreamingmind)\
**Post date:** [May 11, 2022, 5:50pm UTC](https://discourse.cakephp.org/t/how-to-check-auth-user-is-exit-or-not-in-cakephp-4-x/10380/4 "2022-05-11T17:50:06Z")

</div>

> [@Zuluru](#):
>
> ErrorController does not load components

Since this is the case, you’re probably going to need to dig the component out of the `$event`. The class that was working when the error was thrown should be in it.  
[https://api.cakephp.org/4.3/interface-Cake.Event.EventInterface.html](https://api.cakephp.org/4.3/interface-Cake.Event.EventInterface.html)
