扩展Laravel 5.2 SessionGuard [英] Extending Laravel 5.2 SessionGuard

查看:185
本文介绍了扩展Laravel 5.2 SessionGuard的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想扩展Laravel股票身份验证,以便在利用现有功能的同时使用OAuth服务器进行用户检索和身份验证.我已经设法扩展EloquentUserProvider来部分覆盖/扩展Illuminate\Contracts\Auth\UserProvider合同的实现.当前的实现如下所示:

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);
        });
    }

}

auth.php配置中,我像这样更改了描述符:

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,
    ],
]

到目前为止,这是可行的,我可以覆盖方法以添加我的逻辑.但是我意识到我还需要覆盖SessionGuard类的某些方法(具体来说是loginlogout),因为我想使用Laravel会话实现来保存和检索OAuth特定的令牌.周围有一些建议,但它们要么不起作用(也许在Laravel 5.2之前就起作用了),要么需要覆盖看起来过大的Authmanager.

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.

所以我的问题是:我必须在Laravel 5.2中做什么才能覆盖SessionGuard?

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

推荐答案

最近我遇到了同样的问题,所以也许解决方案是这样的.

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);
    }
}

在AppServiceProvider.php中

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());
        }
    );
}

在Config/auth.php驱动程序中应重命名

In Config/auth.php driver should be renamed

'web' => [
    'driver' => 'sessionExtended',
    'provider' => 'users',
],

这篇关于扩展Laravel 5.2 SessionGuard的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆