144 lines
4.6 KiB
PHP
144 lines
4.6 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Auth;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\SocialAccount;
|
|
use App\Models\User;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Str;
|
|
use Laravel\Socialite\Facades\Socialite;
|
|
|
|
class SocialiteController extends Controller
|
|
{
|
|
/**
|
|
* Suggest a username based on social profile.
|
|
*/
|
|
protected function suggestUsername($socialUser): string
|
|
{
|
|
return Str::slug(
|
|
$socialUser->getNickname() ?? explode('@', $socialUser->getEmail())[0],
|
|
'_'
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Get enabled providers from config.
|
|
*/
|
|
protected function getEnabledProviders(): array
|
|
{
|
|
return collect(config('auth-ui.providers', []))
|
|
->filter(fn ($provider) => $provider['enabled'] ?? false)
|
|
->keys()
|
|
->all();
|
|
}
|
|
|
|
/**
|
|
* Redirect the user to the provider authentication page.
|
|
*/
|
|
public function redirect(string $provider): RedirectResponse
|
|
{
|
|
if (! in_array($provider, $this->getEnabledProviders())) {
|
|
abort(404, 'Provider not enabled');
|
|
}
|
|
|
|
return Socialite::driver($provider)->redirect();
|
|
}
|
|
|
|
/**
|
|
* Obtain the user information from provider.
|
|
*/
|
|
public function callback(string $provider): RedirectResponse
|
|
{
|
|
if (! in_array($provider, $this->getEnabledProviders())) {
|
|
abort(404, 'Provider not enabled');
|
|
}
|
|
|
|
try {
|
|
$socialUser = Socialite::driver($provider)->user();
|
|
} catch (\Exception $e) {
|
|
return redirect()->route('login')->withErrors([
|
|
'email' => 'Unable to authenticate with '.$provider.'. Please try again.',
|
|
]);
|
|
}
|
|
|
|
// First, check if this provider account is already linked to a user
|
|
$socialAccount = SocialAccount::where('provider', $provider)
|
|
->where('provider_id', $socialUser->getId())
|
|
->first();
|
|
|
|
if ($socialAccount) {
|
|
Auth::login($socialAccount->user, remember: true);
|
|
request()->session()->regenerate();
|
|
|
|
return redirect()->intended(config('auth-ui.redirects.login', '/'));
|
|
}
|
|
|
|
// Check if a user with this email already exists
|
|
$existingUser = User::where('email', $socialUser->getEmail())->first();
|
|
|
|
if ($existingUser) {
|
|
if (! $existingUser->hasVerifiedEmail()) {
|
|
return redirect()->route('login')->withErrors([
|
|
'email' => 'An account with this email already exists. Please verify your email first, then link your social account.',
|
|
]);
|
|
}
|
|
|
|
$existingUser->socialAccounts()->create([
|
|
'provider' => $provider,
|
|
'provider_id' => $socialUser->getId(),
|
|
]);
|
|
|
|
Auth::login($existingUser, remember: true);
|
|
request()->session()->regenerate();
|
|
|
|
return redirect()->intended(config('auth-ui.redirects.login', '/'));
|
|
}
|
|
|
|
// New user — check if registration is enabled
|
|
if (! config('auth-ui.features.registration')) {
|
|
return redirect()->route('login')->withErrors([
|
|
'email' => 'Registration is disabled. Please contact an administrator.',
|
|
]);
|
|
}
|
|
|
|
$name = $socialUser->getName() ?? $socialUser->getNickname() ?? 'User';
|
|
$nameParts = explode(' ', $name, 2);
|
|
$suggestedUsername = $this->suggestUsername($socialUser);
|
|
|
|
// Check if username is already taken
|
|
if (User::whereRaw('LOWER(username) = ?', [strtolower($suggestedUsername)])->exists()) {
|
|
session()->put('socialite_user', [
|
|
'email' => $socialUser->getEmail(),
|
|
'first_name' => $nameParts[0],
|
|
'last_name' => $nameParts[1] ?? '',
|
|
'suggested_username' => $suggestedUsername,
|
|
'provider' => $provider,
|
|
'provider_id' => $socialUser->getId(),
|
|
]);
|
|
|
|
return redirect()->route('auth.complete-profile');
|
|
}
|
|
|
|
$user = User::create([
|
|
'username' => $suggestedUsername,
|
|
'first_name' => $nameParts[0],
|
|
'last_name' => $nameParts[1] ?? '',
|
|
'email' => $socialUser->getEmail(),
|
|
]);
|
|
|
|
$user->forceFill(['email_verified_at' => now()])->save();
|
|
|
|
$user->socialAccounts()->create([
|
|
'provider' => $provider,
|
|
'provider_id' => $socialUser->getId(),
|
|
]);
|
|
|
|
Auth::login($user, remember: true);
|
|
request()->session()->regenerate();
|
|
|
|
return redirect()->intended(config('auth-ui.redirects.login', '/'));
|
|
}
|
|
}
|