Issue with AuthenticationServiceProviderInterface
Hello, I am following the tutorial to create a user and authentication system : Quick Start - 2.x
I followed the steps correctly but I got this error:
Fatal error : Class App\Application contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (Authentication\AuthenticationServiceProviderInterface::getAuthenticationService) in **/Users/clementlanot/Sites/CLPRESS/src/Application.php
When I add:
class Application extends BaseApplication implements AuthenticationServiceProviderInterface
I searched but I don’t understand how to fix this? Can you help me ? Thank you !!
Implementing an interface means you need to add the methods this interface declares.
The interface authentication/AuthenticationServiceProviderInterface.php at 2.x · cakephp/authentication · GitHub decalres 1 method which is
public function getAuthenticationService(ServerRequestInterface $request): AuthenticationServiceInterface;
and just like the documentation says you have to add that method to your Application.php with the code block given in the documentation.
Otherwise please watch/follow my cakefest workshop where I go through the whole process
1 Like
Hi, thank you for your super fast response!
I did exactly what the doc asks to do, copying the given code but I got this error.
Is it possible that I made a mistake earlier in the rest of my code or that there is a problem with my Cakephp app?
I’m not new to Cake but I hadn’t touched it for 2 years so a lot of things have changed. I will watch your video to see if I find a solution.
<?php
declare(strict_types=1);
namespace App;
use Cake\Core\Configure;
use Cake\Core\ContainerInterface;
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;
class Application extends BaseApplication implements AuthenticationServiceProviderInterface
{
Its not about the use
statements at the top - its about the function
you have to delcare inside your
class Application extends BaseApplication implements AuthenticationServiceProviderInterface
But please follow the video and everything should be clear.
Rookie mistake, thanks for your help! great video !
In your video you did :
} catch (ForbiddenException Sex){
$this->Flash->error("Erreur: l'utilisateur ne peut pas être supprimé.");
}
It’s not working for me, but I try this and its work.
} catch (\Exception $e) {
$this->Flash->error("Erreur: l'utilisateur ne peut pas être supprimé.");
}
Can u help me to understand why ?
If you don’t specify the fully qualified class name (FQCN) when referencing a class you have to add the use
statemenet at the top of your class.
So only doing
} catch (ForbiddenException Sex){
won’t work because you need
use \Authorization\Exception\ForbiddenException;
at the top to import that class.
But EVERY exception is a sub-class of the parent \Exception
class, therefore a generic
} catch (\Exception $e) {
$this->Flash->error("Erreur: l'utilisateur ne peut pas être supprimé.");
}
will catch ALL exceptions, no matter which one it is. (\Exception
is a FQCN because it starts with a \
)
But in this example we only want to catch exceptions which are related to the authorization system, therefore we only want to catch \Authorization\Exception\ForbiddenException
Since you seem a bit unfimilar with PHP namespaces I would also recommend you watch this video
Thank you ! It’s true that the last time I developed and used Cake PHP, it must have been with version 2 and the Auth component. I lost my touch a bit on all his statements, it’s true!
Hey Kevin,
thank you for your Video, that helped me a lot. Unfortunately I get an error
Undefined constant Authentication\Identifier\IdentifierInterface::CREDENTIAL_USERNAME
Here ist the snipplet of the position of the error:
` APP/Application.php at line 158 [(edit)](phpstorm://open?file=C:\PHP\cms\src\Application.php&line=158)
||` 'queryParam' => 'redirect',`|
| --- | --- |
||` ]);`|
||``|
||` $fields = [`|
||` IdentifierInterface::CREDENTIAL_USERNAME => 'email',`|
||` IdentifierInterface::CREDENTIAL_PASSWORD => 'password'`|
||` ];`|
||` // Load the authenticators. Session should be first.`|
||` $service->loadAuthenticator('Authentication.Session');`|`
I guess now I am very close that it will be running soon with authentication
Thank you very much for your help
David
If you are running on the latest version of Cake and the Auth plugins these constants have moved to
\Authentication\Identifier\AbstractIdentifier::CREDENTIAL_USERNAME => 'email',
\Authentication\Identifier\AbstractIdentifier::CREDENTIAL_PASSWORD => 'password'
Yeaaah!! Great!! That worked immediately!!
Thank you very much!
David