You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
<?php
namespace App\HiLib\Providers;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\ServiceProvider;
class AuthServiceProvider extends ServiceProvider
{
public function boot()
{
$this->app['auth']->viaRequest('api', function (Request $request) {
// with token
if ($request->bearerToken()) {
return $this->loginUserWithToken($request);
}
// first party
if ($request->getHost() === $container_name = getenv('CONTAINER_NAME')) {
return $this->loginServiceWithSetUser($container_name);
}
// without token
return null;
});
}
public function loginUserWithToken(Request $request): User
{
$attributes = Http::retry(3, 100)
->withToken($request->bearerToken())
->get(env('USER_URL') . 'auth')
->json();
$attributes = Arr::get($attributes, 'data',[]);
// dd($attributes);
return new User($attributes);
}
public function loginServiceWithSetUser(string $container_name): User
{
Auth::setUser($user = new User([
'name' => $container_name,
'is_service' => true,
]));
return $user;
}
}
|