Skip to content

Commit ec8eadf

Browse files
committed
Add Google authentication support and enhance user token management
This commit updates the SocialAuthController to include the generation of refresh tokens for Google authentication, improving user session management. It also adds Google service configuration to the services file and introduces a new migration for storing Google IDs in the users table. Additionally, a new migration is created for the shop_reviews table, while the notifications table migration is removed, streamlining the database structure.
1 parent 195aa55 commit ec8eadf

File tree

5 files changed

+54
-33
lines changed

5 files changed

+54
-33
lines changed

app/Http/Controllers/Auth/SocialAuthController.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace App\Http\Controllers\Auth;
44

5+
use Carbon\Carbon;
56
use App\Models\User;
67
use Illuminate\Http\Request;
78
use Illuminate\Support\Facades\Log;
@@ -51,7 +52,18 @@ public function handleGoogleCallback(): RedirectResponse
5152
$tokenResult = $user->createToken('GoogleAuthToken', [$scope]);
5253
$token = $tokenResult->accessToken;
5354

54-
return redirect("{$frontendUrl}/auth/callback?token={$token}&user_id={$user->id}&role_id={$user->role_id}");
55+
// Refresh Token (Créer un jeton de durée de vie plus longue pour le rafraîchissement)
56+
$refreshToken = $user->createToken('GoogleRefreshToken', [], Carbon::now()->addDays(30))->accessToken;
57+
58+
$domain = config('app.env') === 'production' ? parse_url(config('app.url'), PHP_URL_HOST) : null;
59+
$secure = config('app.env') === 'production';
60+
61+
return redirect("{$frontendUrl}/auth/callback")->cookie('accessToken', $accessToken,
62+
Carbon::now()->addMinutes(config('passport.token_ttl'))->timestamp,
63+
'/', $domain, $secure, true, false, 'lax') // ttl, path, domain, secure, httpOnly, raw, sameSite
64+
->cookie('refreshToken', $refreshToken,
65+
Carbon::now()->addDays(30)->timestamp, // Longue durée de vie
66+
'/', $domain, $secure, true, false, 'lax');
5567
}
5668

5769
protected function getUserScope(int $roleId): string

config/services.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,12 @@
2020
'endpoint' => env('MAILGUN_ENDPOINT', 'api.mailgun.net'),
2121
'scheme' => 'https',
2222
],
23-
23+
'google' => [
24+
'client_id' => env('GOOGLE_CLIENT_ID'),
25+
'client_secret' => env('GOOGLE_CLIENT_SECRET'),
26+
// Assurez-vous d'utiliser le bon nom de variable d'environnement
27+
'redirect' => env('GOOGLE_REDIRECT_URI'),
28+
],
2429
'postmark' => [
2530
'token' => env('POSTMARK_TOKEN'),
2631
],

database/migrations/2014_10_12_000000_create_users_table.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public function up(): void
3737
$table->string('profile')->nullable();
3838
$table->string("drivers_license")->nullable();
3939
$table->string('residence')->default("1");
40+
$table->string('google_id')->nullable();
4041
$table->boolean('isSeller')->default(0);
4142
$table->boolean('isDelivery')->default(0);
4243
$table->rememberToken();
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
use App\Models\Shop;
4+
use App\Models\User;
5+
use Illuminate\Support\Facades\Schema;
6+
use Illuminate\Database\Schema\Blueprint;
7+
use Illuminate\Database\Migrations\Migration;
8+
9+
return new class extends Migration
10+
{
11+
/**
12+
* Run the migrations.
13+
*/
14+
public function up(): void
15+
{
16+
Schema::create('shop_reviews', function (Blueprint $table) {
17+
$table->id();
18+
$table->foreignIdFor(User::class)->constrained()->cascadeOnDelete()->cascadeOnUpdate();
19+
$table->foreignIdFor(Shop::class)->constrained()->cascadeOnDelete()->cascadeOnUpdate();
20+
$table->integer('rating');
21+
$table->text('comment');
22+
$table->boolean('is_approved')->default(0);
23+
$table->timestamps();
24+
});
25+
}
26+
27+
/**
28+
* Reverse the migrations.
29+
*/
30+
public function down(): void
31+
{
32+
Schema::dropIfExists('shop_reviews');
33+
}
34+
};

database/migrations/2025_08_16_164650_create_notifications_table.php

Lines changed: 0 additions & 31 deletions
This file was deleted.

0 commit comments

Comments
 (0)