Skip to content

Commit

Permalink
[5.x] Bugfix redirect on OAuth Link Failed (#354)
Browse files Browse the repository at this point in the history
* fix route not found

* Add a test
  • Loading branch information
joelbutcher authored Apr 2, 2024
1 parent 993b03b commit 4f756c6
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Http/Responses/OAuthProviderLinkFailedResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class OAuthProviderLinkFailedResponse implements OAuthProviderLinkFailedResponse
public function toResponse($request): RedirectResponse
{
if (class_exists(Jetstream::class)) {
return redirect()->to('profile.show');
return redirect()->route('profile.show');
}

return Socialstream::redirects('provider-link-failed')
Expand Down
51 changes: 51 additions & 0 deletions tests/Feature/SocialstreamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,3 +189,54 @@ public function generate(string $provider): RedirectResponse
'email' => '[email protected]',
]);
});

test('linking and already linked provider fails', function () {
// Arrange - existing user with account linked
$this->actingAs(User::create([
'name' => 'Joel Butcher',
'email' => '[email protected]',
'password' => Hash::make('password'),
]));

$provider = mock(GithubProvider::class);
$provider->shouldReceive('user')->andReturn((new SocialiteUser())
->map([
'id' => $githubId = fake()->numerify('########'),
'nickname' => 'joel',
'name' => 'Joel',
'email' => '[email protected]',
'avatar' => null,
'avatar_original' => null,
])
->setToken('user-token')
->setRefreshToken('refresh-token')
->setExpiresIn(3600));

Socialite::shouldReceive('driver')->with('github')->andReturn($provider);

get('http://localhost/oauth/github/callback');

$this->assertAuthenticated();
$this->assertDatabaseHas('connected_accounts', [
'provider' => 'github',
'provider_id' => $githubId,
'email' => '[email protected]',
]);

// Arrange - new user
$this->actingAs(User::create([
'name' => 'Joel Butcher',
'email' => '[email protected]',
'password' => Hash::make('password'),
]));

// Act
get('http://localhost/oauth/github/callback')
->assertRedirectToRoute('profile.show')
->assertSessionHas([
'flash' => [
'banner' => 'It looks like this GitHub account is used by another user. Please log in.',
'bannerStyle' => 'danger'
]
]);
});

0 comments on commit 4f756c6

Please sign in to comment.