change Cakephp 4 Authenticator userModel

Asked

Viewed 155 times

-2

I am using a table named ga_usuario for access management, I generated the skeleton with Bake in cakephp and the controller became Gausuariocontroller, the problem is that when I log in it says that the users table does not exist, the question is, how can I set userModel to my model ?

My apllication.php has the following function for authentication:

    public function getAuthenticationService(ServerRequestInterface $request): AuthenticationServiceInterface
{
    $service = new AuthenticationService();
    $service->setConfig([
        'unauthenticatedRedirect' => '/cake-erp/GaUsuario/login',
        'queryParam' => 'redirect',
    ]);

    $fields = [
        'username' => 'usuario',
        'password' => 'senha'
    ];

    // Load the authenticators, you want session first
    $service->loadAuthenticator('Authentication.Session');
    $service->loadAuthenticator('Authentication.Form', [
        'fields' => $fields,
        'loginUrl' => '/cake-erp/GaUsuario/login'
    ]);

    // Load identifiers
    $service->loadIdentifier('Authentication.Password', compact('fields'));

    return $service;
}

Even adding the 'Usermodel' => 'Gausuario' it does not identify and keeps giving error, see the error below

inserir a descrição da imagem aqui

2 answers

0

Edit:

for those who have had this problem, to use a different table for users, it is necessary to change the ORM resolve that is found in : vendor cakephp Authentication src Identifier Resolve Ormsolve changing userModel=> 'userModel'

or pass via parameter.

0

The passage via paramenters would be like this. I discovered after having made it work by changing the Ormresolver.

https://book.cakephp.org/authentication/2/en/migration-from-the-authcomponent.html#migrate-authcomponent-Settings

public Function getAuthenticationService(Serverrequestinterface $request): Authenticationserviceinterface { $authenticationService = new Authenticationservice([ 'unauthenticatedRedirect' => '/usuarios/login', 'queryParam' => 'redirect', ]);

    // Load identifiers, ensure we check email and password fields
    $authenticationService->loadIdentifier('Authentication.Password', [

        'resolver' => [    <<=================== incluir
            'className' => 'Authentication.Orm', <<=======
            'userModel' => 'Usuarios', <<===========
        ],

        'fields' => [
            'username' => 'username',
            'password' => 'password',
        ]
    ]);

    // 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' => 'username',
            'password' => 'password',
        ],
        'loginUrl' => '/usuarios/login',
    ]);

    return $authenticationService;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.