From 5a55d466f29f5d9c3f5801afa8d5e99f92c28030 Mon Sep 17 00:00:00 2001 From: Joel Butcher Date: Mon, 30 Oct 2023 11:46:24 +0000 Subject: [PATCH] fix: undefined route errors when redirecting authenticated uses after oauth --- src/Actions/AuthenticateOAuthCallback.php | 39 ++++++++++++++++------- src/Actions/HandleOAuthCallbackErrors.php | 3 ++ 2 files changed, 30 insertions(+), 12 deletions(-) diff --git a/src/Actions/AuthenticateOAuthCallback.php b/src/Actions/AuthenticateOAuthCallback.php index 5adab162..53681417 100644 --- a/src/Actions/AuthenticateOAuthCallback.php +++ b/src/Actions/AuthenticateOAuthCallback.php @@ -20,6 +20,7 @@ use JoelButcher\Socialstream\Socialstream; use Laravel\Fortify\Contracts\LoginResponse; use Laravel\Fortify\Features as FortifyFeatures; +use Laravel\Jetstream\Jetstream; use Laravel\Socialite\Contracts\User as ProviderUser; class AuthenticateOAuthCallback implements AuthenticatesOAuthCallback @@ -97,25 +98,39 @@ class_exists(FortifyFeatures::class) && */ protected function alreadyAuthenticated(Authenticatable $user, ?ConnectedAccount $account, string $provider, ProviderUser $providerAccount): RedirectResponse { + // Get the route + $route = match(true) { + Route::has('filament.admin.home') => route('filament.admin.home'), + Route::has('filament.home') => route('filament.home'), + $this->hasComposerPackage('laravel/breeze') => match(true) { + Route::has('profile.show') => route('profile.show'), + Route::has('profile.edit') => route('profile.edit'), + Route::has('profile') => route('profile'), + }, + Route::has('profile.show') => route('profile.show'), + Route::has('dashboard') => route('dashboard'), + Route::has('home') => route('home'), + default => RouteServiceProvider::HOME + }; + // Connect the account to the user. if (! $account) { $this->createsConnectedAccounts->create($user, $provider, $providerAccount); - return redirect()->route('profile.show')->banner( - __('You have successfully connected :Provider to your account.', ['provider' => Providers::name($provider)]) - ); - } + $status = __('You have successfully connected :Provider to your account.', ['provider' => Providers::name($provider)]); - if ($account->user_id !== $user->id) { - return redirect()->route('profile.show')->dangerBanner( - __('This :Provider sign in account is already associated with another user. Please log in with that user or connect a different :Provider account.', ['provider' => Providers::buttonLabel($provider)]) - ); + return class_exists(Jetstream::class) + ? redirect()->to($route)->banner($status) + : redirect()->to($route)->with('status', $status); } - // Account already connected - return redirect()->route('profile.show')->dangerBanner( - __('This :Provider sign in account is already associated with your user.', ['provider' => Providers::buttonLabel($provider)]) - ); + $error = $account->user_id !== $user->id + ? __('This :Provider sign in account is already associated with another user. Please log in with that user or connect a different :Provider account.', ['provider' => Providers::buttonLabel($provider)]) + : __('This :Provider sign in account is already associated with your user.', ['provider' => Providers::buttonLabel($provider)]); + + return class_exists(Jetstream::class) + ? redirect()->to($route)->dangerBanner($error) + : redirect()->to($route)->withErrors((new MessageBag)->add('socialstream', $error)); } /** diff --git a/src/Actions/HandleOAuthCallbackErrors.php b/src/Actions/HandleOAuthCallbackErrors.php index e03f326d..8f2ac2ed 100644 --- a/src/Actions/HandleOAuthCallbackErrors.php +++ b/src/Actions/HandleOAuthCallbackErrors.php @@ -35,6 +35,9 @@ public function handle(Request $request): ?RedirectResponse $previousUrl = session()->pull('socialstream.previous_url'); return match (true) { + Route::has('filament.admin.home') && $previousUrl === route('filament.admin.home') => redirect() + ->route('filament.admin.home') + ->withErrors((new MessageBag)->add('socialstream', $error)), Route::has('filament.home') && $previousUrl === route('filament.home') => redirect() ->route('filament.home') ->withErrors((new MessageBag)->add('socialstream', $error)),