vendredi 26 février 2016

Laravel Custom Auth Guard not persisting

I'm trying to set up a custom auth guard and everything is mostly working. I'm able to log the Model in, but once I redirect the visitor to a new page the authentication is lost. I can dd() the Auth::guard('client')->user() just fine before the controller does the redirect, but comes up as null in the AuthenticateClient middleware.

I'm using the default guard for authenticating users and everything is working fine with that. I've made sure the routes are under the web middleware which enables sessions.

I've searched for similar issues, but I'm unable to find a solution that works. Any ideas how to fix this?

Side note: I know that I'm using token in the code examples below, but I'm doing more than just validating against that token. So this is a different system than authenticating a token for an api.

Routes:

Route::group(['middleware' => 'web'], function () {
    // other routes...

    Route::get('client/login/{token}', ['as' => 'client.token', 'uses' => 'Auth\ClientController@attemptTokenLogin']);
    Route::get('client/login', ['as' => 'client.login', 'uses' => 'Auth\ClientController@showLoginForm']);
    Route::post('client/login', 'Auth\ClientController@login');

    Route::group(['prefix' => 'client', 'middleware' => 'auth.client'], function () {
       Route::get('dashboard', ['as' => 'client.dashboard',  'uses' => 'ClientController@dashboard']);
    });
});

auth.php

return [

    'defaults' => [
        'guard' => 'web',
        'passwords' => 'users',
    ],

    'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],

        'api' => [
            'driver' => 'token',
            'provider' => 'users',
        ],

        // new auth guard
        'client' => [
            'driver' => 'session',
            'provider' => 'clients',
        ],
    ],

    'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => App\User::class,
        ],

        // new guard provider
        'clients' => [
            'driver' => 'eloquent',
            'model' => App\Client::class,
        ],
    ],
];

Http/Kernel.php

protected $routeMiddleware = [
    'auth' => \App\Http\Middleware\Authenticate::class,
    'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
    'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
    'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
    // new...
    'auth.client' => \App\Http\Middleware\AuthenticateClient::class,
];

ClientController@attemptTokenLogin

$client = // get the client in a custom way...
Auth::guard('client')->login($client);

// dd(Auth::guard('client')->user()); // this works here

return redirect()->route('client.dashboard');

AuthenticateClient

public function handle($request, Closure $next)
{
    // dd(Auth::guard('client')->user()); // this does not work here

    if (!Auth::guard('client')->check()) {
        return redirect()->route('client.login');
    }

    return $next($request);
}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire