I have a requirement where there is an API method (guarded by the well-known package tymon/jwt-auth
) and I need to also be able to access it using the basic session based web
middleware.
I don't want to repeat the route in both api.php
and web.php
even though that would totally work.
I tried adding both to the route but they simply don't work, like: ['auth:api', 'web']
I also tried creating a new middleware with the intention of checking both api
and web
like so:
class CombinedAuth
{
public function handle($request, Closure $next)
{
$web = Auth::guard('web')->user();
if ($web) {
return $next($request);
}
$api = Auth::guard('api')->user();
if ($api) {
return $next($request);
}
if(!$web && !$api) {
throw new AuthorizationException;
}
}
}
and that also doesn't work. The api
middleware works fine but the web
middleware doesn't and it always signs me out and redirects to the login page.
So Is there a neat way of protecting a route with api
and web
middlewares at the same time in Laravel 5.8?
You can use 'auth:api,web'
to check for multiple guards.