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('/');
}
}
}