Extending Laravel 5.2 SessionGuard

后端 未结 1 1966
粉色の甜心
粉色の甜心 2020-12-18 05:55

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 alread

相关标签:
1条回答
  • 2020-12-18 06:29

    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',
    ],
    
    0 讨论(0)
提交回复
热议问题