-
Notifications
You must be signed in to change notification settings - Fork 51
/
LoginController.php
127 lines (108 loc) · 3.59 KB
/
LoginController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Google_Service_Calendar;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Laravel\Socialite\Facades\Socialite;
use Modules\User\Entities\User;
class LoginController extends Controller
{
/*
|--------------------------------------------------------------------------
| Login Controller
|--------------------------------------------------------------------------
|
| This controller handles authenticating users for the application and
| redirecting them to your home screen. The controller uses a trait
| to conveniently provide its functionality to your applications.
|
*/
use AuthenticatesUsers;
/**
* Where to redirect users after login.
*
* @var string
*/
protected $redirectTo = '/home';
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest')->except('logout');
}
/**
* Redirect the user to the OAuth Provider.
*/
public function redirectToProvider($provider)
{
switch ($provider) {
case 'google':
return Socialite::driver($provider)
->with(['access_type' => 'offline', 'hd' => config('constants.gsuite.client-hd'), 'prompt' => 'consent select_account'])
->scopes([Google_Service_Calendar::CALENDAR])
->redirect();
default:
return Socialite::driver($provider)->redirect();
}
}
/**
* Obtain the user information from provider. Check if the user already exists in our
* database by looking up their provider_id in the database.
* If the user exists, log them in. Otherwise, create a new user then log them in. After that
* redirect them to the authenticated users homepage.
*/
public function handleProviderCallback($provider)
{
$user = Socialite::driver($provider)->user();
$authUser = $this->findOrCreateUser($user, $provider);
if ($authUser->trashed()) {
return redirect('login');
}
Auth::login($authUser, true);
/*
* Update user avatar to keep it update with gmail
*/
$authUser->update(['avatar' => $user->avatar_original]);
if (session('saml_request_for_website')) {
if (! $authUser->website_user_role) {
Auth::logout();
return redirect('login');
}
return redirect(config('constants.website_url') . '/wp/wp-admin/');
}
return redirect('home');
}
/**
* If a user has registered before using social auth, return the user
* else, create a new user object.
*
* @return User
*/
public function findOrCreateUser($user, $provider)
{
$authUser = User::withTrashed()
->where('provider_id', $user->id)
->orWhere('email', $user->email)
->first();
if ($authUser) {
$authUser->provider_id = $user->id;
$authUser->save();
return $authUser;
}
$user = User::create([
'name' => $user->name,
'email' => $user->email,
'provider' => $provider,
'provider_id' => $user->id,
'avatar' => '',
]);
$role = DB::table('roles')->select('id')->where('name', 'book-manager')->first();
$user->roles()->attach($role);
return $user;
}
}