Skip to content

Instantly share code, notes, and snippets.

@calebporzio
Created April 19, 2017 21:30

Revisions

  1. calebporzio created this gist Apr 19, 2017.
    30 changes: 30 additions & 0 deletions auth_macro_hack.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    <?php

    // Until SessionGuard Macros get pulled into Laravel core from a PR:
    // Remove this when Laravel 5.4.18 is released.

    $this->app->config->set('auth.guards.hack', [
    'driver' => 'modified-session',
    'provider' => 'users',
    ]);

    Auth::extend('modified-session', function ($app, $name, $config) {
    // The following is taken from AuthManager@createSessionDriver
    $provider = Auth::createUserProvider($config['provider']);
    $guard = new class ($name, $provider, $app['session.store']) extends SessionGuard {
    // This allows Auth::macro(...)
    use Macroable;
    };
    if (method_exists($guard, 'setCookieJar')) {
    $guard->setCookieJar($app['cookie']);
    }
    if (method_exists($guard, 'setDispatcher')) {
    $guard->setDispatcher($app['events']);
    }
    if (method_exists($guard, 'setRequest')) {
    $guard->setRequest($app->refresh('request', $guard, 'setRequest'));
    }
    return $guard;
    });

    Auth::shouldUse('hack');