1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NukaCode\Users\Http\Controllers; |
4
|
|
|
|
5
|
|
|
use App\Http\Controllers\BaseController; |
6
|
|
|
use App\Models\User; |
7
|
|
|
use Laravel\Socialite\Facades\Socialite; |
8
|
|
|
use NukaCode\Users\Events\UserLoggedIn; |
9
|
|
|
use NukaCode\Users\Events\UserRegistered; |
10
|
|
|
|
11
|
|
|
class SocialAuthController extends BaseController |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var array |
15
|
|
|
*/ |
16
|
|
|
protected $providers; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
protected $driver; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
protected $scopes; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
protected $extras; |
32
|
|
|
|
33
|
|
|
public function __construct() |
34
|
|
|
{ |
35
|
|
|
parent::__construct(); |
36
|
|
|
|
37
|
|
|
$this->providers = collect(config('nukacode-user.providers'))->keyBy('driver'); |
|
|
|
|
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Redirect the user to the social providers auth page. |
42
|
|
|
* |
43
|
|
|
* @param null|string $provider |
44
|
|
|
* |
45
|
|
|
* @return mixed |
46
|
|
|
*/ |
47
|
|
|
public function login($provider = null) |
48
|
|
|
{ |
49
|
|
|
$this->getProviderDetails($provider); |
50
|
|
|
|
51
|
|
|
return Socialite::driver($this->driver) |
52
|
|
|
->scopes($this->scopes) |
53
|
|
|
->with($this->extras) |
54
|
|
|
->redirect(); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Use the returned user to register (if needed) and login. |
59
|
|
|
* |
60
|
|
|
* @param null|string $provider |
61
|
|
|
* |
62
|
|
|
* @return mixed |
63
|
|
|
*/ |
64
|
|
|
public function callback($provider = null) |
65
|
|
|
{ |
66
|
|
|
$this->getProviderDetails($provider); |
67
|
|
|
|
68
|
|
|
$socialUser = Socialite::driver($this->driver)->user(); |
69
|
|
|
$user = User::where('email', $socialUser->getEmail())->first(); |
70
|
|
|
|
71
|
|
|
if (is_null($user)) { |
72
|
|
|
$user = $this->register($socialUser); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
if (! $user->hasProvider($this->driver)) { |
76
|
|
|
$user->addSocial($socialUser, $this->driver); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
auth()->login($user, request('remember', false)); |
80
|
|
|
event(new UserLoggedIn($user)); |
|
|
|
|
81
|
|
|
|
82
|
|
|
return redirect() |
83
|
|
|
->intended(route('home')) |
84
|
|
|
->with('message', 'You have been logged in.'); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Create a new user from a social user. |
89
|
|
|
* |
90
|
|
|
* @param $socialUser |
91
|
|
|
* |
92
|
|
|
* @return mixed |
93
|
|
|
*/ |
94
|
|
|
private function register($socialUser) |
95
|
|
|
{ |
96
|
|
|
$names = explode(' ', $socialUser->getName()); |
97
|
|
|
$username = is_null($socialUser->getNickname()) ? $socialUser->getEmail() : $socialUser->getNickname(); |
98
|
|
|
|
99
|
|
|
$userDetails = [ |
100
|
|
|
'username' => $username, |
101
|
|
|
'email' => $socialUser->getEmail(), |
102
|
|
|
'first_name' => isset($names[0]) ? $names[0] : null, |
103
|
|
|
'last_name' => isset($names[1]) ? $names[1] : null, |
104
|
|
|
'display_name' => $username, |
105
|
|
|
]; |
106
|
|
|
|
107
|
|
|
$user = User::create($userDetails); |
108
|
|
|
$user->assignRole(config('nukacode-user.default')); |
109
|
|
|
$user->addSocial($socialUser, $this->driver); |
110
|
|
|
|
111
|
|
|
event(new UserRegistered($user)); |
|
|
|
|
112
|
|
|
|
113
|
|
|
return $user; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Log the user out. |
118
|
|
|
* |
119
|
|
|
* @return mixed |
120
|
|
|
*/ |
121
|
|
|
public function logout() |
122
|
|
|
{ |
123
|
|
|
auth()->logout(); |
124
|
|
|
|
125
|
|
|
return redirect(route('home')) |
126
|
|
|
->with('message', 'You have been logged out.'); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Find the provider's driver, scopes and extras based on a given provider name. |
131
|
|
|
* |
132
|
|
|
* @param $provider |
133
|
|
|
* |
134
|
|
|
* @throws \Exception |
135
|
|
|
*/ |
136
|
|
|
private function getProviderDetails($provider) |
137
|
|
|
{ |
138
|
|
|
if (empty($this->providers)) { |
139
|
|
|
throw new \Exception('No Providers have been set in nukacode-user config.'); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
$provider = is_null($provider) ? $this->providers->first() : $this->providers->get($provider); |
|
|
|
|
143
|
|
|
|
144
|
|
|
if (is_null($provider['driver'])) { |
145
|
|
|
throw new \InvalidArgumentException('You must set a social driver to use the social authenticating features.'); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
$this->driver = $provider['driver']; |
149
|
|
|
$this->scopes = $provider['scopes']; |
150
|
|
|
$this->extras = $provider['extras']; |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..