Lumen Authentication

Rob picture Rob · Feb 27, 2016 · Viewed 13.8k times · Source

Just can't get the Lumen authentication to work at all.

I have a fresh install and trying to follow the docs here:

https://lumen.laravel.com/docs/5.2/authentication

I've Uncommented the AuthProvider line in the app.php file (along with everything else, facade, etc). Then in a simple controller I just do dd(Auth::use()).

I just can't get around this error:

Undefined index: provider
in AuthManager.php line 152
at Application->Laravel\Lumen\Concerns\{closure}('8', 'Undefined index: provider', '/home/vagrant/Code/gryd/api.gryd.com/vendor/illuminate/auth/AuthManager.php', '152', array('name' => 'api', 'config' => array('driver' => 'token'))) in AuthManager.php line 152

Any ideas?

EDIT:

Since someone asked for a code sample.

  1. Install Lumen
  2. Uncomment everything in app.php
  3. Put this in routes:

    $app->get('/api/v1/users/{id}', function () { dd(\Auth::user()); });

Answer

Shaji Ahmed picture Shaji Ahmed · Jun 15, 2016

This is what I've got so far, which is working but not quite how I'd like it. The following works for Token-based auth, which is the default setting in Lumen.

Enable Authentication

Register routeMiddleware and AuthServiceProvider by un-commenting the following lines in bootstrap/app.php.

$app->routeMiddleware([
    'auth' => App\Http\Middleware\Authenticate::class,
]);

and

$app->register(App\Providers\AuthServiceProvider::class);

Configuration

Copy vendor/laravel/lumen-framework/config/auth.php to config/auth.php. Create the root config folder if you have to.

Inside we will find four items (defaults, guards, providers, passwords). We're concerned with the first three.

First we name the default guard as ABC.

'defaults' => [
    'guard' => env('AUTH_GUARD', 'ABC'),
],

Next we define the ABC guard with token as its driver and XYZ as its provider.

'guards' => [
    'ABC' => [
        'driver' => 'token', 
        'provider' => 'XYZ'
    ],
],

And the XYZ provider is defined with eloquent as the driver and App\User::class as the model.

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

Completing Setup

Finally, we use the auth middleware in our routing setup, as usual.

$app->group(['middleware' => 'auth'], function () use ($app) {

So this is what gets the token auth up and running. It uses the api_token field in the users table to authenticate, which can be found in TokenGuard.

I still haven't found out what effect AuthServiceProvider and $this->app['auth']->viaRequest('api', function ($request) { have on my app yet.