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