6

I want to extend the Laravel stock authentication to use an OAuth server for user retrieval and authentication while taking advantage of the existing functionality. I already managed to extend the EloquentUserProvider to partially overwrite/extend the implementations of the Illuminate\Contracts\Auth\UserProvider contract. The current implementation looks like this:

class EloquentOauthServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap the application services.
     *
     * @return HcOAuthProvider;
     */
    public function boot()
    {
        Auth::provider('oauth',function($app){
            $model = $app['config']['auth.providers.oauth.model'];
            $repository = new OauthUserRepository();
            return new EloquentOauthUserProvider($app['hash'], $model, $repository);
        });
    }

}

In the auth.php config I changed the descriptors like this:

'guards' => [
    'web' => [
        'driver' => 'session',
        'provider' => 'oauth',
    ],
]

'providers' => [
    'oauth' => [
        'driver' => 'oauth',
        'model' => App\Models\User::class,
    ],
]

This is working so far, I can overwrite methods to add my logic. But I realized that I also need to overwrite some methods of the SessionGuard class (login and logout to be specific) since I want to save and retrieve OAuth specific tokens using the Laravel session implementation. There are a couple of suggestions around but they are either not working (Maybe they worked before Laravel 5.2) or would require to overwrite the Authmanager which looks like overkill.

So my question is: What I have to do in Laravel 5.2 to overwrite the SessionGuard?

Noir
  • 474
  • 9
  • 21

2 Answers2

11

Recently I've had the same problem, so maybe the solution is something like this.

<?php
namespace App\CoreExtensions;
use Illuminate\Auth\SessionGuard;
use Illuminate\Contracts\Auth\Authenticatable;
class SessionGuardExtended extends SessionGuard
{
    /**
     * Log a user into the application.
     *
     * @param  \Illuminate\Contracts\Auth\Authenticatable  $user
     * @param  bool  $remember
     * @return void
     */
    public function login(Authenticatable $user, $remember = false)
    {
        $this->updateSession($user->getAuthIdentifier());
        if ($remember) {
            $this->refreshRememberToken($user);
            $this->queueRecallerCookie($user);
        }
        $this->fireLoginEvent($user, $remember);
        $this->setUser($user);
    }
}

In AppServiceProvider.php

public function boot()
{
    Auth::extend(
        'sessionExtended',
        function ($app) {
            $provider = new EloquentUserProvider($app['hash'], config('auth.providers.users.model'));
            return new SessionGuardExtended('sessionExtended', $provider, app()->make('session.store'), request());
        }
    );
}

In Config/auth.php driver should be renamed

'web' => [
    'driver' => 'sessionExtended',
    'provider' => 'users',
],
Nikola Spalevic
  • 1,082
  • 13
  • 18
  • Helpful answer. For those looking at this, I just wanted to add a reference back to the docs Authentication [Adding Custom Guards](https://laravel.com/docs/authentication#adding-custom-guards). The answer above follows the same pattern given there, just that in the auth.config it's "replace built in guard" rather than "add new guard" – Daryn Apr 08 '19 at 09:46
1

Extend guard:

use Illuminate\Auth\SessionGuard;

class MySessionGuard extends SessionGuard {
    // ...
}

Update configs

'web' => [
    'driver' => 'my-session',
    'provider' => 'users',
],

Bind new guard in AuthServiceProvider.php:

public function boot()
    {
        Auth::extend(
            'my-session',
            function ($app, $name, array $config) {
                /* Copy functionality form Auth::createSessionDriver */
                $guard = new MySessionGuard($name, Auth::createUserProvider($config['provider'] ?? null), $this->app['session.store']);
                if (method_exists($guard, 'setCookieJar')) {
                    $guard->setCookieJar($this->app['cookie']);
                }
                if (method_exists($guard, 'setDispatcher')) {
                    $guard->setDispatcher($this->app['events']);
                }
                if (method_exists($guard, 'setRequest')) {
                    $guard->setRequest($this->app->refresh('request', $guard, 'setRequest'));
                }

                return $guard;
            }
        );

       // ...
    }
Derrick
  • 336
  • 5
  • 18
yaroslawww
  • 978
  • 9
  • 12