Laravel Authentication: Adding Custom Guards

<?php

namespace App\Providers;

use App\Services\Auth\JwtGuard;
use Illuminate\Support\Facades\Auth;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;

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

        Auth::extend('jwt', function ($app, $name, array $config) {
            // Return an instance of Illuminate\Contracts\Auth\Guard...

            return new JwtGuard(Auth::createUserProvider($config['provider']));
        });
    }
}

Auth.php

'guards' => [
    'api' => [
        'driver' => 'jwt',
        'provider' => 'users',
    ],
],
'guards' => [
    'api' => [
        'driver' => 'custom-token',
    ],
],
'providers' => [
    'users' => [
        'driver' => 'riak',
    ],
],
'guards' => [
    'web' => [
        'driver' => 'session',
        'provider' => 'users',
    ],
],

Closure Request Guards

Use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

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

    Auth::viaRequest('custom-token', function ($request) {
        return User::where('token', $request->token)->first();
    });
}

Laravel Authentication: Adding Custom Guards — Structure map

Clickable & Draggable!

Laravel Authentication: Adding Custom Guards — Related pages: