php - Laravel 5.2: session and token guard on the same routes -
we had session guard , enough.
now need add authorization via token (in headers or params) , via session on same routes.
authorization via token must stateless.
upd: first, think create dubplicate routes. 1 session , 1 token
// api token auth // url: /api/test route::group(['middleware' => ['web', 'auth:api'], 'prefix' => 'api', 'as' => 'api.'], function () { route::resource('test', 'testcontroller'); // 50+ routes }); // session auth // url: /test route::group(['middleware' => ['web', 'auth']], function () { route::resource('test', 'testcontroller'); // 50+ routes });
but it's not want, because urls different.
maybe knows how solve issue?
create new middleware authenticatewithtoken:
class authenticatewithtoken { /** * handle incoming request. * * @param \illuminate\http\request $request * @param \closure $next * @param string|null $guard * * @return mixed */ public function handle($request, closure $next, $guard = null) { if (($user = auth::guard('api')->user())) { auth::setuser($user); } return $next($request); } }
declare in http/kernel.php:
/** * application's route middleware. * * these middleware may assigned groups or used individually. * * @var array */ protected $routemiddleware = [ // ... 'auth.api' => \app\http\middleware\authenticatewithtoken::class, // ... ];
and add before default 'auth' middleware in routes.php:
route::group(['middleware' => ['web', 'auth.api', 'auth']], function () { route::resource('test', 'testcontroller'); // 50+ routes });
Comments
Post a Comment