From 368268a76106653e8468a8a2f131105961baa7c3 Mon Sep 17 00:00:00 2001 From: Joel Butcher Date: Tue, 2 Mar 2021 10:30:32 +0000 Subject: [PATCH] added new updates connected account interface and implementation --- src/Contracts/UpdatesConnectedAccounts.php | 20 ++++++++++ src/Http/Controllers/OAuthController.php | 18 +++++++-- src/Socialstream.php | 12 ++++++ .../Socialstream/CreateConnectedAccount.php | 28 ++----------- .../Socialstream/UpdateConnectedAccount.php | 40 +++++++++++++++++++ .../Providers/SocialstreamServiceProvider.php | 2 + 6 files changed, 93 insertions(+), 27 deletions(-) create mode 100644 src/Contracts/UpdatesConnectedAccounts.php create mode 100644 stubs/app/Actions/Socialstream/UpdateConnectedAccount.php diff --git a/src/Contracts/UpdatesConnectedAccounts.php b/src/Contracts/UpdatesConnectedAccounts.php new file mode 100644 index 00000000..ffb2bba4 --- /dev/null +++ b/src/Contracts/UpdatesConnectedAccounts.php @@ -0,0 +1,20 @@ +guard = $guard; $this->createsUser = $createsUser; $this->createsConnectedAccounts = $createsConnectedAccounts; + $this->updatesConnectedAccounts = $updatesConnectedAccounts; $this->invalidStateHandler = $invalidStateHandler; } @@ -173,11 +183,13 @@ public function handleProviderCallback(Request $request, string $provider) return redirect(config('fortify.home')); } - $this->guard->login($account->user, Socialstream::hasRememberSessionFeatures()); + $this->guard->login($user = $account->user, Socialstream::hasRememberSessionFeatures()); + + $this->updatesConnectedAccounts->update($user, $account, $provider, $providerAccount); - $account->user->forceFill([ + $user->forceFill([ 'current_connected_account_id' => $account->id, - ]); + ])->save(); return redirect(config('fortify.home')); } diff --git a/src/Socialstream.php b/src/Socialstream.php index bca52f17..362222ad 100644 --- a/src/Socialstream.php +++ b/src/Socialstream.php @@ -7,6 +7,7 @@ use JoelButcher\Socialstream\Contracts\GeneratesProviderRedirect; use JoelButcher\Socialstream\Contracts\HandlesInvalidState; use JoelButcher\Socialstream\Contracts\SetsUserPasswords; +use JoelButcher\Socialstream\Contracts\UpdatesConnectedAccounts; class Socialstream { @@ -258,6 +259,17 @@ public static function createConnectedAccountsUsing(string $class) return app()->singleton(CreatesConnectedAccounts::class, $class); } + /** + * Register a class / callback that should be used to update connected accounts. + * + * @param string $class + * @return void + */ + public static function updateConnectedAccountsUsing(string $class) + { + return app()->singleton(UpdatesConnectedAccounts::class, $class); + } + /** * Register a class / callback that should be used to set user passwords. * diff --git a/stubs/app/Actions/Socialstream/CreateConnectedAccount.php b/stubs/app/Actions/Socialstream/CreateConnectedAccount.php index 8996e7b4..5bdbad58 100644 --- a/stubs/app/Actions/Socialstream/CreateConnectedAccount.php +++ b/stubs/app/Actions/Socialstream/CreateConnectedAccount.php @@ -2,9 +2,8 @@ namespace App\Actions\Socialstream; -use App\Models\ConnectedAccount; -use App\Models\User; use JoelButcher\Socialstream\Contracts\CreatesConnectedAccounts; +use JoelButcher\Socialstream\Socialstream; use Laravel\Socialite\Contracts\User as ProviderUser; class CreateConnectedAccount implements CreatesConnectedAccounts @@ -12,33 +11,14 @@ class CreateConnectedAccount implements CreatesConnectedAccounts /** * Create a connected account for a given user. * - * @param \App\Models\User $user + * @param mixed $user * @param string $provider * @param \Laravel\Socialite\Contracts\User $providerUser * @return \JoelButcher\Socialstream\ConnectedAccount */ - public function create(User $user, string $provider, ProviderUser $providerUser) + public function create($user, string $provider, ProviderUser $providerUser) { - if ($user->hasTokenFor($provider)) { - $connectedAccount = $user->getTokenFor($provider); - - $connectedAccount->forceFill([ - 'provider' => strtolower($provider), - 'provider_id' => $providerUser->getId(), - 'name' => $providerUser->getName(), - 'nickname' => $providerUser->getNickname(), - 'email' => $providerUser->getEmail(), - 'avatar_path' => $providerUser->getAvatar(), - 'token' => $providerUser->token, - 'secret' => $providerUser->tokenSecret ?? null, - 'refresh_token' => $providerUser->refreshToken ?? null, - 'expires_at' => property_exists($providerUser, 'expiresIn') ? now()->addSeconds($providerUser->expiresIn) : null, - ])->save(); - - return $connectedAccount; - } - - return ConnectedAccount::forceCreate([ + return Socialstream::connectedAccountModel()::forceCreate([ 'user_id' => $user->id, 'provider' => strtolower($provider), 'provider_id' => $providerUser->getId(), diff --git a/stubs/app/Actions/Socialstream/UpdateConnectedAccount.php b/stubs/app/Actions/Socialstream/UpdateConnectedAccount.php new file mode 100644 index 00000000..f9944d93 --- /dev/null +++ b/stubs/app/Actions/Socialstream/UpdateConnectedAccount.php @@ -0,0 +1,40 @@ +authorize('update', $connectedAccount); + + $connectedAccount->forceFill([ + 'provider' => strtolower($provider), + 'provider_id' => $providerUser->getId(), + 'name' => $providerUser->getName(), + 'nickname' => $providerUser->getNickname(), + 'email' => $providerUser->getEmail(), + 'avatar_path' => $providerUser->getAvatar(), + 'token' => $providerUser->token, + 'secret' => $providerUser->tokenSecret ?? null, + 'refresh_token' => $providerUser->refreshToken ?? null, + 'expires_at' => property_exists($providerUser, 'expiresIn') ? now()->addSeconds($providerUser->expiresIn) : null, + ])->save(); + + return $connectedAccount; + } +} \ No newline at end of file diff --git a/stubs/app/Providers/SocialstreamServiceProvider.php b/stubs/app/Providers/SocialstreamServiceProvider.php index cdc0e3b8..7882b6c6 100644 --- a/stubs/app/Providers/SocialstreamServiceProvider.php +++ b/stubs/app/Providers/SocialstreamServiceProvider.php @@ -6,6 +6,7 @@ use App\Actions\Socialstream\CreateUserFromProvider; use App\Actions\Socialstream\HandleInvalidState; use App\Actions\Socialstream\SetUserPassword; +use App\Actions\Socialstream\UpdateConnectedAccount; use Illuminate\Support\ServiceProvider; use JoelButcher\Socialstream\Actions\GenerateRedirectForProvider; use JoelButcher\Socialstream\Socialstream; @@ -35,6 +36,7 @@ public function boot() Socialstream::createUsersFromProviderUsing(CreateUserFromProvider::class); Socialstream::createConnectedAccountsUsing(CreateConnectedAccount::class); + Socialstream::updateConnectedAccountsUsing(UpdateConnectedAccount::class); Socialstream::setUserPasswordsUsing(SetUserPassword::class); Socialstream::handlesInvalidStateUsing(HandleInvalidState::class); Socialstream::generatesProvidersRedirectsUsing(GenerateRedirectForProvider::class);