Hello, I can not login, it seems like the post, never comes, I’m not sure if it’s a problem with the AJAX or Cakephp
Let’s see if you can help me.
Thanks, then I put the code of the different files:
AppController:
public function initialize() {
parent::initialize();
$this->loadComponent('RequestHandler', [
'enableBeforeRedirect' => false,
]);
$this->loadComponent('Flash');
$this->loadComponent('Auth',[
'authorize' => ['Controller'],
'authenticate' => [
'Form' => [
'fields' => [
'username' => 'email',
'password' => 'password'
],
'userModel' => 'Users'
]
],
'loginAction' => [
'controller' => 'Users',
'action' => 'login'
],
'loginRedirect' => [
'controller' => 'Historia',
'action' => 'historia'
],
'logoutRedirect' => [
'controller' => 'Users',
'action' => 'login'
]
]);
}
public function beforeFilter(Event $event) {
$this->Auth->allow(['index', 'view', 'display']);
}
public function isAutorized($user) {
return true;
}
UsersController:
public function beforeFilter(Event $event) {
parent::beforeFilter($event);
$this->Auth->allow('add','logout');
}
public function login() {
$isPost = $this->request->is('post');
if($isPost) {
$user = $this->Auth->identify();
if($user) {
$this->Auth->setUser($user);
return $this->redirect($this->Auth->redirectUrl());
}
else {
$this->Flash->error(__('Email o contraseña incorrecta.'));
}
}
}
public function logout() {
return $this->redirect($this->Auth->logout());
}
public function home() {
$this->render();
}
login.ctp:
<div id="login">
<?= $this->Flash->render('auth') ?>
<?= $this->Form->create('Usuario',['id' => 'loginform', 'class' => 'form loginform']); ?>
<fieldset>
<legend><?= __('Por favor entra tu email y contraseña') ?></legend>
<?= $this->Form->control('email', ['placeholder' => 'E-mail', 'size' => '30', 'maxlength' => '30']) ?>
<?= $this->Form->control('password', ['type' => 'password', 'placeholder' => 'Contraseña', 'maxlength' => '255']) ?>
</fieldset>
<?= $this->Form->button(__('Acceder', ['id' => 'submit', 'class' => 'submit'])); ?>
<?= $this->Html->link('Registrarse', ['controller' => 'Users', 'action' => 'add'], ['id' => 'registro', 'class' => 'registro']) ?>
<?= $this->Form->end() ?>
</div>
routes:
$routes->connect(’/’, [‘controller’ => ‘Users’, ‘action’ => ‘login’]);
User.php:
protected function _setPassword($password)
{
if (strlen($password) > 0) {
return (new DefaultPasswordHasher)->hash($password);
}
}
protected $_hidden = [
'password'
];
UsersTable.php:
public function buildRules(RulesChecker $rules)
{
$rules->add($rules->isUnique(['email']));
return $rules;
}
jquery.js:
$(document).ready(function() {
if (window.File && window.FileReader && window.FileList && window.Blob) {
} else {
alert('Si esta viendo esto por favor dimelo.');
}
$("body").on("change", "#files", function(event) {
var files = event.target.files[0];
var reader = new FileReader();
reader.onload = (function(theFile) {
return function(e) {
$(datos).html('<img class="thumb" src="' + e.target.result + '" title="' + escape(theFile.name) + '"/>');
};
})(files);
reader.readAsDataURL(files);
});
$("body").on("submit", "form", function(event) {
event.stopPropagation();
event.preventDefault();
var loginform = "." + $(this,"form").prop("class");
var type = "POST";
var url = $(this, "form").prop("action");
var cache = false;
var contentType = 'application/x-www-form-urlencoded';
var processData = true;
if(event.target.files) {
var data = new FormData(this);
contentType = false;
processData = false;
}
else {
var data = $(this).serialize();
}
$.ajax({
url: url,
data: data,
type: type,
cache: cache,
contentType: contentType,
processData: processData
})
.done(function(data) {
if(loginform === ".form loginform") {
$(".section").html(data);
}
else {
$(".section").html(data);
}
})
.fail(function(jqXHR, textStatus, errorThrown) {
if(jqXHR.status === 0) {
alert('Not connect: Verify Network.');
} else if(jqXHR.status == 404) {
alert('Requested page not found [404]');
} else if(jqXHR.status == 500) {
alert('Internal Server Error [500].');
} else if(textStatus === 'parsererror') {
alert('Requested JSON parse failed.');
} else if(textStatus === 'timeout') {
alert('Time out error.');
} else if(textStatus === 'abort') {
alert('Ajax request aborted.');
} else {
alert('Uncaught Error: ' + jqXHR.responseText);
}
})
.always(function(result) {
});
});
});