trypost/app/Http/Controllers/Auth/GoogleController.php
Paulo Castellano d89f6ceede fix: let authenticated users connect Google/GitHub from Settings
The Connect button on /settings/authentication pointed at the
auth.{provider}.redirect routes that live behind `guest` middleware,
so authenticated users were bounced to /app/home before reaching
Socialite. The OAuth callback also needed to handle two flows
(signup/login vs link to current user) but had no branch for the
second case — meaning a different-email GitHub account would have
been registered as a new user, logging the original session out.

Splits the flows by intent:

- New `app.authentication.connect-provider` route in the auth group,
  handled by the settings controller (where it sits next to
  disconnect-provider). Replaces the OAuth signup link as the
  Connect button's target.
- Auth callbacks moved out of the guest group (still one URL per
  provider, since OAuth apps only register one) and gain a single
  Auth::check() branch that calls connectToCurrentUser().
- connectToCurrentUser() rejects if the provider id already belongs
  to a different user; otherwise sets it on the current user and
  redirects back to settings with a flash message.
2026-05-04 19:22:09 -03:00

107 lines
3.1 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Http\Controllers\Auth;
use App\Actions\User\CreateUser;
use App\Http\Controllers\Auth\Concerns\PreservesUtmParameters;
use App\Http\Controllers\Controller;
use App\Models\User;
use Illuminate\Auth\Events\Registered;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Laravel\Socialite\Facades\Socialite;
class GoogleController extends Controller
{
use PreservesUtmParameters;
public function redirect(Request $request): RedirectResponse
{
$this->storeUtmParameters($request);
return Socialite::driver('google-auth')->redirect();
}
public function callback(): RedirectResponse
{
try {
$googleUser = Socialite::driver('google-auth')->user();
} catch (\Exception) {
return redirect()->route('login');
}
if (Auth::check()) {
return $this->connectToCurrentUser(Auth::user(), $googleUser->getId());
}
$user = User::where('google_id', $googleUser->getId())
->orWhere('email', $googleUser->getEmail())
->first();
if ($user) {
return $this->loginExistingUser($user, $googleUser->getId());
}
return $this->registerNewUser($googleUser);
}
private function connectToCurrentUser(User $user, string $googleId): RedirectResponse
{
$existing = User::where('google_id', $googleId)
->where('id', '!=', $user->id)
->first();
if ($existing) {
return redirect()->route('app.authentication.edit')
->with('flash.error', __('settings.authentication.providers.flash_already_linked', ['provider' => 'Google']));
}
if ($user->google_id !== $googleId) {
$user->update(['google_id' => $googleId]);
}
return redirect()->route('app.authentication.edit')
->with('flash.success', __('settings.authentication.providers.flash_connected', ['provider' => 'Google']));
}
private function loginExistingUser(User $user, string $googleId): RedirectResponse
{
if (! $user->google_id) {
$user->update(['google_id' => $googleId]);
}
if (! $user->hasVerifiedEmail()) {
$user->markEmailAsVerified();
}
Auth::login($user, remember: true);
$this->retrieveUtmParameters();
return redirect()->route('app.home');
}
private function registerNewUser(\Laravel\Socialite\Contracts\User $googleUser): RedirectResponse
{
$utmParameters = $this->retrieveUtmParameters();
$user = CreateUser::execute([
'name' => $googleUser->getName(),
'email' => $googleUser->getEmail(),
'google_id' => $googleUser->getId(),
'email_verified_at' => now(),
'registration_ip' => request()->ip(),
], $utmParameters);
event(new Registered($user));
Auth::login($user, remember: true);
session()->flash('auth_provider', 'google');
return redirect()->route('register.success', $utmParameters);
}
}