Skip to content

Instantly share code, notes, and snippets.

@tobiashm
Created June 13, 2019 12:21
Show Gist options
  • Save tobiashm/ad785be8ab92d3e2284cf39768ce65a4 to your computer and use it in GitHub Desktop.
Save tobiashm/ad785be8ab92d3e2284cf39768ce65a4 to your computer and use it in GitHub Desktop.

Revisions

  1. tobiashm revised this gist Jun 13, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion AuthServiceProvider.php
    Original file line number Diff line number Diff line change
    @@ -14,7 +14,7 @@ public function boot(): void
    {
    $this->registerPolicies();

    \Auth::extend('api_token', static function (Application $app) {
    \Auth::extend('any_token', static function (Application $app) {
    $userProvider = \Auth::createUserProvider('users');
    $request = $app->make('request');
    $tokenGuard = new TokenGuard($userProvider, $request);
  2. tobiashm created this gist Jun 13, 2019.
    26 changes: 26 additions & 0 deletions AuthServiceProvider.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,26 @@
    <?php

    namespace App\Providers;

    use App\Auth\CompositeGuard;
    use Illuminate\Auth\TokenGuard;
    use Illuminate\Foundation\Application;
    use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
    use MiladRahimi\LaraJwt\Guards\Jwt as JwtGuard;

    class AuthServiceProvider extends ServiceProvider
    {
    public function boot(): void
    {
    $this->registerPolicies();

    \Auth::extend('api_token', static function (Application $app) {
    $userProvider = \Auth::createUserProvider('users');
    $request = $app->make('request');
    $tokenGuard = new TokenGuard($userProvider, $request);
    $jwtGuard = new JwtGuard($userProvider, $request);

    return new CompositeGuard($tokenGuard, $jwtGuard);
    });
    }
    }
    54 changes: 54 additions & 0 deletions CompositeGuard.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,54 @@
    <?php

    namespace App\Auth;

    use Illuminate\Auth\GuardHelpers;
    use Illuminate\Contracts\Auth\Authenticatable;
    use Illuminate\Contracts\Auth\Guard;
    use Illuminate\Support\Arr;

    class CompositeGuard implements Guard
    {
    use GuardHelpers;

    /**
    * @var Guard[]
    */
    private $guards;

    public function __construct(Guard ...$guards)
    {
    $this->guards = $guards;
    }

    /**
    * Get the currently authenticated user.
    */
    public function user(): ?Authenticatable
    {
    $guard = $this->find(static function(Guard $guard) {
    return $guard->user() !== null;
    });

    return $guard ? $guard->user() : null;
    }

    /**
    * Validate a user's credentials.
    *
    * @param array $credentials
    */
    public function validate(array $credentials = []): bool
    {
    $guard = $this->find(static function (Guard $guard) use ($credentials) {
    return $guard->validate($credentials);
    });

    return $guard !== null;
    }

    private function find(callable $callable): ?Guard
    {
    return Arr::first($this->guards, $callable);
    }
    }