Undefined property: UsersController::$Auth in /var/www/html/cakeacl/src/Controller/UsersController.php on line 118

//// Controller code    
public function login() {

        // $data = $this->request->getData();
        // pr($data);
        if ($this->request->is('post')) {
            $user = $this->Auth->identify();
            if ($user) {
                $this->Auth->setUser($user);
                return $this->redirect($this->Auth->redirectUrl());
            }
            $this->Flash->error(__('Your username or password was incorrect.'));
        }
    }
    public function logout() {
        $this->Flash->success(__('Good-Bye'));
        $this->redirect($this->Auth->logout());
    }

///  ctp  file 
<?= $this->Form->create() ?>
<fieldset>
	<legend><?= __('Login') ?></legend>
	<?= $this->Form->control('username') ?>
	<?= $this->Form->control('password') ?>
	<?= $this->Form->submit(__('Login')) ?>
</fieldset>
<?= $this->Form->end() ?>
///  UserTable /// 
public function validationDefault(Validator $validator)
    {
        $validator
            ->integer('id')
            ->allowEmptyString('id', 'create');

        $validator
            ->scalar('username')
            ->maxLength('username', 255)
            ->requirePresence('username', 'create')
            ->allowEmptyString('username', false)
            ->add('username', 'unique', ['rule' => 'validateUnique', 'provider' => 'table']);

        $validator
            ->scalar('password')
            ->maxLength('password', 60)
            ->requirePresence('password', 'create')
            ->allowEmptyString('password', false);

        return $validator;
    }

    /**
     * Returns a rules checker object that will be used for validating
     * application integrity.
     *
     * @param \Cake\ORM\RulesChecker $rules The rules object to be modified.
     * @return \Cake\ORM\RulesChecker
     */
    public function buildRules(RulesChecker $rules)
    {
        $rules->add($rules->isUnique(['username']));
        $rules->add($rules->existsIn(['group_id'], 'Groups'));

        return $rules;
    }
    public function beforeSave(\Cake\Event\Event $event, \Cake\ORM\Entity $entity, 
    \ArrayObject $options)
    {
        $hasher = new DefaultPasswordHasher;
        $entity->password = $hasher->hash($entity->password);
        return true;
    }
///////////////////////  App controller 

 public $components = [
        'Acl' => [
            'className' => 'Acl.Acl'
        ]
    ];
 
 
    public function initialize()
    {
        parent::initialize();

        $this->loadComponent('RequestHandler', [
            'enableBeforeRedirect' => false,
        ]);
        $this->loadComponent('Flash');

        $this->loadComponent('Auth', [
            'authorize' => [
                'Acl.Actions' => ['actionPath' => 'controllers/']
            ],
            'loginAction' => [
                'plugin' => false,
                'controller' => 'Users',
                'action' => 'login'
            ],
            'loginRedirect' => [
                'plugin' => false,
                'controller' => 'Posts',
                'action' => 'index'
            ],
            'logoutRedirect' => [
                'plugin' => false,
                'controller' => 'Users',
                'action' => 'login'
            ],
            'unauthorizedRedirect' => [
                'controller' => 'Users',
                'action' => 'login',
                'prefix' => false
            ],
            'authError' => 'You are not authorized to access that location.',
            'flash' => [
                'element' => 'error'
            ]
        ]);

App working but when i login then get this error what’s wrong with my code Please !!! suggest

Any chance your UsersController has its own initialize function, which isn’t calling the parent version?

1 Like

Most likely, or it isn’t extending AppController

public function initialize()
{
parent::initialize();

    $this->Auth->allow();
}

Is this problem with my code i am trying do accesss control list using in my app.

Did you check @dakota’s suggestion? Are you using $this->Auth anywhere other than the login function? Have you confirmed that the Auth component is being correctly loaded in this particular execution path?

yes, i have check,I have use in the login as well as public function initialize().

In your AppController::initialize() method, do you have a $this->loadComponent('Auth')?

Does UsersController extend AppController?

Does UsersController have a initialize() method? If yes, does it have a call to parent::initialize()?

If you answer no to any of those, then that’s your problem :slight_smile:

yes, i have UsersController and it’s initialize method and i called parent::initialize().
Also in my AppController ::initialize () method i have load Auth component. All you question i say Yes.