|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace WWON\JwtGuard\Providers; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\Auth; |
|
6
|
|
|
use Illuminate\Support\ServiceProvider; |
|
7
|
|
|
use WWON\JwtGuard\Contract\TokenManager as TokenManagerContract; |
|
8
|
|
|
use WWON\JwtGuard\JwtGuard; |
|
9
|
|
|
use WWON\JwtGuard\JwtService; |
|
10
|
|
|
use WWON\JwtGuard\TokenManager; |
|
11
|
|
|
|
|
12
|
|
|
class JwtGuardServiceProvider extends ServiceProvider |
|
13
|
|
|
{ |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Indicates if loading of the provider is deferred. |
|
17
|
|
|
* |
|
18
|
|
|
* @var bool |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $defer = false; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Boot the service provider. |
|
24
|
|
|
*/ |
|
25
|
|
|
public function boot() |
|
26
|
|
|
{ |
|
27
|
|
|
Auth::extend('jwt', function($app, $name, array $config) { |
|
28
|
|
|
return new JwtGuard( |
|
29
|
|
|
$app['auth']->createUserProvider($config['provider']), |
|
30
|
|
|
$app[JwtService::class], |
|
31
|
|
|
$app['request'] |
|
32
|
|
|
); |
|
33
|
|
|
}); |
|
34
|
|
|
|
|
35
|
|
|
$this->publishConfig(); |
|
36
|
|
|
$this->publishMigration(); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Publish the configuration file. |
|
41
|
|
|
*/ |
|
42
|
|
|
private function publishConfig() |
|
43
|
|
|
{ |
|
44
|
|
|
$configFile = __DIR__ . '/../config/jwt.php'; |
|
45
|
|
|
|
|
46
|
|
|
$this->publishes([ |
|
47
|
|
|
$configFile => config_path('jwt.php') |
|
48
|
|
|
], 'config'); |
|
49
|
|
|
|
|
50
|
|
|
$this->mergeConfigFrom($configFile, 'jwt'); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Publish the migration file. |
|
55
|
|
|
*/ |
|
56
|
|
|
private function publishMigration() |
|
57
|
|
|
{ |
|
58
|
|
|
$this->publishes([ |
|
59
|
|
|
__DIR__ . '/../database/migrations/' => database_path('migrations') |
|
60
|
|
|
], 'migrations'); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Register any application services. |
|
65
|
|
|
*/ |
|
66
|
|
|
public function register() |
|
67
|
|
|
{ |
|
68
|
|
|
$this->app->bind(TokenManagerContract::class, TokenManager::class); |
|
69
|
|
|
|
|
70
|
|
|
$this->app->rebinding('request', function ($app, $request) { |
|
71
|
|
|
$request->setUserResolver(function ($guard = null) { |
|
72
|
|
|
return auth()->guard($guard)->user(); |
|
73
|
|
|
}); |
|
74
|
|
|
}); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
} |