Eddy
Eddy

Reputation: 613

'Illegal offset type' while trying to create my own Authentication on Lumen

I am trying to create my own authentication for my API based on external Oauth2 provider. I am using Lumen as my API backend and I have to secure some of my endpoints for users who don't have a valid access token.

So I started as it is written in docs. I have uncommented $app->register(App\Providers\AuthServiceProvider::class); in bootstrap/app.php

I have created specific login in AuthServiceProvider:

class AuthServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }

    /**
     * Boot the authentication services for the application.
     *
     * @return void
     */
    public function boot()
    {
        // Here you may define how you wish users to be authenticated for your Lumen
        // application. The callback which receives the incoming request instance
        // should return either a User instance or null. You're free to obtain
        // the User instance via an API token or any other method necessary.

        $this->app['auth']->viaRequest('api', function ($request) {
            // my logic
        });
    }
}

And I've also secured my routes in routes/web/php:

$router->group(['prefix' => 'api', 'middleware' => 'auth'], function () use ($router){
    $router->get('grant-access', ['uses' => 'DatabaseController@grantAccess']);
    $router->get('refresh-access', ['uses' => 'DatabaseController@refreshAccess']);
});

I have investigated that in AuthManager there is a method guard() which causes the problem:

 /**
 * Attempt to get the guard from the local cache.
 *
 * @param  string|null  $name
 * @return \Illuminate\Contracts\Auth\Guard|\Illuminate\Contracts\Auth\StatefulGuard
 */
public function guard($name = null)
{
    $name = $name ?: $this->getDefaultDriver();
    return $this->guards[$name] ?? $this->guards[$name] = $this->resolve($name);
}

Beacuse variable $name is an object and object can't be used as keys in PHP arrays.

So my question have I missed something to switch on Authentication in Lumen?

Upvotes: 0

Views: 220

Answers (1)

Eddy
Eddy

Reputation: 613

Okey, It took me few minutes, but I found out that I have to uncomment also this in bootstrap/app.php:

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

I believe that this should be also mentioned in Lumen docs next to that red note.

Upvotes: 1

Related Questions