-
-
Notifications
You must be signed in to change notification settings - Fork 668
Add GitHub account connection and disconnection functionality #1375
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
theHocineSaad
wants to merge
5
commits into
laravelio:main
Choose a base branch
from
theHocineSaad:github-account-connection-and-disconnection
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2aa260e
Add GitHub account connection and disconnection functionality
theHocineSaad be59481
Add tests for GitHub account connection and disconnection functionality
theHocineSaad a984f5a
Add password check before disconnecting GitHub account
theHocineSaad 47fe17e
Merge branch 'laravelio:main' into github-account-connection-and-disc…
theHocineSaad f7a63f6
Use Simple Icons Github icon
theHocineSaad File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
namespace App\Actions; | ||
|
||
use App\Jobs\UpdateUserIdenticonStatus; | ||
use App\Models\User; | ||
use Laravel\Socialite\Two\User as SocialiteUser; | ||
use function dispatch; | ||
|
||
final class ConnectGitHubAccount | ||
{ | ||
public function __invoke(User $user, SocialiteUser $socialiteUser): void | ||
{ | ||
$user->update([ | ||
'github_id' => $socialiteUser->getId(), | ||
'github_username' => $socialiteUser->getNickname(), | ||
]); | ||
|
||
dispatch(new UpdateUserIdenticonStatus($user)); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
namespace App\Actions; | ||
|
||
use App\Models\User; | ||
|
||
final class DisconnectGitHubAccount | ||
{ | ||
public function __invoke(User $user): void | ||
{ | ||
$user->update([ | ||
'github_id' => null, | ||
'github_username' => null, | ||
'github_has_identicon' => false, | ||
]); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\Settings; | ||
|
||
use App\Actions\DisconnectGitHubAccount; | ||
use App\Http\Controllers\Controller; | ||
use Illuminate\Auth\Middleware\Authenticate; | ||
use Illuminate\Http\RedirectResponse; | ||
|
||
final class GitHubAccountController extends Controller | ||
{ | ||
public function __construct() | ||
{ | ||
$this->middleware(Authenticate::class); | ||
} | ||
|
||
public function connect(): RedirectResponse | ||
{ | ||
session()->put('settings.github.connect.intended', true); | ||
|
||
return redirect(route('login.github')); | ||
} | ||
|
||
public function disconnect(DisconnectGitHubAccount $disconnectGitHubAccount): RedirectResponse | ||
{ | ||
$user = auth()->user(); | ||
|
||
if (!$user->password) { | ||
$this->error('You must set a password before disconnecting your GitHub account, otherwise, you will not be able to log in again.'); | ||
|
||
return redirect(route('settings.profile')); | ||
} | ||
|
||
$disconnectGitHubAccount($user); | ||
|
||
$this->success('Your GitHub account has been disconnected.'); | ||
|
||
return redirect(route('settings.profile')); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
@title('GitHub') | ||
|
||
<section aria-labelledby="github_settings_heading" class="mt-6"> | ||
<div class="shadow-sm sm:rounded-md sm:overflow-hidden"> | ||
<div class="bg-white py-6 px-4 space-y-6 sm:p-6"> | ||
<div> | ||
<h2 id="github_settings_heading" class="text-lg leading-6 font-medium text-gray-900"> | ||
GitHub Account | ||
</h2> | ||
|
||
<p class="mt-1 text-sm leading-5 text-gray-500"> | ||
Connect your GitHub account to keep your profile information in sync. | ||
</p> | ||
</div> | ||
|
||
@if (Auth::user()->hasConnectedGitHubAccount()) | ||
<div class="flex items-center justify-between flex-wrap gap-4"> | ||
<div class="space-y-1"> | ||
<span class="block text-sm font-medium text-gray-700"> | ||
Connected as | ||
</span> | ||
|
||
<a href="https://github.com/{{ Auth::user()->githubUsername() }}" class="text-lio-700 font-semibold" | ||
target="_blank" rel="noopener"> | ||
{{ '@' . Auth::user()->githubUsername() }} | ||
</a> | ||
</div> | ||
|
||
@if (Auth::user()->password) | ||
<x-forms.form method="POST" action="{{ route('settings.github.disconnect') }}"> | ||
<x-buttons.danger-button type="submit"> | ||
Disconnect GitHub | ||
</x-buttons.danger-button> | ||
</x-forms.form> | ||
@else | ||
<p class="text-sm text-red-600 mt-2"> | ||
You must set a password before disconnecting your GitHub account, otherwise, you will not be able to log in again. | ||
</p> | ||
@endif | ||
</div> | ||
@else | ||
<div class="flex items-center justify-between flex-wrap gap-4"> | ||
<p class="text-sm text-gray-600"> | ||
Connecting your GitHub account will automatically populate your GitHub username and use your | ||
GitHub profile image. | ||
</p> | ||
</div> | ||
@endif | ||
</div> | ||
|
||
@if (!Auth::user()->hasConnectedGitHubAccount()) | ||
<x-forms.form id="github_settings_form" method="POST" action="{{ route('settings.github.connect') }}"> | ||
<div class="px-4 py-3 bg-gray-50 text-right sm:px-6"> | ||
<x-buttons.primary-button type="submit"> | ||
Connect GitHub | ||
</x-buttons.primary-button> | ||
</div> | ||
</x-forms.form> | ||
@endif | ||
</div> | ||
</section> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
<?php | ||
|
||
use App\Jobs\UpdateUserIdenticonStatus; | ||
use App\Models\User; | ||
use Illuminate\Foundation\Testing\RefreshDatabase; | ||
use Illuminate\Support\Facades\Queue; | ||
use Laravel\Socialite\Contracts\Provider; | ||
use Laravel\Socialite\Facades\Socialite; | ||
use Laravel\Socialite\Two\User as SocialiteUser; | ||
use Tests\TestCase; | ||
|
||
uses(TestCase::class); | ||
uses(RefreshDatabase::class); | ||
|
||
test('users can start connecting their GitHub account from settings', function () { | ||
$user = $this->login(); | ||
|
||
$response = $this->actingAs($user)->post('/settings/github/connect'); | ||
|
||
$response->assertRedirect(route('login.github')); | ||
|
||
expect(session('settings.github.connect.intended'))->toBeTrue(); | ||
}); | ||
|
||
test('users can disconnect their GitHub account from settings', function () { | ||
$user = $this->login([ | ||
'github_id' => '11405387', | ||
'github_username' => 'theHocineSaad', | ||
'github_has_identicon' => true, | ||
]); | ||
|
||
$response = $this->actingAs($user)->post('/settings/github/disconnect'); | ||
|
||
$response->assertRedirect(route('settings.profile')); | ||
$response->assertSessionHas('success', 'Your GitHub account has been disconnected.'); | ||
|
||
$user->refresh(); | ||
|
||
expect($user->github_id)->toBeNull(); | ||
expect($user->github_username)->toBeNull(); | ||
expect($user->github_has_identicon)->toBeFalse(); | ||
}); | ||
|
||
test('users can connect their GitHub account after returning from GitHub', function () { | ||
Queue::fake(); | ||
|
||
$user = $this->login([ | ||
'github_id' => null, | ||
'github_username' => null, | ||
]); | ||
|
||
$socialiteUser = fakeSocialiteUser('11405387', 'theHocineSaad'); | ||
|
||
mockGitHubProvider($socialiteUser); | ||
|
||
$this->withSession(['settings.github.connect.intended' => true]); | ||
|
||
$response = $this->actingAs($user)->get('/auth/github'); | ||
|
||
$response->assertRedirect(route('settings.profile')); | ||
$response->assertSessionHas('success', 'Your GitHub account has been connected.'); | ||
|
||
$user->refresh(); | ||
|
||
expect($user->github_id)->toBe('11405387'); | ||
expect($user->github_username)->toBe('theHocineSaad'); | ||
|
||
Queue::assertPushed(UpdateUserIdenticonStatus::class); | ||
}); | ||
|
||
test('users cannot connect a GitHub account that belongs to another user', function () { | ||
Queue::fake(); | ||
|
||
User::factory()->create([ | ||
'github_id' => '11405387', | ||
'github_username' => 'theHocineSaad', | ||
]); | ||
|
||
$user = $this->login([ | ||
'github_id' => null, | ||
'github_username' => null, | ||
]); | ||
|
||
$socialiteUser = fakeSocialiteUser('11405387', 'theHocineSaad'); | ||
|
||
mockGitHubProvider($socialiteUser); | ||
|
||
$this->withSession(['settings.github.connect.intended' => true]); | ||
|
||
$response = $this->actingAs($user)->get('/auth/github'); | ||
|
||
$response->assertRedirect(route('settings.profile')); | ||
$response->assertSessionHas('error', 'This GitHub account is already connected to another user.'); | ||
|
||
$user->refresh(); | ||
|
||
expect($user->github_id)->toBeNull(); | ||
expect($user->github_username)->toBeNull(); | ||
|
||
Queue::assertNothingPushed(); | ||
}); | ||
|
||
function fakeSocialiteUser(string $id, string $nickname): SocialiteUser | ||
{ | ||
return tap(new SocialiteUser()) | ||
->setRaw([ | ||
'id' => $id, | ||
'login' => $nickname, | ||
]) | ||
->map([ | ||
'id' => $id, | ||
'nickname' => $nickname, | ||
]); | ||
} | ||
|
||
function mockGitHubProvider(SocialiteUser $user): void | ||
{ | ||
$provider = Mockery::mock(Provider::class); | ||
$provider->shouldReceive('user')->once()->andReturn($user); | ||
|
||
Socialite::shouldReceive('driver')->once()->with('github')->andReturn($provider); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.