Skip to content

Commit

Permalink
fix: undefined route errors when redirecting authenticated uses after…
Browse files Browse the repository at this point in the history
… oauth
  • Loading branch information
joelbutcher committed Oct 30, 2023
1 parent 6b371b2 commit 5a55d46
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 12 deletions.
39 changes: 27 additions & 12 deletions src/Actions/AuthenticateOAuthCallback.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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));
}

/**
Expand Down
3 changes: 3 additions & 0 deletions src/Actions/HandleOAuthCallbackErrors.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
Expand Down

0 comments on commit 5a55d46

Please sign in to comment.