Help with login

Greetings one and all.

I am having a problem that I can not figure out. I am working on a website that requires login. I first got the site running with the Auth part in AppController commented out, as well at the login function in my UsersController. Once I got the some of the basic stuff working, I removed all the comments in the controllers and tried to get it to work with the Auth Component enabled, but I am getting an error saying there are to many redirects. I got this working before, but I forgot what I did. I am thinking I adjusted the number of redirects allowed in some configuration file, but I am not sure. Any help would be greatly appreciated.

Troy

When I faced that Error of Too Many redirects.

I firstly opened the method I was supposed to redirect too when I login, in other words loginRedirect.

public function beforeFilter(Event $event)
{
parent::beforeFilter($event);
$this->Auth->allow([ ‘YourLoginRedirectActionName’ ]);
}

Now Open that action via URL.

When your controller action will be open, you will see LOTS of Flash messages, Once You clear them out everything will work back to normal.

Note: It worked with my case, I am not sure if this will solve your problem.

Thanks for the response.

From what I understand, you are saying to put the function above into my users controller as that contains my login action. If that is where it should go, I placed it in the users controller, and received this message: Argument 1 passed to App\Controller\UsersController::beforeFilter() must be an instance of App\Controller\Event, instance of Cake\Event\Event given, called in D:\cen\vendor\cakephp\cakephp\src\Event\EventManager.php on line 414.

I am not sure what that means, but I will keep searching. If I was wrong or there are any other suggestions please let me know.

Thanks again, Troy.

Here is more information.

In my app controller I have the following:
public function initialize()
{
parent::initialize();

    $this->loadComponent('RequestHandler');
    $this->loadComponent('Flash');
	
    $this->loadComponent('Auth', [
        'authenticate' => [
            'Form' => [
                'fields' => [
                    'username' => 'username',
                    'password' => 'password'
                ]
            ]
        ],
        'loginAction' =>[
            'controller' => 'Users',
            'action' => 'login'
        ],
        'unauthorizedRedirect' => $this->referer()
    ]);
	
	$this->Auth->allow(['display']);
}

in my user controller, I have:
public function initialize()
{
parent::initialize();
$this->Auth->allow([‘login’]);

    if (null == $this->Auth->user('level'))
    {
        $this->redirect('/users/login');
    } else {
        if ($this->Auth->user('level') < 4)
        {
            $this->redirect('/pages/403');
        }
    }
    $this->set('loggedin', $this->Auth->user('username'));

}

And the error I get is:

There were too many redirections.

Error Code: INET_E_REDIRECT_FAILED

If I have the code in this code commented out, everything works, but as soon as I run this code, I get the error.

use Cake\Event\Event;

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

    $this->loadComponent('RequestHandler');
    $this->loadComponent('Flash');
    $this->loadComponent('Auth',[
                        'authenticate' => [
                            'Form' => [
                                'fields' => [
                                    'username' => 'email',
                                    'password' => 'password'
                                    ]
                                ]
                            ],
                            'loginAction' => [
                                'controller' => 'Users',
                                'action' => 'login'
                                ]
                        ]);

}

public function beforeRender(Event $event)
{
if($this->request->session()->read(‘Auth.User’)){
$this->set(‘loggedIn’,true);
}
else{
$this->set(‘loggedIn’,false);
}
}

Ah.! That is not the way it is done, looks like you haven’t used beforeRender and beforeFilter events.

Here is the reference:
https://book.cakephp.org/3.0/en/controllers.html#controller-callback-methods

add this namespace in your AppController:

use Cake\Event\Event;

Note That at the bottom of AppController There Must be a beforeRender() function.

In your userController add this namespace as well:

use Cake\Event\Event;

and JUST before the class opening bracket add the following filter:

class UsersController extends AppController
{
public function beforeFilter(Event $event)
{

    parent::beforeFilter($event);
   
    $this->Auth->allow(['YourLoginRedirectActionName']);
}

What this before Filter is doing, It is doing a check before any action is taken. This is where you will define which Controller’s Action access is allowed without authorization.