Ajax + Auth Cakephp only returns false

Asked

Viewed 50 times

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');

1 answer

0

Make sure your Cakephp application is calling the Authentication components, if it is, make sure it is setting the Authentication controller and model correctly.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.