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

Saturday, March 19, 2022

[FIXED] CakePHP3 login auth to admin side always false

 March 19, 2022     admin, authentication, cakephp-3.0     No comments   

Issue

I know theres a few similar question, but i dont find answer which resolved my problem. My problem is authentication is always false. This is my code:

My AdminController

class AdminController extends AppController
{
  public function initialize()
  {
    parent::initialize();
    $this->loadComponent('Auth', [
        'authenticate' => [
            'Form' => [
                'userModel' => 'Admins',
                'fields' => [
                    'username' => 'email',
                    'password' => 'password'
                ]
            ]
        ],
        'loginAction' => [
            'controller' => 'Innerlogin',
        ]
    ]);
  }
}

InnerloginController:

namespace App\Controller\Admin;

use App\Controller\AdminController;
use Cake\Event\Event;

class InnerloginController extends AdminController
{
  public function index()
  {
    $this->loadModel('Admins');

    if( $this->request->is('POST') )
    {
        $admin = $this->Auth->identify();

        if( $admin )
        {
            $this->Auth->setUser( $admin );
            return $this->redirect([ 'controller' => 'admin' ]);
        }
        else
        {
            $this->Flash->error( 'Incorrect username or password' );
        }
    }

  }

}

Entity/Admin

namespace App\Model\Entity;

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


class Admin extends Entity
{

   protected $_accessible = [
       '*' => true,
       'admins_id' => false
   ];

   protected function _setPassword( $password )
   {
       return( new DefaultPasswordHasher() )->hash( $password );
   }
}

Innerlogin/index.ctp

<?= $this->Form->create() ?>
   <?= $this->Form->input('username', ['label' => false, 'class' => 'input-cst' ]) ?>
   <?= $this->Form->input('password', ['label' => false, 'class' => 'input-cst', 'type' => 'password' ]) ?>
   <?= $this->Form->button(__('Login'), ['class' => 'btn btn-success right']) ?>
<?= $this->Form->end() ?>

My admins table

Field       Type          Null  Key Default Extra
admins_id   int(11)       NO    PRI NULL    auto_increment
name        varchar(40)   NO        NULL    
email       varchar(50)   NO        NULL    
password    varchar(255)  NO        NULL    
sex         varchar(1)    NO        NULL    
birthday    varchar(40)   NO        NULL    
created     varchar(40)   NO        NULL    
deleted     varchar(40)   NO        0   

Any idea what am i doing wrong ? And by the way i have another related question. I want to make two authentication, one for users another for admins (I dont want to keep role column in one table and check is user or admin). Can i do that? It will be works properly ?


Solution

The fields option is not a "input field to database column" map, it is used to configure what fields/columns to use instead!

You have configured the form authenticator to use the email field/column instead of the username field/column, and so you have to change that in your form accordingly to use email too, ie

<?= $this->Form->input('email', ['label' => false, 'class' => 'input-cst' ]) ?>


Answered By - ndm
  • 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