1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace STS\SocialiteAuth; |
4
|
|
|
|
5
|
|
|
use Illuminate\Auth\SessionGuard; |
6
|
|
|
use Illuminate\Support\ServiceProvider; |
7
|
|
|
use Illuminate\Support\Facades\Auth; |
8
|
|
|
use Illuminate\Http\Request; |
9
|
|
|
use Illuminate\Support\Facades\Config; |
10
|
|
|
|
11
|
|
|
class SocialiteAuthServiceProvider extends ServiceProvider |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Bootstrap the application services. |
15
|
|
|
* @param \Illuminate\Http\Request $request |
16
|
|
|
*/ |
17
|
|
|
public function boot(Request $request) |
18
|
|
|
{ |
19
|
|
|
$this->loadRoutesFrom(__DIR__ .'/../routes/web.php'); |
20
|
|
|
$this->publishAssetsIfConsole(); |
21
|
|
|
|
22
|
|
|
if(config('socialite-auth.match') == false) { |
23
|
|
|
config(['socialite-auth.provider' => 'null']); |
24
|
|
|
}; |
25
|
|
|
|
26
|
|
|
$this->registerSocialiteGuardMixin(); |
27
|
|
|
$this->registerSocialiteAuthDriver(); |
28
|
|
|
$this->registerNullUserProvider(); |
29
|
|
|
$this->addGuardToConfig(); |
30
|
|
|
$this->configureRedirect(); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Register the application services. |
35
|
|
|
*/ |
36
|
|
|
public function register() |
37
|
|
|
{ |
38
|
|
|
// Automatically apply the package configuration |
39
|
|
|
$this->mergeConfigFrom(__DIR__.'/../config/config.php', 'socialite-auth'); |
40
|
|
|
|
41
|
|
|
// Register the main class to use with the facade |
42
|
|
|
$this->app->singleton('socialite-auth', function () { |
43
|
|
|
return new SocialiteAuth(config('socialite-auth')); |
44
|
|
|
}); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
protected function publishAssetsIfConsole() |
48
|
|
|
{ |
49
|
|
|
if ($this->app->runningInConsole()) { |
50
|
|
|
$this->publishes([ |
51
|
|
|
__DIR__.'/../config/config.php' => config_path('socialite-auth.php'), |
52
|
|
|
], 'config'); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
protected function registerSocialiteGuardMixin() |
57
|
|
|
{ |
58
|
|
|
SessionGuard::mixin(new GuardHelpers()); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
protected function registerSocialiteAuthDriver() |
62
|
|
|
{ |
63
|
|
|
Auth::extend('socialite', function ($app, $name, array $config) { |
64
|
|
|
return Auth::createSessionDriver($name, $config); |
65
|
|
|
}); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
protected function addGuardToConfig() |
69
|
|
|
{ |
70
|
|
|
if (!Config::has('auth.guards.socialite')) { |
71
|
|
|
Config::set('auth.guards.socialite', [ |
72
|
|
|
'driver' => 'socialite', |
73
|
|
|
'provider' => config('socialite-auth.provider') |
74
|
|
|
]); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
protected function registerNullUserProvider() |
79
|
|
|
{ |
80
|
|
|
Auth::provider('null', function($app, $config) { |
|
|
|
|
81
|
|
|
return new NullUserProvider(); |
82
|
|
|
}); |
83
|
|
|
|
84
|
|
|
Config::set('auth.providers.null', ['driver' => 'null']); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* If a redirect URL hasn't been configured, we deduce from the current request |
89
|
|
|
*/ |
90
|
|
|
protected function configureRedirect() |
91
|
|
|
{ |
92
|
|
|
$providerName = config('socialite-auth.driver'); |
93
|
|
|
|
94
|
|
|
if(!config("services.$providerName.redirect")) { |
95
|
|
|
config(["services.$providerName.redirect" => $this->app['request']->root() . "/socialite-auth/callback"]); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.