From 13b5c68e1312e5e5aa54f10e5874438bd894e872 Mon Sep 17 00:00:00 2001 From: Joel Butcher Date: Thu, 12 Oct 2023 12:25:01 +0100 Subject: [PATCH] fix: filament config and auth --- config/filament.php | 12 ++ .../Filament/AuthenticateOauthCallback.php | 174 +----------------- src/SocialstreamServiceProvider.php | 11 +- 3 files changed, 27 insertions(+), 170 deletions(-) create mode 100644 config/filament.php diff --git a/config/filament.php b/config/filament.php new file mode 100644 index 00000000..749b6866 --- /dev/null +++ b/config/filament.php @@ -0,0 +1,12 @@ + ['web'], + 'prompt' => 'Or Login Via', + 'providers' => [ + // Providers::github(), + ], +]; diff --git a/src/Actions/Auth/Filament/AuthenticateOauthCallback.php b/src/Actions/Auth/Filament/AuthenticateOauthCallback.php index 2de40fed..ee6965be 100644 --- a/src/Actions/Auth/Filament/AuthenticateOauthCallback.php +++ b/src/Actions/Auth/Filament/AuthenticateOauthCallback.php @@ -33,177 +33,17 @@ public function __construct( public function authenticate(string $provider, ProviderUser $providerAccount): Response|RedirectResponse { - $account = Socialstream::findConnectedAccountForProviderAndId($provider, $providerAccount->getId()); + $user = auth()->user(); - // Authenticated... - if (! is_null($user = auth()->user())) { - return $this->alreadyAuthenticated($user, $account, $provider, $providerAccount); + if (! $user) { + $user = Socialstream::newUserModel()->where('email', $providerAccount->getEmail())->first() + ?? $this->createsUser->create($provider, $providerAccount); } - // Registration... - $previousUrl = session()->get('socialstream.previous_url'); + ($account = Socialstream::findConnectedAccountForProviderAndId($provider, $providerAccount->getId())) + ? $this->updatesConnectedAccounts->update($user, $account, $provider, $providerAccount) + : $this->createsConnectedAccounts->create($user, $provider, $providerAccount); - if ( - ! $account && - (Features::hasCreateAccountOnFirstLoginFeatures() && $previousUrl === route('filament.admin.auth.login')) - ) { - $user = Socialstream::newUserModel()->where('email', $providerAccount->getEmail())->first(); - - if ($user) { - return $this->alreadyRegistered($user, $account, $provider, $providerAccount); - } - - return $this->register($provider, $providerAccount); - } - - if (! Features::hasCreateAccountOnFirstLoginFeatures() && ! $account) { - $messageBag = new MessageBag; - $messageBag->add( - 'socialstream', - __('An account with this :Provider sign in was not found. Please register or try a different sign in method.', ['provider' => Providers::name($provider)]) - ); - - return redirect()->route('filament.admin.auth.login')->withErrors( - $messageBag - ); - } - - if (Features::hasCreateAccountOnFirstLoginFeatures() && ! $account) { - if (Socialstream::newUserModel()->where('email', $providerAccount->getEmail())->exists()) { - $messageBag = new MessageBag; - $messageBag->add( - 'socialstream', - __('An account with that email address already exists. Please login to connect your :Provider account.', ['provider' => Providers::name($provider)]) - ); - - return redirect()->route('filament.admin.auth.login')->withErrors( - $messageBag - ); - } - - $user = $this->createsUser->create($provider, $providerAccount); - - return $this->login($user); - } - - if (! Features::hasCreateAccountOnFirstLoginFeatures() && ! $account) { - $messageBag = new MessageBag; - $messageBag->add( - 'socialstream', - __('An account with this :Provider sign in was not found. Please register or try a different sign in method.', ['provider' => Providers::name($provider)]) - ); - - return redirect()->route('filament.admin.auth.login')->withErrors( - $messageBag - ); - } - - if (Features::hasCreateAccountOnFirstLoginFeatures() && ! $account) { - if (Socialstream::newUserModel()->where('email', $providerAccount->getEmail())->exists()) { - $messageBag = new MessageBag; - $messageBag->add( - 'socialstream', - __('An account with that email address already exists. Please login to connect your :Provider account.', ['provider' => Providers::name($provider)]) - ); - - return redirect()->route('filament.admin.auth.login')->withErrors( - $messageBag - ); - } - - $user = $this->createsUser->create($provider, $providerAccount); - - return $this->login($user); - } - - $user = $account->user; - - $this->updatesConnectedAccounts->update($user, $account, $provider, $providerAccount); - - return $this->login($user); - } - - /** - * Handle connection of accounts for an already authenticated user. - */ - protected function alreadyAuthenticated(Authenticatable $user, ?ConnectedAccount $account, string $provider, ProviderUser $providerAccount): RedirectResponse - { - // Connect the account to the user. - if (! $account) { - $this->createsConnectedAccounts->create($user, $provider, $providerAccount); - - return redirect()->route('profile')->with( - 'status', __('You have successfully connected :Provider to your account.', ['provider' => Providers::name($provider)]) - ); - } - - if ($account->user_id !== $user->id) { - return redirect()->route('profile')->withErrors( - ['callback' => __('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::name($provider)])] - ); - } - - // Account already connected - return redirect()->route('profile')->withErrors( - ['callback' => __('This :Provider sign in account is already associated with your user.', ['provider' => Providers::name($provider)])] - ); - } - - /** - * Handle when a user is already registered. - */ - protected function alreadyRegistered(Authenticatable $user, ?ConnectedAccount $account, string $provider, ProviderUser $providerAccount): RedirectResponse - { - if (Features::hasLoginOnRegistrationFeatures()) { - // The user exists, but they're not registered with the given provider. - if (! $account) { - $this->createsConnectedAccounts->create($user, $provider, $providerAccount); - } - - return $this->login($user); - } - - $messageBag = new MessageBag; - $messageBag->add('socialstream', __('An account with that :Provider sign in already exists, please login.', ['provider' => Providers::name($provider)])); - - return redirect()->route('filament.admin.auth.login')->withErrors($messageBag); - } - - /** - * Handle the registration of a new user. - */ - protected function register(string $provider, ProviderUser $providerAccount): RedirectResponse - { - if (! $providerAccount->getEmail()) { - $messageBag = new MessageBag; - $messageBag->add( - 'socialstream', - __('No email address is associated with this :Provider account. Please try a different account.', ['provider' => Providers::name($provider)]) - ); - - return redirect()->route('filament.admin.auth.login')->withErrors($messageBag); - } - - if (Socialstream::newUserModel()->where('email', $providerAccount->getEmail())->exists()) { - $messageBag = new MessageBag; - $messageBag->add( - 'socialstream', - __('An account with that email address already exists. Please login to connect your :Provider account.', ['provider' => Providers::name($provider)]) - ); - - return redirect()->route('filament.admin.auth.login')->withErrors($messageBag); - } - - $user = $this->createsUser->create($provider, $providerAccount); - - return $this->login($user); - } - - /** - * Authenticate the given user and return a login response. - */ - protected function login(Authenticatable $user): RedirectResponse - { $this->guard->login($user, Socialstream::hasRememberSessionFeatures()); return redirect()->route('filament.admin.pages.dashboard'); diff --git a/src/SocialstreamServiceProvider.php b/src/SocialstreamServiceProvider.php index b97b92ef..f95a7e3a 100644 --- a/src/SocialstreamServiceProvider.php +++ b/src/SocialstreamServiceProvider.php @@ -67,9 +67,14 @@ public function boot(): void } if ($this->app->runningInConsole()) { - $this->publishes([ - __DIR__.'/../config/socialstream.php' => config_path('socialstream.php'), - ], 'socialstream-config'); + match (true) { + $this->hasComposerPackage('filament/filament') => $this->publishes([ + __DIR__.'/../config/filament.php' => config_path('socialstream.php'), + ], 'socialstream-config'), + default => $this->publishes([ + __DIR__.'/../config/socialstream.php' => config_path('socialstream.php'), + ], 'socialstream-config') + }; } }