Tuesday, July 21, 2015

Re: CakePHP 3 - Login form not working

Hi, 

Did you make sure that the password column in your database is long enough for a hashed password? 

Otherwise your code looks fine and should actually work, I guess. 

Cheers, 
Sajoscha

On Jul 20, 2015, at 9:33 PM, Mark Dijkema <mark.dijkema@gmail.com> wrote:

I am following both the tutorials in the CakePHP 3 documentation and I am trying to get the login functionality working, but so far I have no luck.
I have a Users table with the 2 fields 'username' and 'password'. The password is hashed with `(new DefaultPasswordHasher)->hash($password);`

After I enter the username and password, I do get into the login function, but the `$this->Auth->identify();` never returns an user.
The post data is filled, but the password is not hashed yet. I assume this happens in `$this->Auth->identify();`?

Could anyone tell me what I ma doing wrong? I am not getting any errors I could work with.

I have the following form:
<?php
    if(!is_null($this->request->session()->read('Auth.User.username')))
    {
        echo 'logged in';
    }
    else
    {
        echo $this->Form->create('User', array('action' => 'login'));
        echo $this->Form->input('username', [
            'class' => 'w3-input',
                'placeholder' => 'username',
                'label' => false
            ]);
        echo $this->Form->input('password', [
            'class' => 'w3-input',
                'placeholder' => 'password',
                'label' => false
            ]);
        echo $this->Form->button('Login', [
            'class' => 'w3-btn'
        ]);
        echo $this->Form->end();
    }
?>

this is my AppController:
public function initialize()
    {
        parent::initialize();
        $this->loadComponent('Flash');
        $this->loadComponent('Auth', [
            'scope' => [
                'Users.active' => 1
            ],
            'authenticate' => [
                'Form' => [
                    'fields' => [
                        'username' => 'username',
                        'password' => 'password'
                    ]
                ]
            ],
            'loginAction' => [
                'controller' => 'Users',
                'action' => 'login'
            ],
            'logoutRedirect' => [
                'controller' => 'Users',
                'action' => 'homepage'
            ]
        ]);

        $this->layout = 'creed';
    }

In the query it uses (she bottom of post), it never uses the scope at all or the password field.

And this is my login function:

public function login()
    {
        if($this->request->is('post'))
        {
            $user = $this->Auth->identify();
            if($user)
            {
                $this->Auth->setUser($user);

                return $this->redirect($this->Auth->redirectUrl());
            }
        }
    }


This is the query that is being used in the login function
SELECT `Users`.`id` AS `Users__id`,
        `Users`.`username` AS `Users__username`,
        `Users`.`password` AS `Users__password`,
        `Users`.`email` AS `Users__email`,
        `Users`.`level` AS `Users__level`,
        `Users`.`image` AS `Users__image`,
        `Users`.`signature` AS `Users__signature`,
        `Users`.`active` AS `Users__active`,
        `Users`.`date_joined` AS `Users__date_joined`
    FROM `users` `Users`
    WHERE `Users`.`username` = 'DijkeMark'
    LIMIT 1


This returns 1 result, because there is a user in the DB with this name. But the password field is not being used.

I also baked the Entity and the Table file, so that should be correct too. I really start to hate CakePHP and it's authentication system, because I have done everything I am supposed to do and it gives not a single error on what I am doing wrong.

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.

No comments: