Skip to content

Instantly share code, notes, and snippets.

@Ahed91
Created July 25, 2018 07:27
Show Gist options
  • Save Ahed91/ac9ed01bdec01a4c1ff613e5c021119b to your computer and use it in GitHub Desktop.
Save Ahed91/ac9ed01bdec01a4c1ff613e5c021119b to your computer and use it in GitHub Desktop.
multi-api example with laravel
<?php
public function boot()
{
Auth::viaRequest('multip-api', function ($request) {
$authorization = $request->header('Authorization');
$role = $request->header('Auth-Role');
if (!in_array($role, ['client', 'employee', 'admin', 'user'])) {
return null;
}
$type = substr($authorization, 0, 5);
if ($type == 'token') {
$token = substr($authorization, 6);
} else {
return null;
}
if ($token) {
$role = 'App\Models\\' . ucfirst($role);
return $role::where('api_token', $token)->first();
}
});
}
<?php
'guards' => [
'api' => [
'driver' => 'multip-api', // this line
'provider' => 'users',
],
],
http --json get http://mysite.local/api/user 'Accept:application/json' Accept-Language:ar Auth-Role:client 'Authorization:token fedb2dbf95a00ec0cd84085ae78ea6d6713da43304a7aa80421c2b28a81ef6bb1844f076c7aaed5e2b8a01852b3ed4a3f635fe58a6401712617d283a586c5b97'
<?php
// example route
Route::middleware('auth:api')->get('/user', function (Request $request) {
return \Auth::user();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment