Search code examples
authenticationcakephppluginscakephp-3.xcakephp-3.8

Cakephp 3 Authentication plugin, login URL did not match


I want to use the Authentication plugin for CakePHP 3.8 and I'm having problems that are not in documentation.

After follow Getting Started (https://book.cakephp.org/authentication/1/en/index.html) I have one question.

Originally $fields were specified to change username and password relation in real database, same that Auth component, and login URL is where login form si loaded.

First, in getting started or any part of documentation doesn't says about a login view (form), so, like old Auth Component I created this to Users/login.ctp

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

My code in Application.php includes this (with its respective uses and implements):

    public function getAuthenticationService(ServerRequestInterface $request, ResponseInterface $response)
    {
        $service = new AuthenticationService();

        $fields = [
            'username' => 'email',
            'password' => 'password'
        ];

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

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

        return $service;
    }

But when I try to login, I have this error, after var_dump in login.ctp I get this:

object(Authentication\Authenticator\Result)[124]
  protected '_status' => string 'FAILURE_OTHER' (length=13)
  protected '_data' => null
  protected '_errors' => 
    array (size=1)
      0 => string 'Login URL `http://localhost/users/login` did not match `/users/login`.' (length=70)

If I comment 'loginUrl' => '/users/login' line, then login works fine.

Additional notes: - I've tested with hashed and textplane passwords, same results. - I've added $this->Authentication->allowUnauthenticated(['view', 'index', 'login', 'add']); in beforeFilter to access login. - It's a clean cakephp 3.8 install, only database is the same for tests. - I've added Crud only with cake console.

I would like to learn more about that loginURL, I should include some uses in UsersController? What causes this error?

Thank you


Solution

  • The error messages is currently a little misleading, as it doesn't show you the possible base directory, which is the actual issue that you are experiencing. I've proposed a fix for that, which may make into the next release.

    When your application lives in a subdirectory, you need to make sure that your login URL configuration takes that into account, that is by either passing the URL including the base directory, which you could do either manually:

    'loginUrl' => '/myapp/users/login'
    

    or by using the router:

    'loginUrl' => \Cake\Routing\Router::url('/users/login')
    
    'loginUrl' => \Cake\Routing\Router::url([
        'plugin' => null,
        'prefix' => null,
        'controller' => 'Users',
        'action' => 'login'
    ])
    

    Another option would be to use the routes based URL checker, which can be configured via the form authenticators urlChecker option, then you can define the login URL using URL arrays, without having to use the router:

    'urlChecker' => 'Authentication.CakeRouter',
    'loginUrl' => [
        'plugin' => null,
        'prefix' => null,
        'controller' => 'Users',
        'action' => 'login'
    ]
    

    See also: