-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(middleware): add check admin middleware
- Loading branch information
Showing
2 changed files
with
36 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace App\Http\Middleware; | ||
|
||
use App\Http\Trait\ApiResponseTrait; | ||
use Closure; | ||
use Illuminate\Http\Request; | ||
use Laravel\Sanctum\PersonalAccessToken; | ||
use Symfony\Component\HttpFoundation\Response; | ||
|
||
class CheckAdminMiddleware | ||
{ | ||
use ApiResponseTrait; | ||
/** | ||
* Handle an incoming request. | ||
* | ||
* @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next | ||
*/ | ||
public function handle(Request $request, Closure $next): Response | ||
{ | ||
$authHeader = $request->header('authorization'); | ||
$token = $authHeader && strlen($authHeader) > 7 ? substr($authHeader, 7) : null; | ||
$personalAccessToken = $token ? PersonalAccessToken::findToken($token) : null; | ||
$user = $personalAccessToken?->tokenable; | ||
|
||
if ($user and $user?->getRoleNames()->toArray()[0] === "admin") { | ||
return $next($request); | ||
}else { | ||
return $this->fail(); | ||
} | ||
} | ||
} |
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