1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Cortex\Fort\Http\Controllers\Frontarea; |
6
|
|
|
|
7
|
|
|
use Laravel\Socialite\Facades\Socialite; |
8
|
|
|
use Illuminate\Database\Eloquent\Builder; |
9
|
|
|
|
10
|
|
|
class SocialAuthenticationController extends AuthenticationController |
|
|
|
|
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Redirect the user to the provider authentication page. |
14
|
|
|
* |
15
|
|
|
* @param string $provider |
16
|
|
|
* |
17
|
|
|
* @return \Illuminate\Http\Response |
18
|
|
|
*/ |
19
|
|
|
public function redirectToProvider(string $provider) |
20
|
|
|
{ |
21
|
|
|
return Socialite::driver($provider)->redirect(); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Obtain the user information from Provider. |
26
|
|
|
* |
27
|
|
|
* @param string $provider |
28
|
|
|
* |
29
|
|
|
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse |
30
|
|
|
*/ |
31
|
|
|
public function handleProviderCallback(string $provider) |
32
|
|
|
{ |
33
|
|
|
$providerUser = Socialite::driver($provider)->user(); |
34
|
|
|
|
35
|
|
|
$attributes = [ |
36
|
|
|
'id' => $providerUser->id, |
37
|
|
|
'email' => $providerUser->email, |
38
|
|
|
'username' => $providerUser->nickname ?? trim(strstr($providerUser->email, '@', true)), |
39
|
|
|
'first_name' => trim(strstr($providerUser->name, ' ', true)), |
40
|
|
|
'last_name' => trim(strstr($providerUser->name, ' ')), |
41
|
|
|
]; |
42
|
|
|
|
43
|
|
|
switch ($provider) { |
44
|
|
|
case 'twitter': |
|
|
|
|
45
|
|
|
$attributes['bio'] = $providerUser->user['description']; |
46
|
|
|
$attributes['profile_picture'] = $providerUser->avatar_original; |
47
|
|
|
break; |
48
|
|
|
case 'github': |
|
|
|
|
49
|
|
|
$attributes['bio'] = $providerUser->user['bio']; |
50
|
|
|
$attributes['profile_picture'] = $providerUser->avatar; |
51
|
|
|
break; |
52
|
|
|
case 'facebook': |
53
|
|
|
$attributes['profile_picture'] = $providerUser->avatar_original; |
54
|
|
|
break; |
55
|
|
|
case 'linkedin': |
56
|
|
|
$attributes['bio'] = $providerUser->headline; |
57
|
|
|
$attributes['profile_picture'] = $providerUser->avatar_original; |
58
|
|
|
break; |
59
|
|
|
case 'google': |
60
|
|
|
$attributes['bio'] = $providerUser->tagline; |
61
|
|
|
$attributes['profile_picture'] = $providerUser->avatar_original; |
62
|
|
|
break; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
if (! ($localUser = $this->getLocalUser($provider, $providerUser->id))) { |
66
|
|
|
$localUser = $this->createLocalUser($provider, $attributes); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$loginResult = auth()->guard($this->getGuard())->attempt([ |
|
|
|
|
70
|
|
|
'is_active' => $localUser->is_active, |
71
|
|
|
'email' => $localUser->email, |
72
|
|
|
'social' => true, |
73
|
|
|
], true); |
74
|
|
|
|
75
|
|
|
return $this->getLoginResponse(request(), $loginResult); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Get local user for the given provider. |
80
|
|
|
* |
81
|
|
|
* @param string $provider |
82
|
|
|
* @param string $providerUserId |
83
|
|
|
* |
84
|
|
|
* @return \Illuminate\Database\Eloquent\Model|null |
85
|
|
|
*/ |
86
|
|
|
protected function getLocalUser(string $provider, string $providerUserId) |
87
|
|
|
{ |
88
|
|
|
return app('rinvex.fort.user')->whereHas('socialites', function (Builder $builder) use ($provider, $providerUserId) { |
|
|
|
|
89
|
|
|
$builder->where('provider', $provider)->where('provider_uid', $providerUserId); |
90
|
|
|
})->first(); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Create local user for the given provider. |
95
|
|
|
* |
96
|
|
|
* @param string $provider |
97
|
|
|
* @param array $attributes |
98
|
|
|
* |
99
|
|
|
* @return \Illuminate\Database\Eloquent\Model|null |
100
|
|
|
*/ |
101
|
|
|
protected function createLocalUser(string $provider, array $attributes) |
102
|
|
|
{ |
103
|
|
|
$localUser = app('rinvex.fort.user'); |
104
|
|
|
$defaultRole = app('rinvex.fort.role')->where('slug', config('rinvex.fort.registration.default_role'))->first(); |
105
|
|
|
|
106
|
|
|
$attributes['password'] = str_random(); |
107
|
|
|
$attributes['email_verified'] = true; |
108
|
|
|
$attributes['email_verified_at'] = now(); |
109
|
|
|
$attributes['is_active'] = ! config('rinvex.fort.registration.moderated'); |
110
|
|
|
$attributes['roles'] = $defaultRole ? [$defaultRole->id] : null; |
111
|
|
|
|
112
|
|
|
$localUser->fill($attributes)->save(); |
113
|
|
|
|
114
|
|
|
// Fire the register success event |
115
|
|
|
event('rinvex.fort.register.success', [$localUser]); |
116
|
|
|
|
117
|
|
|
$localUser->socialites()->create([ |
118
|
|
|
'provider' => $provider, |
119
|
|
|
'provider_uid' => $attributes['id'], |
120
|
|
|
]); |
121
|
|
|
|
122
|
|
|
return $localUser; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
This check looks for classes that have been defined more than once.
If you can, we would recommend to use standard object-oriented programming techniques. For example, to avoid multiple types, it might make sense to create a common interface, and then multiple, different implementations for that interface.
This also has the side-effect of providing you with better IDE auto-completion, static analysis and also better OPCode caching from PHP.