Skip to content

Commit 6d52313

Browse files
committed
OAuth Clear
1 parent 06d8798 commit 6d52313

File tree

10 files changed

+247
-12
lines changed

10 files changed

+247
-12
lines changed

.rnd

0 Bytes
Binary file not shown.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace App\Http\Controllers;
4+
5+
use Illuminate\Http\Request;
6+
use Laravel\Socialite\Facades\Socialite;
7+
use App\Models\User;
8+
use Auth;
9+
10+
class UserController extends Controller
11+
{
12+
public function login()
13+
{
14+
return view('auth.user.login');
15+
}
16+
17+
public function google()
18+
{
19+
return Socialite::driver('google')->redirect();
20+
}
21+
22+
public function handleProviderCallback()
23+
{
24+
$callback = Socialite::driver('google')->stateless()->user();
25+
$data = [
26+
'name' => $callback->getName(),
27+
'email' => $callback->getEmail(),
28+
'avatar' => $callback->getAvatar(),
29+
'email_verified_at' => date('Y-m-d H:i:s', time())
30+
];
31+
32+
$user = User::firstOrCreate(
33+
[
34+
'email' => $data['email']
35+
], $data);
36+
Auth::login($user, true);
37+
return redirect(route('welcome'));
38+
}
39+
}

app/Models/User.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ class User extends Authenticatable
2424
'password',
2525
'avatar',
2626
'occupation',
27-
'is_admin'
27+
'is_admin',
28+
'email_verified_at'
2829
];
2930

3031
/**

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"laravel/breeze": "1.9.2",
1212
"laravel/framework": "^8.75",
1313
"laravel/sanctum": "^2.11",
14+
"laravel/socialite": "^5.6",
1415
"laravel/tinker": "^2.5"
1516
},
1617
"require-dev": {

composer.lock

Lines changed: 146 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/services.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,11 @@
3030
'region' => env('AWS_DEFAULT_REGION', 'us-east-1'),
3131
],
3232

33+
// socialite config
34+
'google' => [
35+
'client_id' => env('GOOGLE_CLIENT_ID'),
36+
'client_secret' => env('GOOGLE_CLIENT_SECRET'),
37+
'redirect' => env('GOOGLE_CLIENT_REDIRECT'),
38+
],
39+
3340
];

resources/views/login.blade.php renamed to resources/views/auth/user/login.blade.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
<meta charset="UTF-8">
66
<meta name="viewport" content="width=device-width, initial-scale=1">
77

8+
<!--===============================================================================================-->
9+
<link rel="shortcut icon" type="image/icon" href="..." />
810
<!--===============================================================================================-->
911
<link rel="stylesheet" type="text/css" href="{{asset('asset-login/vendor/bootstrap/css/bootstrap.min.css')}}">
1012
<!--===============================================================================================-->
@@ -85,13 +87,14 @@
8587
<i class="fa fa-facebook-f" aria-hidden="true"></i>
8688
</a>
8789

90+
<a href="{{route('user.login.google')}}" class="login100-form-social-item flex-c-m bg3 m-r-5">
91+
<i class="fa fa-google" aria-hidden="true"></i>
92+
</a>
93+
8894
<a href="#" class="login100-form-social-item flex-c-m bg2 m-r-5">
8995
<i class="fa fa-twitter" aria-hidden="true"></i>
9096
</a>
9197

92-
<a href="#" class="login100-form-social-item flex-c-m bg3 m-r-5">
93-
<i class="fa fa-google" aria-hidden="true"></i>
94-
</a>
9598
</div>
9699
</form>
97100

resources/views/components/navbar.blade.php

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!-- -->
1+
22
<nav class="navbar navbar-expand-lg navbar-light">
33
<div class="container">
44
<a class="navbar-brand" href="{{route('welcome')}}">
@@ -35,10 +35,45 @@ class="navbar-toggler"
3535
<a class="nav-link" href="#">Business</a>
3636
</li>
3737
</ul>
38+
@auth
39+
{{-- Login --}}
40+
<div class="d-flex user-logged nav-item dropdown no-arrow">
41+
<a href="#" id="dropdownMenuLink" data-bs-toggle="dropdown" aria-expanded="false">
42+
Halo, {{Auth::user()->name}}!
43+
<img src={{Auth::user()->avatar}} class="user-photo rounded-circle" alt="">
44+
<ul class="dropdown-menu" aria-labelledby="dropdownMenuLink" style="right:0; left: auto" >
45+
<li>
46+
<a href="#" class="dropdown-item">Profile</a>
47+
</li>
48+
<li>
49+
<a href="#" class="dropdown-item">Dashboard</a>
50+
</li>
51+
<li><hr class="dropdown-divider"></li>
52+
<li>
53+
<a href="#" class="dropdown-item" onclick="event.preventDefault(); document.getElementById('logout-form').submit()">Sign out</a>
54+
<form action="{{route('logout')}}" method="post" id="logout-form" style="display: none">
55+
<input type="hidden" name="_token" value="{{ csrf_token() }}">
56+
</form>
57+
</li>
58+
</ul>
59+
</a>
60+
</div>
61+
62+
@else
63+
{{-- Guest --}}
3864
<div class="d-flex">
39-
<a href="{{route('login')}}" class="btn btn-master btn-secondary me-3"> Sign In </a>
40-
<a href="{{route('login')}}" class="btn btn-master btn-primary"> Sign Up </a>
65+
<a href="{{route('login')}}" class="btn btn-master btn-secondary me-3">
66+
Sign In
67+
</a>
68+
<a href="{{route('login')}}" class="btn btn-master btn-primary">
69+
Sign Up
70+
</a>
4171
</div>
72+
73+
@endauth
74+
4275
</div>
4376
</div>
44-
</nav>
77+
</nav>
78+
79+
{{-- Login --}}

routes/auth.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use App\Http\Controllers\Auth\PasswordResetLinkController;
99
use App\Http\Controllers\Auth\RegisteredUserController;
1010
use App\Http\Controllers\Auth\VerifyEmailController;
11+
use App\Http\Controllers\UserController;
1112
use Illuminate\Support\Facades\Route;
1213

1314
Route::middleware('guest')->group(function () {
@@ -16,9 +17,7 @@
1617

1718
Route::post('register', [RegisteredUserController::class, 'store']);
1819

19-
Route::get('login', function(){
20-
return view('login');
21-
})->name('login');
20+
Route::get('login', [UserController::class, 'login'] )->name('login');
2221

2322
Route::get('admin/login', [AuthenticatedSessionController::class, 'create'])
2423
->name('login.admin');

routes/web.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
use Illuminate\Support\Facades\Route;
4+
use App\Http\Controllers\UserController;
45

56
/*
67
|--------------------------------------------------------------------------
@@ -29,6 +30,10 @@
2930
return view('checkout-success');
3031
})->name('checkout.success');
3132

33+
// socialite routes
34+
Route::get('sign-in-google', [UserController::class, 'google'])->name('user.login.google');
35+
36+
Route::get('auth/google/callback', [UserController::class, 'handleProviderCallback'])->name('user.google.callback');
3237

3338

3439
Route::get('/dashboard', function () {

0 commit comments

Comments
 (0)