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
|
|
|
$this->registerSocialiteGuardMixin(); |
23
|
|
|
$this->registerSocialiteAuthDriver(); |
24
|
|
|
$this->addGuardToConfig(); |
25
|
|
|
$this->configureRedirect(); |
26
|
|
|
|
27
|
|
|
if(config('socialite-auth.match') == false) { |
28
|
|
|
config(['socialite-auth.provider' => 'null']); |
29
|
|
|
}; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Register the application services. |
34
|
|
|
*/ |
35
|
|
|
public function register() |
36
|
|
|
{ |
37
|
|
|
// Automatically apply the package configuration |
38
|
|
|
$this->mergeConfigFrom(__DIR__.'/../config/config.php', 'socialite-auth'); |
39
|
|
|
|
40
|
|
|
// Register the main class to use with the facade |
41
|
|
|
$this->app->singleton('socialite-auth', function () { |
42
|
|
|
return new SocialiteAuth(config('socialite-auth')); |
43
|
|
|
}); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
protected function publishAssetsIfConsole() |
47
|
|
|
{ |
48
|
|
|
if ($this->app->runningInConsole()) { |
49
|
|
|
$this->publishes([ |
50
|
|
|
__DIR__.'/../config/config.php' => config_path('socialite-auth.php'), |
51
|
|
|
], 'config'); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
protected function registerSocialiteGuardMixin() |
56
|
|
|
{ |
57
|
|
|
SessionGuard::mixin(new GuardHelpers()); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
protected function registerSocialiteAuthDriver() |
61
|
|
|
{ |
62
|
|
|
Auth::extend('socialite', function ($app, $name, array $config) { |
63
|
|
|
return Auth::createSessionDriver($name, $config); |
64
|
|
|
}); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
protected function addGuardToConfig() |
68
|
|
|
{ |
69
|
|
|
if (!Config::has('auth.guards.socialite')) { |
70
|
|
|
Config::set('auth.guards.socialite', [ |
71
|
|
|
'driver' => 'socialite', |
72
|
|
|
'provider' => config('socialite-auth.provider') |
73
|
|
|
]); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
protected function addNullUserProvider() |
78
|
|
|
{ |
79
|
|
|
Auth::provider('null', function($app, $config) { |
|
|
|
|
80
|
|
|
return new NullUserProvider(); |
81
|
|
|
}); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* If a redirect URL hasn't been configured, we deduce from the current request |
86
|
|
|
*/ |
87
|
|
|
protected function configureRedirect() |
88
|
|
|
{ |
89
|
|
|
$providerName = config('socialite-auth.driver'); |
90
|
|
|
|
91
|
|
|
if(!config("services.$providerName.redirect")) { |
92
|
|
|
config(["services.$providerName.redirect" => $this->app['request']->root() . "/socialite-auth/callback"]); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.