|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Http\Controllers\Auth; |
|
4
|
|
|
|
|
5
|
|
|
use App\Http\Controllers\Controller; |
|
6
|
|
|
use App\OAuthIdentity; |
|
7
|
|
|
use App\User; |
|
8
|
|
|
use Chrisbjr\ApiGuard\Models\ApiKey; |
|
9
|
|
|
use Exception; |
|
10
|
|
|
use Illuminate\Http\Response; |
|
11
|
|
|
use Illuminate\Support\Facades\Auth; |
|
12
|
|
|
use Illuminate\Support\Facades\Config; |
|
13
|
|
|
use Illuminate\Support\Facades\Redirect; |
|
14
|
|
|
use Laravel\Socialite\Facades\Socialite; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Class SocialAuthController. |
|
18
|
|
|
*/ |
|
19
|
|
|
class SocialAuthController extends Controller |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* Redirect the user to the Provider authentication page. |
|
23
|
|
|
* |
|
24
|
|
|
* @return Response |
|
25
|
|
|
*/ |
|
26
|
|
|
public function redirectToAuthenticationServiceProvider($provider) |
|
27
|
|
|
{ |
|
28
|
|
|
return Socialite::driver($provider)->redirect(); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Obtain the user information from authentication service provider. |
|
33
|
|
|
* |
|
34
|
|
|
* @return Response |
|
35
|
|
|
*/ |
|
36
|
|
|
public function handleAuthenticationServiceProviderCallback($provider) |
|
37
|
|
|
{ |
|
38
|
|
|
try { |
|
39
|
|
|
$user = Socialite::driver($provider)->user(); |
|
40
|
|
|
} catch (Exception $e) { |
|
41
|
|
|
return Redirect::to('auth/'.$provider); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
$authUser = $this->findOrCreateUser($user, $provider); |
|
45
|
|
|
Auth::login($authUser, true); |
|
46
|
|
|
|
|
47
|
|
|
return Redirect::to('upload'); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @param $providerUser |
|
52
|
|
|
* @param $provider |
|
53
|
|
|
* |
|
54
|
|
|
* @return bool|mixed |
|
55
|
|
|
*/ |
|
56
|
|
|
private function findOrCreateUser($providerUser, $provider) |
|
57
|
|
|
{ |
|
58
|
|
|
if ($authUser = $this->userExistsByProviderUserId($providerUser)) { |
|
59
|
|
|
return $authUser; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
return $this->createUser($providerUser, $provider); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @param $providerUser |
|
67
|
|
|
* @param $provider |
|
68
|
|
|
* |
|
69
|
|
|
* @return User |
|
70
|
|
|
*/ |
|
71
|
|
|
private function createUser($providerUser, $provider) |
|
72
|
|
|
{ |
|
73
|
|
|
$user = $providerUser; |
|
74
|
|
|
$userExsists = $this->userExistsByEmail($providerUser); |
|
75
|
|
|
|
|
76
|
|
|
if (!$userExsists) { |
|
77
|
|
|
$user = $this->newUser(); |
|
78
|
|
|
foreach (['name', 'email', 'avatar'] as $item) { |
|
79
|
|
|
$user->$item = $providerUser->$item; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
$user->save(); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
$oAuthIdentity = new OAuthIdentity(); |
|
86
|
|
|
$oAuthIdentity->provider_user_id = $providerUser->getId(); |
|
|
|
|
|
|
87
|
|
|
$oAuthIdentity->provider = $provider; |
|
|
|
|
|
|
88
|
|
|
$oAuthIdentity->access_token = $providerUser->token; |
|
|
|
|
|
|
89
|
|
|
$oAuthIdentity->user_id = $user->id; |
|
|
|
|
|
|
90
|
|
|
$oAuthIdentity->avatar = $providerUser->getAvatar(); |
|
|
|
|
|
|
91
|
|
|
$oAuthIdentity->name = $providerUser->getName(); |
|
|
|
|
|
|
92
|
|
|
$oAuthIdentity->nickname = $providerUser->getNickname(); |
|
|
|
|
|
|
93
|
|
|
$this->createUserApiKey($user); |
|
94
|
|
|
$oAuthIdentity->save(); |
|
95
|
|
|
|
|
96
|
|
|
return $user; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @return mixed |
|
101
|
|
|
*/ |
|
102
|
|
|
private function newUser() |
|
103
|
|
|
{ |
|
104
|
|
|
$user_model = Config::get('laraveltube-socialite.model'); |
|
105
|
|
|
|
|
106
|
|
|
return new $user_model(); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @param $providerUser |
|
111
|
|
|
* |
|
112
|
|
|
* @return bool|mixed |
|
113
|
|
|
*/ |
|
114
|
|
|
private function userExistsByProviderUserId($providerUser) |
|
115
|
|
|
{ |
|
116
|
|
|
/** @var OAuthIdentity $provUser */ |
|
117
|
|
|
if ($provUser = OAuthIdentity::where('provider_user_id', $providerUser->id)->first()) { |
|
118
|
|
|
return $provUser->user; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
return false; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* @param $providerUser |
|
126
|
|
|
* |
|
127
|
|
|
* @return bool |
|
128
|
|
|
*/ |
|
129
|
|
|
private function userExistsByEmail($providerUser) |
|
130
|
|
|
{ |
|
131
|
|
|
if ($user = User::where('email', $providerUser->email)->first()) { |
|
132
|
|
|
return $user; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
return false; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* @param User $user |
|
140
|
|
|
* |
|
141
|
|
|
* @return mixed |
|
142
|
|
|
*/ |
|
143
|
|
|
private function createUserApiKey(User $user) |
|
144
|
|
|
{ |
|
145
|
|
|
$apiKey = ApiKey::make($user->id); |
|
146
|
|
|
$user->apiKey()->save($apiKey); |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
|
Since your code implements the magic setter
_set, this function will be called for any write access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.