6

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()); });

patricus
  • 59,488
  • 15
  • 143
  • 145
Rob
  • 10,851
  • 21
  • 69
  • 109
  • And your code is ...? – walther Feb 27 '16 at 09:31
  • There is no code, it's fresh Lumen install, just put `Auth::user()` in the controller. – Rob Feb 27 '16 at 10:42
  • Try this http://www.experts-exchange.com/questions/28928192/Lumen-Authentication-unknown-index-provider.html I'm seriously surprised people need hacks like this... Also the installer is bugged if you try to install alongside laravel. Very dissapointed in that. – walther Feb 27 '16 at 12:34
  • Well that's kind of a deal breaker. Now I'm worried it's just going to be one issue after another. Pretty crazy to just get errors like this out of the box. – Rob Feb 27 '16 at 12:44
  • Just realized in the error that it's looking for a `token` driver so I changed `Auth::viaRequest('api', functi` to `Auth::viaRequest('token', funct` which seems to work. I guess the question remains, how to change the auth driver via `.env` And why on earth would they default it to an error...strange. – Rob Feb 27 '16 at 12:56
  • I have just created a PR that addresses this issue. Should be accepted shortly. – JamesG Mar 10 '16 at 06:13

2 Answers2

8

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.

Shaji Ahmed
  • 91
  • 4
  • 4
  • what if I want to use env variable instead of eloquent, what possibly can be my providers configuration look like??? – user269867 May 26 '17 at 20:43
2

Well I still haven't found out how to change the api request type via .env. But for now switching it to token seems to work.

Changed Auth::viaRequest('api', functi to Auth::viaRequest('token', funct.

Rob
  • 10,851
  • 21
  • 69
  • 109
  • I tried this but did'nt work. http://stackoverflow.com/questions/41697095/lumen-5-3-authentication – Kiren S Jan 18 '17 at 09:46