Created
April 19, 2020 19:42
-
-
Save tobyokeke/bf8f7c27dd7a1e93da5969735080d2c4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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