Skip to content

Instantly share code, notes, and snippets.

@tobyokeke
Created April 19, 2020 19:42
Show Gist options
  • Save tobyokeke/bf8f7c27dd7a1e93da5969735080d2c4 to your computer and use it in GitHub Desktop.
Save tobyokeke/bf8f7c27dd7a1e93da5969735080d2c4 to your computer and use it in GitHub Desktop.
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Auth\AuthenticationException;
class CheckForAllScopes
{
/**
* Handle the incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @param mixed ...$scopes
* @return \Illuminate\Http\Response
*
* @throws \Illuminate\Auth\AuthenticationException|\Laravel\Passport\Exceptions\MissingScopeException
*/
public function handle($request, $next, ...$scopes)
{
if (! $request->user() || ! $request->user()->token()) {
throw new AuthenticationException;
}
foreach ($scopes as $scope) {
if ($request->user()->tokenCan($scope)) {
return $next($request);
}
}
return response( array( "message" => "Not Authorized." ), 403 );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment