PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, January 18, 2022

[FIXED] Users Authentication fail

 January 18, 2022     cakephp, cakephp-3.0     No comments   

Issue

So this is my login method:

public function login()
    {
        if ($this->request->is('post')) {
            $user = $this->Auth->identify();
            if ($user) {
                $this->Auth->setUser($user);
                return $this->redirect($this->Auth->redirectUrl());
            } else {
                $this->Flash->error(__('Invalid username or password, try again'));
            }

        }
    }

And this is how I has my password inside my User entity:

<?php
namespace App\Model\Entity;

use Cake\Auth\DefaultPasswordHasher;
use Cake\ORM\Entity;

/**
 * User Entity.
 */
class User extends Entity
{

    /**
     * Fields that can be mass assigned using newEntity() or patchEntity().
     *
     * @var array
     */
    protected $_accessible = array(
        'first_name' => true,
        'last_name' => true,
        'email' => true,
        'username' => true,
        'password' => true,
        'role' => true,
        'created' => true
    );
    protected function _setPassword($password)
    {
        return (new DefaultPasswordHasher)->hash($password);
    }
}

I already have working user registration and it's working without problems(password is being hashed), but for some reason I can't login, it always says that

Invalid username or password, try again

But those are present in my table, here is the query:

SELECT 
    Users.id AS `Users__id`, 
    Users.first_name AS `Users__first_name`, 
    Users.last_name AS `Users__last_name`, 
    Users.email AS `Users__email`, 
    Users.username AS `Users__username`, 
    Users.password AS `Users__password`, 
    Users.role AS `Users__role`, 
    Users.created AS `Users__created` 
FROM 
    users Users 
WHERE 
    Users.username = 'admin' LIMIT 1

But for some reason it does nothing in my app. I guess something is wrong with the password?

Form:

<div class="users form">
    <?= $this->Flash->render('auth') ?>
    <?= $this->Form->create() ?>
        <fieldset>
            <legend><?= __('Please enter your username and password') ?></legend>
            <?= $this->Form->input('username') ?>
            <?= $this->Form->input('password') ?>
        </fieldset>
    <?= $this->Form->button(__('Login')); ?>
    <?= $this->Form->end() ?>
</div>

AppController:

class AppController extends Controller
{

    /**
     * Initialization hook method.
     *
     * Use this method to add common initialization code like loading components.
     *
     * @return void
     */
    public function initialize()
    {
        $this->loadComponent('Flash');
        $this->loadComponent('Auth', [
            'loginRedirect' => [
                'controller' => 'Jobs',
                'action' => 'index'
            ],
            'logoutRedirect' => [
                'controller' => 'Jobs',
                'action' => 'index'
            ],
            'authorize' => ['controller']
        ]);
    }

    public function beforeFilter(Event $event)
    {
        $this->Auth->allow(['index', 'browse', 'view', 'register']);
    }
}

Solution

Found the answer here: Login [ Auth->identify() ] always false on CakePHP 3

Summary:

Table field password was VARCHAR(45) instead of VARCHAR(255)



Answered By - Tachi
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing