2026-03-31 00:18:07 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Auth;
|
|
|
|
|
|
|
|
|
|
use App\Actions\User\CreateUser;
|
2026-05-04 21:42:25 +00:00
|
|
|
use App\Http\Controllers\Auth\Concerns\PreservesUtmParameters;
|
2026-03-31 00:18:07 +00:00
|
|
|
use App\Http\Controllers\Controller;
|
|
|
|
|
use App\Models\User;
|
|
|
|
|
use Illuminate\Auth\Events\Registered;
|
|
|
|
|
use Illuminate\Http\RedirectResponse;
|
2026-05-04 21:42:25 +00:00
|
|
|
use Illuminate\Http\Request;
|
2026-03-31 00:18:07 +00:00
|
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
|
use Laravel\Socialite\Facades\Socialite;
|
|
|
|
|
|
2026-05-04 21:42:25 +00:00
|
|
|
class GoogleController extends Controller
|
2026-03-31 00:18:07 +00:00
|
|
|
{
|
2026-05-04 21:42:25 +00:00
|
|
|
use PreservesUtmParameters;
|
|
|
|
|
|
|
|
|
|
public function redirect(Request $request): RedirectResponse
|
2026-03-31 00:18:07 +00:00
|
|
|
{
|
2026-05-04 21:42:25 +00:00
|
|
|
$this->storeUtmParameters($request);
|
|
|
|
|
|
2026-03-31 00:18:07 +00:00
|
|
|
return Socialite::driver('google-auth')->redirect();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function callback(): RedirectResponse
|
|
|
|
|
{
|
2026-03-31 03:40:18 +00:00
|
|
|
try {
|
|
|
|
|
$googleUser = Socialite::driver('google-auth')->user();
|
|
|
|
|
} catch (\Exception) {
|
|
|
|
|
return redirect()->route('login');
|
|
|
|
|
}
|
2026-03-31 00:18:07 +00:00
|
|
|
|
2026-05-03 20:42:44 +00:00
|
|
|
$user = User::where('google_id', $googleUser->getId())
|
|
|
|
|
->orWhere('email', $googleUser->getEmail())
|
|
|
|
|
->first();
|
2026-03-31 00:18:07 +00:00
|
|
|
|
|
|
|
|
if ($user) {
|
2026-05-03 20:42:44 +00:00
|
|
|
return $this->loginExistingUser($user, $googleUser->getId());
|
2026-03-31 00:18:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->registerNewUser($googleUser);
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-03 20:42:44 +00:00
|
|
|
private function loginExistingUser(User $user, string $googleId): RedirectResponse
|
2026-03-31 00:18:07 +00:00
|
|
|
{
|
2026-05-03 20:42:44 +00:00
|
|
|
if (! $user->google_id) {
|
|
|
|
|
$user->update(['google_id' => $googleId]);
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-31 00:18:07 +00:00
|
|
|
if (! $user->hasVerifiedEmail()) {
|
|
|
|
|
$user->markEmailAsVerified();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Auth::login($user, remember: true);
|
|
|
|
|
|
2026-05-04 21:42:25 +00:00
|
|
|
$this->retrieveUtmParameters();
|
|
|
|
|
|
2026-03-31 00:18:07 +00:00
|
|
|
return redirect()->route('app.home');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function registerNewUser(\Laravel\Socialite\Contracts\User $googleUser): RedirectResponse
|
|
|
|
|
{
|
2026-05-04 21:42:25 +00:00
|
|
|
$utmParameters = $this->retrieveUtmParameters();
|
|
|
|
|
|
2026-03-31 00:18:07 +00:00
|
|
|
$user = CreateUser::execute([
|
|
|
|
|
'name' => $googleUser->getName(),
|
|
|
|
|
'email' => $googleUser->getEmail(),
|
2026-05-03 20:42:44 +00:00
|
|
|
'google_id' => $googleUser->getId(),
|
2026-03-31 00:18:07 +00:00
|
|
|
'email_verified_at' => now(),
|
2026-05-04 21:42:25 +00:00
|
|
|
'registration_ip' => request()->ip(),
|
|
|
|
|
], $utmParameters);
|
2026-03-31 00:18:07 +00:00
|
|
|
|
|
|
|
|
event(new Registered($user));
|
|
|
|
|
|
|
|
|
|
Auth::login($user, remember: true);
|
|
|
|
|
|
2026-03-31 00:32:43 +00:00
|
|
|
session()->flash('auth_provider', 'google');
|
|
|
|
|
|
2026-05-04 21:42:25 +00:00
|
|
|
return redirect()->route('register.success', $utmParameters);
|
2026-03-31 00:18:07 +00:00
|
|
|
}
|
|
|
|
|
}
|