Skip to content

Commit

Permalink
Merge pull request #46 from adamwathan/aw-update-existing-identity
Browse files Browse the repository at this point in the history
Update existing oauth_identities instead of flushing all and creating new
  • Loading branch information
adamwathan committed Mar 21, 2015
2 parents 6ad6d26 + 619288c commit cece734
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
19 changes: 12 additions & 7 deletions src/Authenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ protected function getUser($provider, $details)
protected function updateUser($user, $provider, $details)
{
$this->users->store($user);
$this->updateAccessToken($user, $provider, $details);
$this->storeProviderIdentity($user, $provider, $details);
}

protected function getExistingUser($provider, $details)
Expand All @@ -49,18 +49,23 @@ protected function getExistingUser($provider, $details)
return $this->users->findByIdentity($identity);
}

protected function updateAccessToken($user, $provider, ProviderUserDetails $details)
protected function storeProviderIdentity($user, $provider, ProviderUserDetails $details)
{
$this->flushAccessTokens($user, $provider);
$this->addAccessToken($user, $provider, $details);
if ($this->identities->userExists($provider, $details)) {
$this->updateProviderIdentity($provider, $details);
} else {
$this->addProviderIdentity($user, $provider, $details);
}
}

protected function flushAccessTokens($user, $provider)
protected function updateProviderIdentity($provider, ProviderUserDetails $details)
{
$this->identities->flush($user, $provider);
$identity = $this->identities->getByProvider($provider, $details);
$identity->access_token = $details->accessToken;
$this->identities->store($identity);
}

protected function addAccessToken($user, $provider, ProviderUserDetails $details)
protected function addProviderIdentity($user, $provider, ProviderUserDetails $details)
{
$identity = new OAuthIdentity;
$identity->user_id = $user->getKey();
Expand Down
3 changes: 2 additions & 1 deletion tests/AuthenticatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use Mockery as M;
use AdamWathan\EloquentOAuth\Authenticator;
use AdamWathan\EloquentOAuth\OAuthIdentity;

class AuthenticatorTest extends PHPUnit_Framework_TestCase
{
Expand Down Expand Up @@ -36,7 +37,7 @@ public function test_login_uses_existing_user_if_matching_user_exists()
$identities = M::mock('AdamWathan\\EloquentOAuth\\IdentityStore')->shouldIgnoreMissing();

$userDetails = M::mock('AdamWathan\\EloquentOAuth\\ProviderUserDetails');
$identity = M::mock('AdamWathan\\EloquentOAuth\\OAuthIdentity');
$identity = new OAuthIdentity;

$user = M::mock('stdClass')->shouldIgnoreMissing();

Expand Down

0 comments on commit cece734

Please sign in to comment.