Ajax + Auth CakePHP only returns false

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>

CakePHP controller function

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');
    
asked by anonymous 28.03.2015 / 13:47

1 answer

0

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

    
18.06.2015 / 20:01