Support the ongoing development of Laravel.io →
Authentication
Last updated 1 year ago.
0

agougousis said:

Does anyone knows how can I use a stateless basic authentication for some of my routes in Laravel 5? In Laravel 4, I could do that by using the ollieread/multiauth package. Since there is some native multi-auth support in Laravel 5, I am trying to make use of it but without success. What I have done is:

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

       'api' => [
           'driver' => 'array',
           'provider' => 'api_users',
       ],
   ],


'providers' => [
       'gui_users' => [
           'driver' => 'eloquent',
           'model' => App\Models\GuiUser::class,
       ],

        'api_users' => [
            'driver' => 'eloquent',
            'model' => App\Models\ApiUser::class,
        ],
   ],

I have also defined a new middleware:

namespace App\Http\Middleware;

use Auth;
use Closure;

class AuthenticateOnceBasic
{
   /**
    * Handle an incoming request.
    *
    * @param  \Illuminate\Http\Request  $request
    * @param  \Closure  $next
    * @return mixed
    */
   public function handle($request, Closure $next)
   {
       return Auth::guard('api')->onceBasic('username') ?: $next($request);
   }

}

I am using this middleware to 'myapi' middleware group:

protected $middlewareGroups = [
       'web' => [
  	            
       ],        
       'myweb' =>  [
  	// middlewares for non-API routes
  	...
  ],
  'myapi' => [
  	// middlewares for API routes
  	\App\Http\Middleware\AuthenticateOnceBasic::class,
  ]
]

which in turn is used for API routes:

Route::group(['prefix'=>'api','middleware' => ['myapi']], function () {
  ...my API routes...
});

When I try to visit one of my API routes, I get an AuthManager exception with the message:

Auth guard driver [api] is not defined.

Hi, that error seems to come from the Illuminate\Auth\AuthManager class, resolve method. Your api guard has array written as a driver in the config file. This causes the error thrown, because the resolve class method from AuthManager class fails.

By default, the laravel supports the session and token driver. You can specify a custom driver but you'll need to create a config file, and the auth manager will resolve that driver by using callCustomCreator method.

Take a look at Illuminate\Auth\AuthManager class for more details.

0

Sign in to participate in this thread!

Eventy

Your banner here too?

agougousis agougousis Joined 20 Jan 2015

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.