Skip to content

Commit

Permalink
added new updates connected account interface and implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Joel Butcher committed Mar 2, 2021
1 parent c5f4e08 commit 368268a
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 27 deletions.
20 changes: 20 additions & 0 deletions src/Contracts/UpdatesConnectedAccounts.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace JoelButcher\Socialstream\Contracts;

use JoelButcher\Socialstream\ConnectedAccount;
use Laravel\Socialite\Contracts\User;

interface UpdatesConnectedAccounts
{
/**
* Update a given connected account.
*
* @param mixed $user
* @param \JoelButcher\Socialstream\ConnectedAccount $connectedAccount
* @param string $provider
* @param \Laravel\Socialite\Contracts\User $providerUser
* @return \JoelButcher\Socialstream\ConnectedAccount
*/
public function update($user, ConnectedAccount $connectedAccount, string $provider, User $providerUser);
}
18 changes: 15 additions & 3 deletions src/Http/Controllers/OAuthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use JoelButcher\Socialstream\Contracts\CreatesConnectedAccounts;
use JoelButcher\Socialstream\Contracts\CreatesUserFromProvider;
use JoelButcher\Socialstream\Contracts\GeneratesProviderRedirect;
use JoelButcher\Socialstream\Contracts\UpdatesConnectedAccounts;
use JoelButcher\Socialstream\Features;
use JoelButcher\Socialstream\Socialstream;
use Laravel\Jetstream\Jetstream;
Expand Down Expand Up @@ -39,6 +40,13 @@ class OAuthController extends Controller
*/
protected $createsConnectedAccounts;

/**
* The updates connected accounts implementation.
*
* @var \JoelButcher\Socialstream\Contracts\UpdatesConnectedAccounts;
*/
protected $updatesConnectedAccounts;

/**
* The handler for Socialite's InvalidStateException.
*
Expand All @@ -56,11 +64,13 @@ public function __construct(
StatefulGuard $guard,
CreatesUserFromProvider $createsUser,
CreatesConnectedAccounts $createsConnectedAccounts,
UpdatesConnectedAccounts $updatesConnectedAccounts,
HandleInvalidState $invalidStateHandler
) {
$this->guard = $guard;
$this->createsUser = $createsUser;
$this->createsConnectedAccounts = $createsConnectedAccounts;
$this->updatesConnectedAccounts = $updatesConnectedAccounts;
$this->invalidStateHandler = $invalidStateHandler;
}

Expand Down Expand Up @@ -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'));
}
Expand Down
12 changes: 12 additions & 0 deletions src/Socialstream.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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.
*
Expand Down
28 changes: 4 additions & 24 deletions stubs/app/Actions/Socialstream/CreateConnectedAccount.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,23 @@

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
{
/**
* 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(),
Expand Down
40 changes: 40 additions & 0 deletions stubs/app/Actions/Socialstream/UpdateConnectedAccount.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace App\Actions\Socialstream;

use Illuminate\Support\Facades\Gate;
use JoelButcher\Socialstream\ConnectedAccount;
use JoelButcher\Socialstream\Contracts\UpdatesConnectedAccounts;
use Laravel\Socialite\Contracts\User;

class UpdateConnectedAccount implements UpdatesConnectedAccounts
{
/**
* Update a given connected account.
*
* @param mixed $user
* @param \JoelButcher\Socialstream\ConnectedAccount $connectedAccount
* @param string $provider
* @param \Laravel\Socialite\Contracts\User $providerUser
* @return \JoelButcher\Socialstream\ConnectedAccount
*/
public function update($user, ConnectedAccount $connectedAccount, string $provider, User $providerUser)
{
Gate::forUser($user)->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;
}
}
2 changes: 2 additions & 0 deletions stubs/app/Providers/SocialstreamServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 368268a

Please sign in to comment.