|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Cortex\Auth\Http\Controllers\Frontarea; |
|
6
|
|
|
|
|
7
|
|
|
use Illuminate\Http\Request; |
|
8
|
|
|
use Illuminate\Auth\Events\Registered; |
|
9
|
|
|
use Laravel\Socialite\Facades\Socialite; |
|
10
|
|
|
use Illuminate\Database\Eloquent\Builder; |
|
11
|
|
|
use Cortex\Foundation\Http\Controllers\AbstractController; |
|
12
|
|
|
|
|
13
|
|
|
class SocialAuthenticationController extends AbstractController |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* Redirect the user to the provider authentication page. |
|
17
|
|
|
* |
|
18
|
|
|
* @param string $provider |
|
19
|
|
|
* |
|
20
|
|
|
* @return \Illuminate\Http\Response |
|
21
|
|
|
*/ |
|
22
|
|
|
public function redirectToProvider(string $provider) |
|
23
|
|
|
{ |
|
24
|
|
|
return Socialite::driver($provider)->redirect(); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Obtain the user information from Provider. |
|
29
|
|
|
* |
|
30
|
|
|
* @param \Illuminate\Http\Request $request |
|
31
|
|
|
* @param string $provider |
|
32
|
|
|
* |
|
33
|
|
|
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse |
|
34
|
|
|
*/ |
|
35
|
|
|
public function handleProviderCallback(Request $request, string $provider) |
|
|
|
|
|
|
36
|
|
|
{ |
|
37
|
|
|
$providerUser = Socialite::driver($provider)->user(); |
|
38
|
|
|
$fullName = explode(' ', $providerUser->name); |
|
39
|
|
|
|
|
40
|
|
|
$attributes = [ |
|
41
|
|
|
'id' => $providerUser->id, |
|
42
|
|
|
'email' => $providerUser->email, |
|
43
|
|
|
'username' => $providerUser->nickname ?? trim(mb_strstr($providerUser->email, '@', true)), |
|
44
|
|
|
'given_name' => current($fullName), |
|
45
|
|
|
'family_name' => end($fullName), |
|
46
|
|
|
]; |
|
47
|
|
|
|
|
48
|
|
|
switch ($provider) { |
|
49
|
|
|
case 'twitter': |
|
50
|
|
|
$attributes['title'] = $providerUser->user['description']; |
|
51
|
|
|
$attributes['profile_picture'] = $providerUser->avatar_original; |
|
52
|
|
|
break; |
|
53
|
|
|
case 'github': |
|
54
|
|
|
$attributes['title'] = $providerUser->user['bio']; |
|
55
|
|
|
$attributes['profile_picture'] = $providerUser->avatar; |
|
56
|
|
|
break; |
|
57
|
|
|
case 'facebook': |
|
58
|
|
|
$attributes['profile_picture'] = $providerUser->avatar_original; |
|
59
|
|
|
break; |
|
60
|
|
|
case 'linkedin': |
|
61
|
|
|
$attributes['title'] = $providerUser->headline; |
|
62
|
|
|
$attributes['profile_picture'] = $providerUser->avatar_original; |
|
63
|
|
|
break; |
|
64
|
|
|
case 'google': |
|
65
|
|
|
$attributes['title'] = $providerUser->tagline; |
|
66
|
|
|
$attributes['profile_picture'] = $providerUser->avatar_original; |
|
67
|
|
|
break; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
if (! ($localUser = $this->getLocalUser($provider, (string) $providerUser->id))) { |
|
71
|
|
|
$localUser = $this->createLocalUser($provider, $attributes); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
// Auto-login registered member |
|
75
|
|
|
auth()->guard($this->getGuard())->login($localUser, true); |
|
|
|
|
|
|
76
|
|
|
|
|
77
|
|
|
return intend([ |
|
78
|
|
|
'intended' => route('frontarea.home'), |
|
79
|
|
|
'with' => ['success' => trans('cortex/auth::messages.auth.login')], |
|
80
|
|
|
]); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Get local user for the given provider. |
|
85
|
|
|
* |
|
86
|
|
|
* @param string $provider |
|
87
|
|
|
* @param string $providerUserId |
|
88
|
|
|
* |
|
89
|
|
|
* @return \Illuminate\Database\Eloquent\Model|null |
|
90
|
|
|
*/ |
|
91
|
|
|
protected function getLocalUser(string $provider, string $providerUserId) |
|
92
|
|
|
{ |
|
93
|
|
|
return app('cortex.auth.member')->whereHas('socialites', function (Builder $builder) use ($provider, $providerUserId) { |
|
94
|
|
|
$builder->where('provider', $provider)->where('provider_uid', $providerUserId); |
|
95
|
|
|
})->first(); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Create local user for the given provider. |
|
100
|
|
|
* |
|
101
|
|
|
* @param string $provider |
|
102
|
|
|
* @param array $attributes |
|
103
|
|
|
* |
|
104
|
|
|
* @return \Illuminate\Database\Eloquent\Model|null |
|
105
|
|
|
*/ |
|
106
|
|
|
protected function createLocalUser(string $provider, array $attributes) |
|
107
|
|
|
{ |
|
108
|
|
|
$localUser = app('cortex.auth.member'); |
|
109
|
|
|
|
|
110
|
|
|
$attributes['password'] = str_random(); |
|
|
|
|
|
|
111
|
|
|
$attributes['email_verified_at'] = now(); |
|
112
|
|
|
$attributes['is_active'] = ! config('cortex.auth.registration.moderated'); |
|
113
|
|
|
|
|
114
|
|
|
$localUser->fill($attributes)->save(); |
|
115
|
|
|
|
|
116
|
|
|
// Fire the register success event |
|
117
|
|
|
event(new Registered($localUser)); |
|
118
|
|
|
|
|
119
|
|
|
$localUser->socialites()->create([ |
|
120
|
|
|
'provider' => $provider, |
|
121
|
|
|
'provider_uid' => $attributes['id'], |
|
122
|
|
|
]); |
|
123
|
|
|
|
|
124
|
|
|
return $localUser; |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.