Error the Authentication Service Interface

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

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.


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!