1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Controllers\Auth; |
4
|
|
|
|
5
|
|
|
use App\Http\Controllers\Controller; |
6
|
|
|
use App\Models\Profile; |
7
|
|
|
use App\Models\Social; |
8
|
|
|
use App\Models\User; |
9
|
|
|
use App\Traits\ActivationTrait; |
10
|
|
|
use App\Traits\CaptureIpTrait; |
11
|
|
|
use Illuminate\Support\Facades\Config; |
12
|
|
|
use Illuminate\Support\Facades\Input; |
13
|
|
|
use jeremykenedy\LaravelRoles\Models\Role; |
14
|
|
|
use Laravel\Socialite\Facades\Socialite; |
15
|
|
|
|
16
|
|
|
class SocialController extends Controller |
17
|
|
|
{ |
18
|
|
|
use ActivationTrait; |
|
|
|
|
19
|
|
|
|
20
|
|
|
public function getSocialRedirect($provider) |
21
|
|
|
{ |
22
|
|
|
$providerKey = Config::get('services.'.$provider); |
23
|
|
|
|
24
|
|
|
if (empty($providerKey)) { |
25
|
|
|
return view('pages.status') |
26
|
|
|
->with('error', trans('socials.noProvider')); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
return Socialite::driver($provider)->redirect(); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function getSocialHandle($provider) |
33
|
|
|
{ |
34
|
|
|
if (Input::get('denied') != '') { |
35
|
|
|
return redirect()->to('login') |
36
|
|
|
->with('status', 'danger') |
37
|
|
|
->with('message', trans('socials.denied')); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
$socialUserObject = Socialite::driver($provider)->user(); |
41
|
|
|
|
42
|
|
|
$socialUser = null; |
43
|
|
|
|
44
|
|
|
// Check if email is already registered |
45
|
|
|
$userCheck = User::where('email', '=', $socialUserObject->email)->first(); |
|
|
|
|
46
|
|
|
|
47
|
|
|
$email = $socialUserObject->email; |
48
|
|
|
|
49
|
|
|
if (!$socialUserObject->email) { |
50
|
|
|
$email = 'missing'.str_random(10); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
if (empty($userCheck)) { |
54
|
|
|
$sameSocialId = Social::where('social_id', '=', $socialUserObject->id) |
|
|
|
|
55
|
|
|
->where('provider', '=', $provider) |
56
|
|
|
->first(); |
57
|
|
|
|
58
|
|
|
if (empty($sameSocialId)) { |
59
|
|
|
$ipAddress = new CaptureIpTrait(); |
60
|
|
|
$socialData = new Social(); |
61
|
|
|
$profile = new Profile(); |
62
|
|
|
$role = Role::where('name', '=', 'user')->first(); |
63
|
|
|
$fullname = explode(' ', $socialUserObject->name); |
|
|
|
|
64
|
|
|
if (count($fullname) == 1) { |
65
|
|
|
$fullname[1] = ''; |
66
|
|
|
} |
67
|
|
|
$username = $socialUserObject->nickname; |
|
|
|
|
68
|
|
|
|
69
|
|
|
if ($username == null) { |
70
|
|
|
foreach ($fullname as $name) { |
71
|
|
|
$username .= $name; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$user = User::create([ |
76
|
|
|
'name' => $username, |
77
|
|
|
'first_name' => $fullname[0], |
78
|
|
|
'last_name' => $fullname[1], |
79
|
|
|
'email' => $email, |
80
|
|
|
'password' => bcrypt(str_random(40)), |
81
|
|
|
'token' => str_random(64), |
82
|
|
|
'activated' => true, |
83
|
|
|
'signup_sm_ip_address' => $ipAddress->getClientIp(), |
84
|
|
|
|
85
|
|
|
]); |
86
|
|
|
|
87
|
|
|
$socialData->social_id = $socialUserObject->id; |
88
|
|
|
$socialData->provider = $provider; |
89
|
|
|
$user->social()->save($socialData); |
90
|
|
|
$user->attachRole($role); |
91
|
|
|
$user->activated = true; |
92
|
|
|
|
93
|
|
|
$user->profile()->save($profile); |
94
|
|
|
$user->save(); |
95
|
|
|
|
96
|
|
|
if ($socialData->provider == 'github') { |
97
|
|
|
$user->profile->github_username = $socialUserObject->nickname; |
98
|
|
|
} |
99
|
|
|
if ($socialData->provider == 'twitter') { |
100
|
|
|
$user->profile()->twitter_username = $socialUserObject->username; |
|
|
|
|
101
|
|
|
} |
102
|
|
|
$user->profile->save(); |
103
|
|
|
|
104
|
|
|
$socialUser = $user; |
105
|
|
|
} else { |
106
|
|
|
$socialUser = $sameSocialId->user; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
auth()->login($socialUser, true); |
110
|
|
|
|
111
|
|
|
return redirect('home')->with('success', trans('socials.registerSuccess')); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
$socialUser = $userCheck; |
115
|
|
|
|
116
|
|
|
auth()->login($socialUser, true); |
117
|
|
|
|
118
|
|
|
return redirect('home'); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|