2

Even when user is not logged in and tries to open homepage, after being redirected to login page, authError is displayed.Is there an elegant way to prevent this, without modifying Auth component? This is how I load Auth component(I am using TinyAuth as authorization adapter):

$this->loadComponent('Auth', [
                'loginAction' => [
                    'controller' => 'Users',
                    'action' => 'login'
                ],
                'loginRedirect' => [
                   'controller' => 'Home',
                   'action' => 'index'
                ],
                'authError' => 'You dont have permissions for that action',
                'authenticate' => [
                    'Form' => [
                        'fields' => [
                            'username' => 'email',
                            'password' => 'password'
                        ],
                        'scope' => ['Users.active' => true],
                        'contain' => ['Roles']
                    ]
                ],
                'authorize' => [
                    'TinyAuth.Tiny' => [
                        'roleColumn' => 'role_id',
                        'rolesTable' => 'Roles',
                        'multiRole' => true,
                        'pivotTable' => 'roles_users',
                        'superAdminRole' => null,
                        'authorizeByPrefix' => false,
                        'prefixes' => [],
                        'allowUser' => false,
                        'adminPrefix' => null,
                        'autoClearCache' => true
                    ] 
                ]
            ]
        );

1 Answer 1

7

According to CakePHP's documentation you can prevent the error message from being shown by setting authError to false.

Sometimes, you want to display the authorization error only after the user has already logged-in. You can suppress this message by setting its value to boolean false.

This should disable the error message:

if (!$this->Auth->user()) {
    $this->Auth->config('authError', false);
}
3
  • where should I add this code . in Appcontroller beforefileter or somewhere else. Jun 7, 2018 at 6:22
  • 3
    The documentation mentions that you should use this in the beforeFilter(). :).
    – Sevvlor
    Jun 7, 2018 at 9:02
  • 2
    use setConfig since cakePHP 4
    – Ruth
    Nov 2, 2020 at 8:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.