|
1
|
|
|
<?php |
|
2
|
|
|
namespace Xetaravel\Http\Controllers\Auth; |
|
3
|
|
|
|
|
4
|
|
|
use Exception; |
|
5
|
|
|
use Illuminate\Http\Request; |
|
6
|
|
|
use Illuminate\Foundation\Auth\RedirectsUsers; |
|
7
|
|
|
use Illuminate\Support\Facades\Auth; |
|
8
|
|
|
use Laravel\Socialite\Facades\Socialite; |
|
9
|
|
|
use Laravel\Socialite\Two\User as ProviderUser; |
|
10
|
|
|
use Xetaravel\Events\RegisterEvent; |
|
11
|
|
|
use Xetaravel\Http\Controllers\Controller; |
|
12
|
|
|
use Xetaravel\Models\User; |
|
13
|
|
|
use Xetaravel\Models\Repositories\UserRepository; |
|
14
|
|
|
use Xetaravel\Models\Role; |
|
15
|
|
|
|
|
16
|
|
|
class SocialiteController extends Controller |
|
17
|
|
|
{ |
|
18
|
|
|
use RedirectsUsers; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Where to redirect users after login. |
|
22
|
|
|
* |
|
23
|
|
|
* @var string |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $redirectTo = '/'; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Redirect the user to the GitHub authentication page. |
|
29
|
|
|
* |
|
30
|
|
|
* @return Response |
|
31
|
|
|
*/ |
|
32
|
|
|
public function redirectToProvider() |
|
33
|
|
|
{ |
|
34
|
|
|
return Socialite::driver('github')->redirect(); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Obtain the user information from GitHub. |
|
39
|
|
|
* |
|
40
|
|
|
* @return Response |
|
|
|
|
|
|
41
|
|
|
*/ |
|
42
|
|
|
public function handleProviderCallback(Request $request) |
|
43
|
|
|
{ |
|
44
|
|
|
try { |
|
45
|
|
|
$user = Socialite::driver('github')->user(); |
|
46
|
|
|
} catch (Exception $e) { |
|
47
|
|
|
return redirect() |
|
48
|
|
|
->route('users.auth.login') |
|
49
|
|
|
->with('danger', 'An error occurred while getting your information from GitHub !'); |
|
50
|
|
|
} |
|
51
|
|
|
$user = $this->findOrCreateUser($user); |
|
52
|
|
|
|
|
53
|
|
|
Auth::login($user, true); |
|
54
|
|
|
|
|
55
|
|
|
return $this->authenticated($request, $user) ?: redirect()->intended($this->redirectPath()); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* The user has been authenticated. |
|
60
|
|
|
* |
|
61
|
|
|
* @param \Illuminate\Http\Request $request The request object. |
|
62
|
|
|
* @param \Xetaravel\Models\User $user The user that has been logged in. |
|
63
|
|
|
* |
|
64
|
|
|
* @return void |
|
65
|
|
|
*/ |
|
66
|
|
View Code Duplication |
protected function authenticated(Request $request, $user) |
|
|
|
|
|
|
67
|
|
|
{ |
|
68
|
|
|
event(new RegisterEvent($user)); |
|
69
|
|
|
|
|
70
|
|
|
$request->session()->flash( |
|
|
|
|
|
|
71
|
|
|
'success', |
|
72
|
|
|
'Welcome back <strong>' . e($user->username) . '</strong>! You\'re successfully connected !' |
|
73
|
|
|
); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Find the user if he exists or create it. |
|
78
|
|
|
* |
|
79
|
|
|
* @param \Laravel\Socialite\Two\User $providerUser |
|
80
|
|
|
* |
|
81
|
|
|
* @return \Xetaravel\Models\User |
|
82
|
|
|
*/ |
|
83
|
|
|
protected function findOrCreateUser(ProviderUser $providerUser): User |
|
84
|
|
|
{ |
|
85
|
|
|
if ($user = User::where('github_id', $providerUser->id)->first()) { |
|
86
|
|
|
return $user; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
$user = UserRepository::create( |
|
90
|
|
|
[ |
|
91
|
|
|
'username' => $providerUser->nickname, |
|
92
|
|
|
'email' => $providerUser->email |
|
93
|
|
|
], |
|
94
|
|
|
[ |
|
95
|
|
|
'github_id' => $providerUser->id |
|
96
|
|
|
], |
|
97
|
|
|
true |
|
98
|
|
|
); |
|
99
|
|
|
|
|
100
|
|
|
$this->registered($user); |
|
101
|
|
|
|
|
102
|
|
|
return $user; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* The user has been registered. |
|
107
|
|
|
* |
|
108
|
|
|
* @param \Xetaravel\Models\User $user The user that has been registered. |
|
109
|
|
|
* |
|
110
|
|
|
* @return void |
|
111
|
|
|
*/ |
|
112
|
|
View Code Duplication |
protected function registered(User $user) |
|
|
|
|
|
|
113
|
|
|
{ |
|
114
|
|
|
$role = Role::where('slug', 'user')->first(); |
|
115
|
|
|
$user->attachRole($role); |
|
116
|
|
|
|
|
117
|
|
|
$user->clearMediaCollection('avatar'); |
|
118
|
|
|
$user->addMedia(resource_path('assets/images/avatar.png')) |
|
119
|
|
|
->preservingOriginal() |
|
120
|
|
|
->setName(substr(md5($user->username), 0, 10)) |
|
121
|
|
|
->setFileName(substr(md5($user->username), 0, 10) . '.png') |
|
122
|
|
|
->toMediaCollection('avatar'); |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.