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
|
|
|
|
36
|
|
|
$attributes = [ |
37
|
|
|
'id' => $providerUser->id, |
38
|
|
|
'email' => $providerUser->email, |
39
|
|
|
'username' => $providerUser->nickname ?? trim(strstr($providerUser->email, '@', true)), |
40
|
|
|
'first_name' => trim(strstr($providerUser->name, ' ', true)), |
41
|
|
|
'last_name' => trim(strstr($providerUser->name, ' ')), |
42
|
|
|
]; |
43
|
|
|
|
44
|
|
|
//switch ($provider) { |
|
|
|
|
45
|
|
|
// case 'twitter': |
46
|
|
|
// $attributes['bio'] = $providerUser->user['description']; |
|
|
|
|
47
|
|
|
// $attributes['profile_picture'] = $providerUser->avatar_original; |
|
|
|
|
48
|
|
|
// break; |
49
|
|
|
// case 'github': |
50
|
|
|
// $attributes['bio'] = $providerUser->user['bio']; |
|
|
|
|
51
|
|
|
// $attributes['profile_picture'] = $providerUser->avatar; |
|
|
|
|
52
|
|
|
// break; |
53
|
|
|
// case 'facebook': |
54
|
|
|
// $attributes['profile_picture'] = $providerUser->avatar_original; |
|
|
|
|
55
|
|
|
// break; |
56
|
|
|
// case 'linkedin': |
57
|
|
|
// $attributes['bio'] = $providerUser->headline; |
|
|
|
|
58
|
|
|
// $attributes['profile_picture'] = $providerUser->avatar_original; |
|
|
|
|
59
|
|
|
// break; |
60
|
|
|
// case 'google': |
61
|
|
|
// $attributes['bio'] = $providerUser->tagline; |
|
|
|
|
62
|
|
|
// $attributes['profile_picture'] = $providerUser->avatar_original; |
|
|
|
|
63
|
|
|
// break; |
64
|
|
|
//} |
65
|
|
|
|
66
|
|
|
//dd($providerUser, $attributes); |
|
|
|
|
67
|
|
|
|
68
|
|
|
if (! ($localUser = $this->getLocalUser($provider, $providerUser->id))) { |
69
|
|
|
$localUser = $this->createLocalUser($provider, $attributes); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$loginResult = auth()->guard($this->getGuard())->attempt([ |
|
|
|
|
73
|
|
|
'is_active' => $localUser->is_active, |
74
|
|
|
'email' => $localUser->email, |
75
|
|
|
'social' => true, |
76
|
|
|
], true); |
77
|
|
|
|
78
|
|
|
return $this->getLoginResponse(request(), $loginResult); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Get local user for the given provider. |
83
|
|
|
* |
84
|
|
|
* @param string $provider |
85
|
|
|
* @param string $providerUserId |
86
|
|
|
* |
87
|
|
|
* @return \Illuminate\Database\Eloquent\Model|null |
88
|
|
|
*/ |
89
|
|
|
protected function getLocalUser(string $provider, string $providerUserId) |
90
|
|
|
{ |
91
|
|
|
return app('rinvex.fort.user')->whereHas('socialites', function (Builder $builder) use ($provider, $providerUserId) { |
|
|
|
|
92
|
|
|
$builder->where('provider', $provider)->where('provider_uid', $providerUserId); |
93
|
|
|
})->first(); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Create local user for the given provider. |
98
|
|
|
* |
99
|
|
|
* @param string $provider |
100
|
|
|
* @param array $attributes |
101
|
|
|
* |
102
|
|
|
* @return \Illuminate\Database\Eloquent\Model|null |
103
|
|
|
*/ |
104
|
|
|
protected function createLocalUser(string $provider, array $attributes) |
105
|
|
|
{ |
106
|
|
|
$localUser = app('rinvex.fort.user'); |
107
|
|
|
$defaultRole = app('rinvex.fort.role')->where('slug', config('rinvex.fort.registration.default_role'))->first(); |
108
|
|
|
|
109
|
|
|
$attributes['password'] = str_random(); |
110
|
|
|
$attributes['email_verified'] = true; |
111
|
|
|
$attributes['email_verified_at'] = now(); |
112
|
|
|
$attributes['is_active'] = ! config('rinvex.fort.registration.moderated'); |
113
|
|
|
$attributes['roles'] = $defaultRole ? [$defaultRole->id] : null; |
114
|
|
|
|
115
|
|
|
$localUser->fill($attributes)->save(); |
116
|
|
|
|
117
|
|
|
// Fire the register success event |
118
|
|
|
event('rinvex.fort.register.success', [$localUser]); |
119
|
|
|
|
120
|
|
|
$localUser->socialites()->create([ |
121
|
|
|
'provider' => $provider, |
122
|
|
|
'provider_uid' => $attributes['id'], |
123
|
|
|
]); |
124
|
|
|
|
125
|
|
|
return $localUser; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.