Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.x] Fix undefined routes for authenticated users in Filament #309

Merged
merged 1 commit into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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