|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Cortex\Fort\Http\Controllers\Frontarea; |
|
6
|
|
|
|
|
7
|
|
|
use Illuminate\Http\Request; |
|
8
|
|
|
use Laravel\Socialite\Facades\Socialite; |
|
9
|
|
|
use Illuminate\Database\Eloquent\Builder; |
|
10
|
|
|
|
|
11
|
|
|
class SocialAuthenticationController extends AuthenticationController |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* Redirect the user to the provider authentication page. |
|
15
|
|
|
* |
|
16
|
|
|
* @param string $provider |
|
17
|
|
|
* |
|
18
|
|
|
* @return \Illuminate\Http\Response |
|
19
|
|
|
*/ |
|
20
|
|
|
public function redirectToProvider(string $provider) |
|
21
|
|
|
{ |
|
22
|
|
|
return Socialite::driver($provider)->redirect(); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Obtain the user information from Provider. |
|
27
|
|
|
* |
|
28
|
|
|
* @param string $provider |
|
29
|
|
|
* |
|
30
|
|
|
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse |
|
31
|
|
|
*/ |
|
32
|
|
|
public function handleProviderCallback(string $provider) |
|
33
|
|
|
{ |
|
34
|
|
|
$providerUser = Socialite::driver($provider)->user(); |
|
35
|
|
|
dd($providerUser); |
|
36
|
|
|
// @TODO |
|
|
|
|
|
|
37
|
|
|
switch ($provider) { |
|
38
|
|
|
case 'github': |
|
39
|
|
|
$asd = 'asd'; |
|
|
|
|
|
|
40
|
|
|
break; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
$attributes = [ |
|
44
|
|
|
'id' => $providerUser->id, |
|
45
|
|
|
'email' => $providerUser->email, |
|
46
|
|
|
'username' => $providerUser->nickname, |
|
47
|
|
|
'last_token' => $providerUser->token, |
|
48
|
|
|
]; |
|
49
|
|
|
|
|
50
|
|
|
if (! ($localUser = $this->getLocalUser($provider, $providerUser->id))) { |
|
51
|
|
|
$localUser = $this->createLocalUser($provider, $attributes); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
$loginResult = auth()->guard($this->getGuard())->attempt([ |
|
|
|
|
|
|
55
|
|
|
'is_active' => $localUser->is_active, |
|
56
|
|
|
'email' => $localUser->email, |
|
57
|
|
|
'social' => true, |
|
58
|
|
|
], true); |
|
59
|
|
|
|
|
60
|
|
|
return $this->getLoginResponse(request(), $loginResult); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Get local user for the given provider. |
|
65
|
|
|
* |
|
66
|
|
|
* @param string $provider |
|
67
|
|
|
* @param int $providerUserId |
|
68
|
|
|
* |
|
69
|
|
|
* @return \Illuminate\Database\Eloquent\Model|null |
|
70
|
|
|
*/ |
|
71
|
|
|
protected function getLocalUser(string $provider, int $providerUserId) |
|
72
|
|
|
{ |
|
73
|
|
|
return app('rinvex.fort.user')->whereHas('socialites', function (Builder $builder) use ($provider, $providerUserId) { |
|
|
|
|
|
|
74
|
|
|
$builder->where('provider', $provider)->where('provider_uid', $providerUserId); |
|
75
|
|
|
})->first(); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Create local user for the given provider. |
|
80
|
|
|
* |
|
81
|
|
|
* @param string $provider |
|
82
|
|
|
* @param array $attributes |
|
83
|
|
|
* |
|
84
|
|
|
* @return \Illuminate\Database\Eloquent\Model|null |
|
85
|
|
|
*/ |
|
86
|
|
|
protected function createLocalUser(string $provider, array $attributes) |
|
87
|
|
|
{ |
|
88
|
|
|
$defaultRole = app('rinvex.fort.role')->where('slug', config('rinvex.fort.registration.default_role'))->first(); |
|
89
|
|
|
|
|
90
|
|
|
$localUser = app('rinvex.fort.user')->fill([ |
|
91
|
|
|
'password' => str_random(), |
|
92
|
|
|
'username' => $attributes['username'], |
|
93
|
|
|
'email' => $attributes['email'], |
|
94
|
|
|
'email_verified' => true, |
|
95
|
|
|
'email_verified_at' => now(), |
|
96
|
|
|
'is_active' => ! config('rinvex.fort.registration.moderated'), |
|
97
|
|
|
'roles' => $defaultRole ? [$defaultRole->id] : null, |
|
98
|
|
|
])->save(); |
|
99
|
|
|
|
|
100
|
|
|
// Fire the register success event |
|
101
|
|
|
event('rinvex.fort.register.success', [$localUser]); |
|
102
|
|
|
|
|
103
|
|
|
$localUser->socialites()->create([ |
|
104
|
|
|
'provider' => $provider, |
|
105
|
|
|
'provider_uid' => $attributes['id'], |
|
106
|
|
|
'last_token' => $attributes['last_token'], |
|
107
|
|
|
]); |
|
108
|
|
|
|
|
109
|
|
|
return $localUser; |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|
This check looks
TODOcomments that have been left in the code.``TODO``s show that something is left unfinished and should be attended to.