Login Cakephp 3 - Redirect Logout


#1

I’m trying to create a Login function to enter the dashboard of a site by ajax, but it usually goes in! The functions work without error and even enter the panel, but redirect to the Login page which is the home page. I’ve already checked the functions and see that the $ this->Auth->setUser(); Always returns null, not if you are actually logging in. Is there a part of the site that also stopped cookie, can this be a global system configuration?

namespace App\Controller;

use App\Controller\AppController;
use Cake\Controller\Component;
use Cake\Controller\Component\AuthComponent;
use Cake\Event\Event;
use Cake\ORM\TableRegistry;

class AccountController extends AppController {
    public function initialize() {
        parent::initialize();
        $this->loadComponent('Flash');
        $this->loadComponent('Auth', [
            'authenticate' => [
                'Form' => [
                    'fields' => ['username' => 'email', 'password' => 'password'],
                    'userModel' => 'Account',
                ]
            ],
            'loginAction' => [
                'controller' => '/'
            ],
            'loginRedirect' => [
                'controller' => 'Account',
                'action' => 'Panel'
            ],
            'logoutRedirect' => [
                'controller' => '/'
            ],
            'storage' => 'Memory'
        ]);
        $this->Auth->allow(['index']);
    }

    public function index() {
        if ($this->request->is('ajax')) {
            $user = $this->Auth->identify();
            if ($user) {
                $this->Auth->setUser($user);
                echo 'success';
            } else {
                echo 'incorrect';
            }
        }
        else {
            return $this->redirect('/');
        }
    }
}

#2

Why do you need ajax for login anyway?

Would you elaborate what problem you are facing or what are you trying to achieve in clear words?


#3

I’m sorry, I’m Brazilian. The site will be for an Agency and it requires that most of the functions be by ajax. For example login, forgot my password, sign up for course. They are actions made by modal and can not refresh the page. Also, I can not reload the page just to enter the account that would only change the header of the site or reload to show incorrect message in the modal.