0
I’m using ajax to log in to my system using Cakephp
Page with Ajax
<form id="login">
<input type="text" name="username" placeholder="Usuário" required>
<input type="text" name="password" placeholder="Senha" required>
<button onclick="enviar()" type="submit" class="button button-block button-outline button-positive">Entrar</button>
</form>
<script type="text/javascript" src="js/jquery-1.11.2.min.js"></script>
<script type="text/javascript">
function enviar() {
var formula = $('#login').serialize();
$.ajax({
type: 'POST',
dataType: 'json',
data: formula,
url: 'http://localhost/teste/web/users/login_app',
success: function(data) {
if(data == true) {
window.location = 'dash.html';
}
if(data == false) {
alert('Usuário ou senha incorreta');
}
},
error: function(error) {
console.log(error.responseText);
}
})
}
</script>
Function of the Cakephp controller
public function login_app() {
if($this->request->is('post')) {
if ($this->Auth->login()) {
echo json_encode(true);
exit();
} else {
echo json_encode(false);
exit();
}
}
}
login_app.ctp
<?php
echo $this->Session->flash('auth');
echo $this->Form->create('User');
echo $this->Form->input('username', array('type' => 'text'));
echo $this->Form->input('password', array('type' => 'text'));
echo $this->Form->end('Entrar');