-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAuthController.php
More file actions
58 lines (48 loc) · 1.54 KB
/
Copy pathAuthController.php
File metadata and controls
58 lines (48 loc) · 1.54 KB
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
<?php
namespace App\Http\Controllers;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Laravel\Socialite\Facades\Socialite;
use Illuminate\Support\Str;
class AuthController extends Controller
{
public function redirectGoogle()
{
return Socialite::driver('google')->redirect();
}
/**
* Redirect to Google OAuth page.
*
* Uses Laravel Socialite to start the OAuth flow.
*
* @return \Illuminate\Http\RedirectResponse
*/
public function callbackGoogle()
{
$googleUser = Socialite::driver('google')->stateless()->user();
$user = User::updateOrCreate(
['email' => $googleUser->getEmail()],
[
'name' => $googleUser->getName() ?: ($googleUser->getNickname() ?: 'Student'),
'google_id' => $googleUser->getId(),
'avatar_url' => $googleUser->getAvatar(),
'password' => bcrypt(Str::random(32)),
'email_verified_at' => now(),
]
);
if (!$this->userHasAnyRole($user, ['Admin','LabStaff','Student'])) {
$user->assignRole('Student');
}
Auth::login($user, true);
return redirect()->route('dashboard');
}
/**
* Handle Google OAuth callback.
*
* Finds or creates a `User` by email, assigns a default `Student` role if none
* of the expected roles are present, logs the user in and redirects to the dashboard.
*
* @return \Illuminate\Http\RedirectResponse
*/
}