1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NukaCode\Users\Http\Controllers; |
4
|
|
|
|
5
|
|
|
use App\Http\Controllers\BaseController; |
6
|
|
|
use App\Models\User; |
7
|
|
|
use Illuminate\Http\Request; |
8
|
|
|
use Laravel\Socialite\Facades\Socialite; |
9
|
|
|
|
10
|
|
|
class SocialAuthController extends BaseController |
11
|
|
|
{ |
12
|
|
|
protected $driver; |
13
|
|
|
|
14
|
|
|
protected $scopes; |
15
|
|
|
|
16
|
|
|
protected $extras; |
17
|
|
|
|
18
|
|
|
public function __construct() |
19
|
|
|
{ |
20
|
|
|
parent::__construct(); |
21
|
|
|
|
22
|
|
|
list($driver, $scopes, $extras) = config('nukacode-user.social'); |
23
|
|
|
|
24
|
|
|
$this->driver = $driver; |
25
|
|
|
$this->scopes = $scopes; |
26
|
|
|
$this->extras = $extras; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Redirect the user to the social providers auth page. |
31
|
|
|
* |
32
|
|
|
* @return mixed |
33
|
|
|
*/ |
34
|
|
|
public function login() |
35
|
|
|
{ |
36
|
|
|
if (is_null($this->driver)) { |
37
|
|
|
throw new \InvalidArgumentException('You must set a social driver to use the social authenticating features.'); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
return Socialite::driver($this->driver) |
41
|
|
|
->scopes($this->scopes) |
42
|
|
|
->with($this->extras) |
43
|
|
|
->redirect(); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Use the returned user to register (if needed) and login. |
48
|
|
|
* |
49
|
|
|
* @param \Illuminate\Http\Request $request |
50
|
|
|
* |
51
|
|
|
* @return mixed |
52
|
|
|
*/ |
53
|
|
|
public function callback(Request $request) |
54
|
|
|
{ |
55
|
|
|
$socialUser = Socialite::driver($this->driver)->user(); |
56
|
|
|
$user = User::where('email', $socialUser->getEmail())->first(); |
57
|
|
|
|
58
|
|
|
if (is_null($user)) { |
59
|
|
|
$user = $this->register($socialUser); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
auth()->login($user, $request->get('remember', false)); |
63
|
|
|
|
64
|
|
|
return redirect() |
65
|
|
|
->intended('home') |
66
|
|
|
->with('message', 'You have been logged in.'); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Create a new user from a social user. |
71
|
|
|
* |
72
|
|
|
* @param $socialUser |
73
|
|
|
* |
74
|
|
|
* @return mixed |
75
|
|
|
*/ |
76
|
|
|
private function register($socialUser) |
77
|
|
|
{ |
78
|
|
|
$names = explode(' ', $socialUser->getName()); |
79
|
|
|
|
80
|
|
|
$userDetails = [ |
81
|
|
|
'username' => $socialUser->getNickname(), |
82
|
|
|
'email' => $socialUser->getEmail(), |
83
|
|
|
'first_name' => $names[0], |
84
|
|
|
'last_name' => $names[1], |
85
|
|
|
'display_name' => $socialUser->getNickname(), |
86
|
|
|
'social_id' => $socialUser->getId(), |
87
|
|
|
'social_avatar' => $socialUser->getAvatar(), |
88
|
|
|
]; |
89
|
|
|
|
90
|
|
|
$user = User::create($userDetails); |
91
|
|
|
$user->assignRole(config('nukacode-user.default')); |
92
|
|
|
|
93
|
|
|
return $user; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Log the user out. |
98
|
|
|
* |
99
|
|
|
* @return mixed |
100
|
|
|
*/ |
101
|
|
|
public function logout() |
102
|
|
|
{ |
103
|
|
|
auth()->logout(); |
104
|
|
|
|
105
|
|
|
return redirect(route('home')) |
106
|
|
|
->with('message', 'You have been logged out.'); |
107
|
|
|
} |
108
|
|
|
} |