vendredi 16 octobre 2020

Custom Json Guard no longer works after beginning to upgrade Laravel App

I am upgrading an inherited app from Laravel 5.2 to 5.4 (hoping to go on to the latest version) But I have run into an error with the JSON Guard no longer working. This code is working (in production) in v5.2 after the changes to v5.4 I am now getting Argument 3 passed to App\Services\Auth\JsonGuard::__construct() must be an instance of Symfony\Component\HttpFoundation\Session\SessionInterface, instance of Illuminate\Session\Store given

This is the JsonGuard __construct:

namespace App\Services\Auth;
 
use Illuminate\Contracts\Auth\UserProvider;
use Illuminate\Contracts\Auth\StatefulGuard;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
 
class JsonGuard implements StatefulGuard
{
      /**
       * Create a new authentication guard.
       *
       * @param  \Illuminate\Contracts\Auth\UserProvider  $provider
       * @param  \Symfony\Component\HttpFoundation\Session\SessionInterface  $session
       * @param  \Symfony\Component\HttpFoundation\Request  $request
       * @return void
       */
      public function __construct($name,
                                  UserProvider $provider,
                                  SessionInterface $session,
                                  Request $request) {
        $this->name = $name;
        $this->session = $session;
        $this->request = $request;
        $this->provider = $provider;
      }
     

This is the AuthServiceProvider

use Illuminate\Support\Facades\Auth;
use Illuminate\Contracts\Auth\Access\Gate as GateContract;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
use App\Services\Auth\JsonGuard;
use App\Extensions\OptimumHQUserProvider;
use App\Database\OptimumHQDatabase;
use App\Models\Auth\User;
// use Illuminate\Http\Request;
// use Illuminate\Support\Facades\Config;

class AuthServiceProvider extends ServiceProvider
{
    /**
     * The policy mappings for the application.
     *
     * @var array
     */
    protected $policies = [
        'App\Model' => 'App\Policies\ModelPolicy',
    ];

    /**
     * Register any application authentication / authorization services.
     *
     * @return void
     */
    public function boot(GateContract $gate)
    {
        $this->registerPolicies($gate);

        $this->app->bind('App\Database\OptimumHQDatabase', function ($app) {
          return new OptimumHQDatabase(config('optimumhq.defaults.host'), config('optimumhq.defaults.username'), config('optimumhq.defaults.token'));
        });
         
        $this->app->bind('App\Models\Auth\User', function ($app) {
          return new User($app->make('App\Database\OptimumHQDatabase'));
        });
     
        // add custom guard provider
        Auth::provider('optimumhq', function ($app, array $config) {
          return new OptimumHQUserProvider($app->make('App\Models\Auth\User'));
        });
     
        // add custom guard
        Auth::extend('json', function ($app, $name, array $config) {
          return new JsonGuard('json', Auth::createUserProvider($config['provider']), $this->app['session.store'], $app->make('request'));
        });
    }

The error is that $this->app['session.store'] is the instance of "store" but I do not know how to change it to be an instance of SessionInterface or how to track it all of the way back to where it is initially set.

I "think" if I can change $this->app['session.store'] to a SessionInterface it would work. I am very unsure as to why it worked in 5.2 but no longer. It seems it may have something to do with https://laravel.com/docs/5.3/upgrade#5.3-session-in-constructors but I wasn't able to fully understand the middleware implementation and my attempt at the type hinting failed.

Does anyone have a suggestion as to where to start?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire