|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SMartins\PassportMultiauth\Providers; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\Event; |
|
6
|
|
|
use Illuminate\Support\ServiceProvider; |
|
7
|
|
|
use Laravel\Passport\Events\AccessTokenCreated; |
|
8
|
|
|
use SMartins\PassportMultiauth\ProviderRepository; |
|
9
|
|
|
|
|
10
|
|
|
class MultiauthServiceProvider extends ServiceProvider |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* Bootstrap any application services. |
|
14
|
|
|
* |
|
15
|
|
|
* @return void |
|
16
|
|
|
*/ |
|
17
|
38 |
|
public function boot(ProviderRepository $providers) |
|
18
|
|
|
{ |
|
19
|
38 |
|
if ($this->app->runningInConsole()) { |
|
20
|
38 |
|
$this->registerMigrations(); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
38 |
|
$this->createAccessTokenProvider($providers); |
|
24
|
|
|
|
|
25
|
|
|
// Register the middleware as singleton to use the same middleware |
|
26
|
|
|
// instance when the handle and terminate methods are called. |
|
27
|
38 |
|
$this->app->singleton(\SMartins\PassportMultiauth\Http\Middleware\AddCustomProvider::class); |
|
28
|
38 |
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Register migrations to work on `php artisan migrate` command. |
|
32
|
|
|
* |
|
33
|
|
|
* @return void |
|
34
|
|
|
*/ |
|
35
|
38 |
|
protected function registerMigrations() |
|
36
|
|
|
{ |
|
37
|
38 |
|
$migrationsPath = __DIR__.'/../../database/migrations'; |
|
38
|
|
|
|
|
39
|
38 |
|
$this->loadMigrationsFrom($migrationsPath); |
|
40
|
|
|
|
|
41
|
38 |
|
$this->publishes( |
|
42
|
38 |
|
[$migrationsPath => database_path('migrations')], |
|
43
|
38 |
|
'migrations' |
|
44
|
|
|
); |
|
45
|
38 |
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Create access token provider when access token is created. |
|
49
|
|
|
* |
|
50
|
|
|
* @param ProviderRepository $repository |
|
51
|
|
|
* @return void |
|
52
|
|
|
*/ |
|
53
|
38 |
|
protected function createAccessTokenProvider(ProviderRepository $repository) |
|
54
|
|
|
{ |
|
55
|
|
|
Event::listen(AccessTokenCreated::class, function ($event) use ($repository) { |
|
56
|
4 |
|
$provider = config('auth.guards.api.provider'); |
|
57
|
|
|
|
|
58
|
4 |
|
$repository->create($event->tokenId, $provider); |
|
59
|
38 |
|
}); |
|
60
|
38 |
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|