Persists marketing attribution and registration metadata for new users across the three signup paths (email, Google, GitHub): - 5 utm_* columns + registration_ip on the users table - PreservesUtmParameters trait stores incoming utm_* query params on the register/redirect GET, retrieves them on the POST/callback — surviving the OAuth round-trip via session - request()->ip() captured at the controller layer Adds GitHub as a second OAuth provider: - GitHubController mirroring the Google one (now renamed from SocialLoginController for symmetry) - Settings → Authentication can connect/disconnect GitHub like Google - Single SocialLogin.vue component replaces the per-provider buttons on Login/Register, rendering each enabled provider plus a single "or continue with" divider UserFactory gains defaults for the new nullable columns so model strict-mode access in tests doesn't trip.
84 lines
2.3 KiB
PHP
84 lines
2.3 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');
|
|
}
|
|
|
|
$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 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);
|
|
}
|
|
}
|