Created
July 25, 2018 07:27
-
-
Save Ahed91/ac9ed01bdec01a4c1ff613e5c021119b to your computer and use it in GitHub Desktop.
multi-api example with laravel
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 | |
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(); | |
} | |
}); | |
} |
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 | |
'guards' => [ | |
'api' => [ | |
'driver' => 'multip-api', // this line | |
'provider' => 'users', | |
], | |
], |
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
http --json get http://mysite.local/api/user 'Accept:application/json' Accept-Language:ar Auth-Role:client 'Authorization:token fedb2dbf95a00ec0cd84085ae78ea6d6713da43304a7aa80421c2b28a81ef6bb1844f076c7aaed5e2b8a01852b3ed4a3f635fe58a6401712617d283a586c5b97' |
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 | |
// 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