Reputation: 632
I am using authentication plugin for login in cakephp.
application.php , configAuth() method code
protected function configAuth(): \Authentication\AuthenticationService
{
$authenticationService = new \Authentication\AuthenticationService([
// 'unauthenticatedRedirect' => '/cake_invo/users/login' , //<= manually working fine
'unauthenticatedRedirect' => \Cake\Routing\Router::url(['controller' => 'Users', 'action' => 'login']),
'queryParam' => 'redirect',
]);
// Load identifiers, ensure we check email and password fields
$authenticationService->loadIdentifier('Authentication.Password', [
'fields' => [
'username' => 'email',
'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' => 'email',
'password' => 'password',
],
// 'loginUrl' => '/cake_invo/users/login' //<= manually working fine
'loginUrl' => \Cake\Routing\Router::url(['controller' => 'Users', 'action' => 'login']),
]);
return $authenticationService;
}
middleware method
public function middleware(MiddlewareQueue $middlewareQueue): MiddlewareQueue
{
$middlewareQueue
->add(new ErrorHandlerMiddleware(Configure::read('Error')))
->add(new AssetMiddleware([
'cacheTime' => Configure::read('Asset.cacheTime'),
]))
->add(new RoutingMiddleware($this))
// add Authentication after RoutingMiddleware
->add(new \Authentication\Middleware\AuthenticationMiddleware($this->configAuth()));
return $middlewareQueue;
}
Getting error like image
How can I solve this problem ?
After give command bin/cake routes
Upvotes: 0
Views: 2064
Reputation: 60463
The problem is that your routes aren't loaded yet when you make the call to Router::url()
, hence it will fail.
While the order of the middlewares is correct, ie the authentication middleware is being added after the routing middleware, you are building the authentication service immediately by invoking $this->configAuth()
, which means that the Router::url()
call will be invoked before any of the middlewares have run, specifically before the routing middleware has run, which is responsible for loading your routes.
Instead of passing a built authentication service instance, setup things as shown in the docs, that is make sure that your Application
class implements \Authentication\AuthenticationServiceProviderInterface
, change your configAuth
method to match AuthenticationServiceProviderInterface::getAuthenticationService()
, and then pass $this
to the authentication middleware constructor instead. That way the method will only be invoked when the authentication middleware runs, that is after the routing middleware.
// ...
use Authentication\AuthenticationServiceInterface;
use Authentication\AuthenticationServiceProviderInterface;
use Cake\Http\BaseApplication;
use Psr\Http\Message\ServerRequestInterface;
class Application extends BaseApplication implements AuthenticationServiceProviderInterface
{
// ...
public function getAuthenticationService(ServerRequestInterface $request): AuthenticationServiceInterface
{
// ...
return $authenticationService;
}
public function middleware(MiddlewareQueue $middlewareQueue): MiddlewareQueue
{
$middlewareQueue
// ...
->add(new RoutingMiddleware($this))
->add(new AuthenticationMiddleware($this));
return $middlewareQueue;
}
// ...
}
Upvotes: 2