1

i'm working in cakephp 4 . i have set the model ('Representatives') for login authentication but it is using another model with name users for authentication. i have uploaded the AppController.php file as well as EmployeesController.php file where I defined the method(action).

in Controller/AppController.php

 class AppController extends Controller
 {
   public function initialize(): void
   {
    parent::initialize();

    $this->loadComponent('RequestHandler');
    $this->loadComponent('Flash');       
    $this->loadComponent('Auth', [
        "authenticate" => [
            "Form" => [
                "fields" => [
                    "username" => "email",
                    "password" => "password"
                ],
                "userModel" => "Representatives"
            ]
        ],
        "loginAction" => [
            "controller" => "Employees",
            "action" => "login"
        ],
        "loginRedirect" => [
            "controller" => "Employees",
            "action" => "home"
        ],
        "logoutRedirect" => [
            "controller" => "Employees",
            "action" => "login"
        ]
    ]);
}

in EmployeesController :

 class EmployeesController extends AppController
  {
     public function initialize(): void
     {
      parent::initialize();
      $this->loadModel("Employees");
      $this->loadModel("Representatives");
      $this->loadComponent('Flash');
    }

public function login()
  {
    if ($this->request->is('post')) {
        $user = $this->Auth->identify();
        // echo "<pre>"; print_r($user); die; 
        if ($user) {
            $this->Auth->setUser($user);
            echo "<pre>"; print_r($user); exit(); 
            return $this->redirect($this->Auth->redirectUrl());
        } else {
            $this->Flash->error(__('Username or password is incorrect'));
        }
    }
    $this->viewBuilder()->setLayout('home');
    $this->set("title", "Login");
  }

in model/entity/Representative.php

<?php

namespace App\Model\Entity;

use Authentication\PasswordHasher\DefaultPasswordHasher;
use Cake\ORM\Entity;

class Representative extends Entity
 {
  protected $_accessible = [
    
    'id' => false,
    'name' => true,
    'role' => true,
    'email' => true,
    'password' => true,
    'phone' => true,
    'slug' => false
];

protected function _setPassword(string $password) : ?string
{
    if (strlen($password) > 0) {
        return (new DefaultPasswordHasher())->hash($password);
    }
}
}

in model/table/RepresentativesTable.php

 <?php
 namespace App\Model\Table;

 use Cake\ORM\Table;
 use Cake\Utility\Text;
 use Cake\Event\EventInterface;

 class RepresentativesTable extends Table
 {
  public function initialize(array $config) :void
   {
  

    $this->setTable("newusers");
    


 }


  public function beforeSave(EventInterface $event, $entity, $options)
  {
    if ($entity->isNew()  && !$entity->slug) {
        $sluggedTitle = Text ::slug($entity->name);
        $entity->slug = substr($sluggedTitle, 0, 191);
    }
  }
 }

2 Answers2

1

By default, cakephp uses the user model for authentication

You can find an example of authentication using another route here in this video

In the example, it is defined as follows:

public function getAuthenticationService(ServerRequestInterface $request): AuthenticationServiceInterface
    {
        $authenticationService = new AuthenticationService([
            'unauthenticatedRedirect' => '/cinema4/usuarios/login',
            'queryParam' => 'redirect',
        ]);
    
        // Load identifiers, ensure we check email and password fields
        $authenticationService->loadIdentifier('Authentication.Password', [
            'fields' => [
                'username' => 'login',
                'password' => 'senha',
            ],
            'resolver' => [
                'className' => 'Authentication.Orm',
                'userModel' => 'Usuarios'
            ],        
        ]);
    
        // Load the authenticators, you want session first
        $authenticationService->loadAuthenticator('Authentication.Session');
        // Configure form data check to pick email and password
        $authenticationService->loadAuthenticator('Authentication.Form', [
            'fields' => [
                'username' => 'login',
                'password' => 'senha',
            ],
            'loginUrl' => '/cinema4/usuarios/login',
        ]);
    
        return $authenticationService;
    }    
Dharman
  • 30,962
  • 25
  • 85
  • 135
E. Biagi
  • 452
  • 3
  • 12
0

Although the video translations are terrible. I was able to follow the example in the video, the important part was adding the 'resolver' to allow cakephp to find the right table.

,
            'resolver' => [
                'className' => 'Authentication.Orm',
                'userModel' => 'YourUserTable'
            ],  

This did create another problem with $this->Identiy->get failing, I expect will do add a resolver for this to work as expected also.

Thanks !!

Gordon
  • 1