|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* NOTICE OF LICENSE |
|
5
|
|
|
* |
|
6
|
|
|
* Part of the Rinvex Fort Package. |
|
7
|
|
|
* |
|
8
|
|
|
* This source file is subject to The MIT License (MIT) |
|
9
|
|
|
* that is bundled with this package in the LICENSE file. |
|
10
|
|
|
* |
|
11
|
|
|
* Package: Rinvex Fort Package |
|
12
|
|
|
* License: The MIT License (MIT) |
|
13
|
|
|
* Link: https://rinvex.com |
|
14
|
|
|
*/ |
|
15
|
|
|
|
|
16
|
|
|
namespace Rinvex\Fort\Http\Controllers\Frontend; |
|
17
|
|
|
|
|
18
|
|
|
use Exception; |
|
19
|
|
|
use Illuminate\Http\Request; |
|
20
|
|
|
use Illuminate\Support\Facades\Auth; |
|
21
|
|
|
use Laravel\Socialite\Facades\Socialite; |
|
22
|
|
|
use Rinvex\Fort\Contracts\UserRepositoryContract; |
|
23
|
|
|
|
|
24
|
|
|
class SocialAuthenticationController extends AuthenticationController |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* Redirect to Github for authentication. |
|
28
|
|
|
* |
|
29
|
|
|
* @return \Illuminate\Http\Response |
|
30
|
|
|
*/ |
|
31
|
|
|
public function redirectToGithub() |
|
32
|
|
|
{ |
|
33
|
|
|
return Socialite::driver('github')->redirect(); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Handle Github authentication callback. |
|
38
|
|
|
* |
|
39
|
|
|
* @param \Illuminate\Http\Request $request |
|
40
|
|
|
* @param \Rinvex\Fort\Contracts\UserRepositoryContract $userRepository |
|
41
|
|
|
* |
|
42
|
|
|
* @return \Illuminate\Http\Response |
|
43
|
|
|
*/ |
|
44
|
|
|
public function handleGithubCallback(Request $request, UserRepositoryContract $userRepository) |
|
45
|
|
|
{ |
|
46
|
|
|
try { |
|
47
|
|
|
$githubUser = Socialite::driver('github')->user(); |
|
48
|
|
|
} catch (Exception $e) { |
|
49
|
|
|
return intend([ |
|
50
|
|
|
'intended' => route('rinvex.fort.frontend.auth.social.github'), |
|
51
|
|
|
]); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
$user = app('rinvex.fort.user')->whereHas('socialites', function ($query) use ($githubUser) { |
|
55
|
|
|
$query->where('provider', 'github')->where('provider_uid', $githubUser->id); |
|
56
|
|
|
})->first(); |
|
57
|
|
|
|
|
58
|
|
|
if (! $user) { |
|
59
|
|
|
// Prepare registration data |
|
60
|
|
|
$input = $request->except(['_method', '_token']) + [ |
|
61
|
|
|
'email' => $githubUser->email, |
|
62
|
|
|
'username' => $githubUser->username, |
|
63
|
|
|
'password' => str_random(), |
|
64
|
|
|
'active' => ! config('rinvex.fort.registration.moderated'), |
|
65
|
|
|
]; |
|
66
|
|
|
|
|
67
|
|
|
// Fire the register start event |
|
68
|
|
|
$result = event('rinvex.fort.register.social.start', [$input]); |
|
69
|
|
|
|
|
70
|
|
|
// Create user |
|
71
|
|
|
$user = $userRepository->create($input); |
|
72
|
|
|
|
|
73
|
|
|
// Fire the register success event |
|
74
|
|
|
event('rinvex.fort.register.social.success', [$result]); |
|
75
|
|
|
|
|
76
|
|
|
$user->socialites()->create([ |
|
77
|
|
|
'user_id' => 'github', |
|
78
|
|
|
'provider' => 'github', |
|
79
|
|
|
'provider_uid' => $githubUser->id, |
|
80
|
|
|
]); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
$result = Auth::guard($this->getGuard())->login($user, true); |
|
84
|
|
|
|
|
85
|
|
|
return $this->getLoginResponse($request, $result); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|