Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ LOG_STACK=single
LOG_DEPRECATIONS_CHANNEL=null
LOG_LEVEL=debug

GOOGLE_CLIENT_ID=test
GOOGLE_CLIENT_SECRET=test
GOOGLE_REDIRECT_URL=http://localhost/auth/google/callback
GITHUB_CLIENT_ID=test
GITHUB_CLIENT_SECRET=test
GITHUB_REDIRECT_URL=http://localhost/auth/github/callback


DB_CONNECTION=sqlite
# DB_HOST=127.0.0.1
# DB_PORT=3306
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
/.nova
/.phpunit.cache
/.zed
/.pnpm-store
/auth.json
/node_modules
/public/build
Expand All @@ -26,3 +27,4 @@ components.d.ts
resources/js/routes
resources/js/actions
resources/js/wayfinder

116 changes: 116 additions & 0 deletions app/Http/Controllers/SocialProviderController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use App\Models\User;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;
use Laravel\Socialite\Facades\Socialite;
class SocialProviderController extends Controller
{
//

public function redirectToProvider($provider)
{
return Socialite::driver($provider)->redirect();
}

public function handleProviderCallback($provider, Request $request)
{
$socialUser = Socialite::driver($provider)->user();

try {
// If user is already authenticated, link the social account
if (Auth::check()) {
return $this->linkSocialAccount(Auth::user(), $provider, $socialUser);
}

// Check if user already exists with this social provider
$existingSocialUser = User::where([
'provider' => $provider,
'provider_id' => $socialUser->getId()
])->first();

if ($existingSocialUser) {
// User exists with this social provider, log them in
$existingSocialUser->provider_token = $socialUser->token;
$existingSocialUser->save();

Auth::login($existingSocialUser);
return redirect()->route('dashboard');
}

// Check if email is already used by another account
$existingUser = User::where('email', $socialUser->getEmail())->first();

if ($existingUser) {
// Email exists but with different provider or username/password
if ($existingUser->provider && $existingUser->provider !== $provider) {
return redirect()->route('login')->withErrors([
'email' => 'Cet email est déjà associé à un compte ' . $existingUser->provider
]);
} else if (!$existingUser->provider) {
return redirect()->route('login')->withErrors([
'email' => 'Cet email utilise une autre méthode de connexion (nom d\'utilisateur/mot de passe)'
]);
}
}

// Create new user account
$user = User::create([
'name' => $socialUser->getName(),
'email' => $socialUser->getEmail(),
'provider' => $provider,
'provider_id' => $socialUser->getId(),
'provider_token' => $socialUser->token,
'email_verified_at' => now(),
'profile_photo_path' => $socialUser->getAvatar(),
]);

Auth::login($user);
return redirect()->route('dashboard');

} catch (\Exception $e) {
return redirect()->route('login')->withErrors([
'email' => 'Une erreur est survenue lors de la connexion'
]);
}
}

/**
* Link a social account to an existing authenticated user
*/
private function linkSocialAccount(User $user, string $provider, $socialUser)
{
// Check if this social account is already linked to another user
$existingSocialUser = User::where([
'provider' => $provider,
'provider_id' => $socialUser->getId()
])->first();

if ($existingSocialUser && $existingSocialUser->id !== $user->id) {
return redirect()->route('user.profile.index')->withErrors([
'social' => 'Ce compte ' . $provider . ' est déjà lié à un autre utilisateur'
]);
}

// Check if user already has this provider linked
if ($user->provider === $provider) {
return redirect()->route('user.profile.index')->withErrors([
'social' => 'Ce compte ' . $provider . ' est déjà lié à votre profil'
]);
}

// Update user with social provider information
$user->update([
'provider' => $provider,
'provider_id' => $socialUser->getId(),
'provider_token' => $socialUser->token,
]);

return redirect()->route('user.profile.index')->with('success',
'Compte ' . $provider . ' lié avec succès à votre profil'
);
}
}
5 changes: 5 additions & 0 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ class User extends Authenticatable implements MustVerifyEmail
'name',
'email',
'password',
'provider',
'provider_id',
'provider_token',
'email_verified_at',
'profile_photo_path',
];

/**
Expand Down
12 changes: 11 additions & 1 deletion app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Providers;

use Illuminate\Support\Facades\Route;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
Expand All @@ -19,6 +20,15 @@ public function register(): void
*/
public function boot(): void
{
//
$this->loadAuthRoutes();
}

/**
* Load the auth routes.
*/
private function loadAuthRoutes(): void
{
Route::middleware('web')
->group(base_path('routes/auth.php'));
}
}
Empty file modified artisan
100755 → 100644
Empty file.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"inertiajs/inertia-laravel": "^2.0",
"laravel/fortify": "^1.28",
"laravel/framework": "^12.0",
"laravel/socialite": "^5.23",
"laravel/tinker": "^2.10.1",
"laravel/wayfinder": "^0.1.6"
},
Expand Down
Loading