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

Hi

In AppController I am using this code

    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

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 ?

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

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

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