0
votes

I'm trying to login with POST and Auth however unsuccessfully returning FALSE the function "$ this-> Auth-> identify ()" within my UsersController

Here are my settings from AppController to view

AppController

public function initialize() {
    parent::initialize();
    $this->loadComponent('Flash');
    $this->loadComponent('Auth');
    $this->loadComponent('Cookie');
}

 function beforeFilter(Event $event) {

    $this->Auth->allow('add');
    $this->Auth->config('authorize','Controller',true);
    $this->Auth->config('authenticate', ['Form' => ['fields' => ['username' => 'username', 'password' => 'password']]]);
    $this->Auth->config('logoutRedirect', 'Homes/index',true);
    $this->Auth->config('loginRedirect', 'Homes/index',true);
    $this->Auth->config('loginAction', 'Users/login',true);
    $this->Auth->config('loginError', 'Usuário e/ou senha incorreto(s)',true);
    $this->Auth->config('authError', 'Você precisa fazer login para acessar esta página',true);
    $this->Auth->config('userModel', 'Users',true);

}

UsersController

public function login() {

    if ($this->request->is('post')) {

        /* Here return FALSE */
        if ($this->Auth->identify()) {

            $this->Session->setFlash(__('Welcome, ' . $this->Auth->user('username')));
        } else {
            $this->Auth->flash('Login errado');
        }
    }
}

login.ctp

<?= $this->Form->create(); ?>
<?php // $this->Form->create('Users'); ?>
<fieldset>
    <legend><?= __('Login') ?></legend>
    <?php

        echo $this->Form->input('username');
        echo $this->Form->input('password');
    ?>
</fieldset>
<?= $this->Form->button(__('Login')) ?>
<?= $this->Form->end() ?>

table mysql username and password varchar 200

1
Is your password been hashed out and saved correctly?AKKAweb
debug($this->request->data()); in your login controller and compare with the password of your dbCoolLife

1 Answers

0
votes

Two things - 1, depending on how strict your database is, you might have to make sure the username is saved exactly as you typed it. I've had an issue before where I created a username with camelcase like JohnDoe. Typing 'johndoe' worked for me until I upgraded to MySQL Enterprise, which was incredibly picky and demanded that I type JohnDoe instead.

2 - It looks like your input names match with the fields in your database, but double check that you're saving the password correctly when you set up the account. I'm going to assume you followed the CakePHP 3 tutorial and your password is being saved and hashed through the entity:

    protected function _setPassword($password)
{
    return (new DefaultPasswordHasher)->hash($password);
}
// inside the tutorial on the cake site

Following the instructions in the Cake tutorial should work for you, and be sure to follow suit with $this->Auth->setUser($user); afterwards once it returns true.