Splits the OAuth connect flow off entirely from signup/login so each
route has a single responsibility.
- routes/auth.php returns to its original state — redirect + callback
both back inside the `guest` group.
- routes/app.php gains a paired callback route at
/settings/authentication/providers/{provider}/callback.
- AuthenticationController::connectProvider /
connectProviderCallback override Socialite's redirectUrl so the
round-trip stays on the connect-flow URL. The Auth::check() branch
in the auth controllers is gone.
The OAuth apps in Google Cloud and GitHub Developer Settings need the
new callback URL registered alongside the existing one — documented in
.env.example.
92 lines
2.6 KiB
PHP
92 lines
2.6 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 GitHubController extends Controller
|
|
{
|
|
use PreservesUtmParameters;
|
|
|
|
public function redirect(Request $request): RedirectResponse
|
|
{
|
|
$this->storeUtmParameters($request);
|
|
|
|
return Socialite::driver('github')
|
|
->scopes(['read:user', 'user:email'])
|
|
->redirect();
|
|
}
|
|
|
|
public function callback(): RedirectResponse
|
|
{
|
|
try {
|
|
$githubUser = Socialite::driver('github')->user();
|
|
} catch (\Exception) {
|
|
return redirect()->route('login');
|
|
}
|
|
|
|
$user = User::where('github_id', (string) $githubUser->getId())
|
|
->when($githubUser->getEmail(), fn ($query, $email) => $query->orWhere('email', $email))
|
|
->first();
|
|
|
|
if ($user) {
|
|
return $this->loginExistingUser($user, (string) $githubUser->getId());
|
|
}
|
|
|
|
if (! $githubUser->getEmail()) {
|
|
return redirect()->route('login')->withErrors([
|
|
'email' => __('auth.github_email_unavailable'),
|
|
]);
|
|
}
|
|
|
|
return $this->registerNewUser($githubUser);
|
|
}
|
|
|
|
private function loginExistingUser(User $user, string $githubId): RedirectResponse
|
|
{
|
|
if (! $user->github_id) {
|
|
$user->update(['github_id' => $githubId]);
|
|
}
|
|
|
|
if (! $user->hasVerifiedEmail()) {
|
|
$user->markEmailAsVerified();
|
|
}
|
|
|
|
Auth::login($user, remember: true);
|
|
|
|
$this->retrieveUtmParameters();
|
|
|
|
return redirect()->route('app.home');
|
|
}
|
|
|
|
private function registerNewUser(\Laravel\Socialite\Contracts\User $githubUser): RedirectResponse
|
|
{
|
|
$utmParameters = $this->retrieveUtmParameters();
|
|
|
|
$user = CreateUser::execute([
|
|
'name' => $githubUser->getName() ?? $githubUser->getNickname() ?? explode('@', $githubUser->getEmail())[0],
|
|
'email' => $githubUser->getEmail(),
|
|
'github_id' => (string) $githubUser->getId(),
|
|
'email_verified_at' => now(),
|
|
'registration_ip' => request()->ip(),
|
|
], $utmParameters);
|
|
|
|
event(new Registered($user));
|
|
|
|
Auth::login($user, remember: true);
|
|
|
|
session()->flash('auth_provider', 'github');
|
|
|
|
return redirect()->route('register.success', $utmParameters);
|
|
}
|
|
}
|