With another CakePHP 4 installation on Linux, I can redirect after login for only the first few days. After a weeks of develop other models and controller, the redirection does not work anymore. Did not change anything in authentication process apart from adding LDAP identifier.
What I did for Authentication are as below, the code is copied from my application, based on CakePHP 4 documentation.
In Application.php:
Implement the interface
class Application extends BaseApplication implements AuthenticationServiceProviderInterface
the use statements
use Cake\Core\Configure;
use Cake\Core\ContainerInterface;
use Cake\Core\Exception\MissingPluginException;
use Cake\Datasource\FactoryLocator;
use Cake\Error\Middleware\ErrorHandlerMiddleware;
use Cake\Http\BaseApplication;
use Cake\Http\Middleware\BodyParserMiddleware;
use Cake\Http\Middleware\CsrfProtectionMiddleware;
use Cake\Http\MiddlewareQueue;
use Cake\ORM\Locator\TableLocator;
use Cake\Routing\Middleware\AssetMiddleware;
use Cake\Routing\Middleware\RoutingMiddleware;
use Authentication\AuthenticationService;
use Authentication\AuthenticationServiceInterface;
use Authentication\AuthenticationServiceProviderInterface;
use Authentication\Identifier\IdentifierInterface;
use Authentication\Middleware\AuthenticationMiddleware;
use Cake\Routing\Router;
use Psr\Http\Message\ServerRequestInterface;
the middleware function
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(new BodyParserMiddleware())
->add(new AuthenticationMiddleware($this))
->add(new CsrfProtectionMiddleware([
'httponly' => true,
]));
return $middlewareQueue;
}
The getAuthenticationService function
public function getAuthenticationService(ServerRequestInterface $request): AuthenticationServiceInterface
{
$service = new AuthenticationService();
// Define where users should be redirected to when they are not authenticated
$service->setConfig([
'unauthenticatedRedirect' => Router::url([
'prefix' => false,
'plugin' => null,
'controller' => 'Users',
'action' => 'login',
]),
'queryParam' => 'redirect',
]);
$fields = [
IdentifierInterface::CREDENTIAL_USERNAME => 'username',
IdentifierInterface::CREDENTIAL_PASSWORD => 'password'
];
// Load the authenticators. Session should be first.
$service->loadAuthenticator('Authentication.Session');
$service->loadAuthenticator('Authentication.Form', [
'fields' => $fields,
'loginUrl' => Router::url([
'prefix' => false,
'plugin' => null,
'controller' => 'Users',
'action' => 'login',
]),
]);
// Load identifiers
$service->loadIdentifier('Authentication.Ldap', [
'host' => Configure::read('LDAP_server'),
'port' => Configure::read('LDAP_port'),
'bindDN' => function ($username) {
return "$username" . Configure::read('LDAP_suffix');
},
'ldap' => \Authentication\Identifier\Ldap\ExtensionAdapter::class,
'options' => [
\LDAP_OPT_PROTOCOL_VERSION => 3,
],
]);
//$service->loadIdentifier('Authentication.Password', compact('fields'));
$service->loadIdentifier('Authentication.Password',
['fields' => $fields,
'resolver' => ['className' => 'Authentication.Orm', 'finder' => 'active']
]
);
return $service;
}
And login() function in UsersController is as normal login with little LDAP data extraction and formatting
The code for redirection is as below
$target = $this->Authentication->getLoginRedirect() ?? '/timesheets';
return $this->redirect($target);
However, the redirected route always is: /timesheets
What can I fixed?