4 years ago

L5 extending Guard object in Authentication framework

Posted 4 years ago by fulup

I'm trying to add few methods to Auth in order to do things like $this->auth->setLOA(xxx); Obliviously I would like to do this without touching core source file from L5.

As far this is what I did. - extended Guard class to add my 'setLOA' method. - created a new LoaAuthServiceProvider that extends AuthServiceProvider and overloaded "registerAuthenticator" to force registration of my own manager. - created a new LoaAuthManager that point to my new LOAGuard class. - implemented interface & Facade for my LoaGuard class.

I replace the original 'Auth' Service provider with my in app/config. While constructor of my LoaAuthManager is called normally, the method 'createDatabaseDriver' is never called.

My service provider

class LoaAuthProvider extends AuthServiceProvider {
    protected function registerAuthenticator()  {
        $abstract = 'OIDconnec\LoaAuth\LoaAuthInterface';
    $this->app->bind ('auth', function($app) {
    $app['auth.loaded'] = true;
    return new LoaAuthManager($app);
    $this->app->singleton('auth.driver', function($app) {
        return $app['auth']->driver();

        // Overload standard aliases [usefull ???]
    $this->app->booting(function() {
        $loader = AliasLoader::getInstance();
        $loader->alias('auth'       , 'OidConnect\LoaAuth\LoaAuthManager');
        $loader->alias('auth.driver', ['OidConnect\LoaAuth\LoaAuthService','OIDconnec\LoaAuth\LoaAuthInterface']);

I'm not sure that the alias part is useful, but I'm concern that in file vendor/laravel/framework/src/Illuminate/Foundation/Application.php hardcode alias on for both auth and auth.driver. This might be where the binding on between Guard interface and concrete class is done ? In standard authentication case we use Guard's Interface from src/Illuminate/Contracts/Auth/Guard.php as the injected class. if I replace this with my own LoaGuardInterface and I get en error as Interface cannot be instantiated. I must miss a binding somewhere ?

  • Question-1 : how is Guard standard Interface replace with the concrete class of Guard ? Where is this binding declare.
  • Question-2: what the role of createDatabaseDrive in AuthManager ? In which case is this method called ?

My manager

use Illuminate\Auth\AuthManager;
class LoaAuthManager extends AuthManager {
    protected function callCustomCreator($driver)   {
        $custom = parent::callCustomCreator($driver);
        if ($custom instanceof Guard) return $custom;
        return new LoaGuard($custom, $this->app['session.store']);
   public function createDatabaseDriver()    {
        $provider = $this->createDatabaseProvider();
        return new LoaGuard($provider, $this->app['session.store']);

Please sign in or create an account to participate in this conversation.