Skip to content

Commit

Permalink
Merge pull request #58 from chrisreedio/feature/oauth-scopes
Browse files Browse the repository at this point in the history
✨ feat: Added support for custom scopes in social providers
  • Loading branch information
chrisreedio committed May 4, 2024
2 parents 6256da4 + ef073aa commit 49fce43
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
stability: [prefer-lowest, prefer-stable]
include:
- laravel: 10.*
testbench: 8.*
testbench: ^8.14
carbon: ^2.63
- laravel: 11.*
testbench: 9.*
Expand Down
21 changes: 18 additions & 3 deletions src/Http/Controllers/SocialmentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@
use ChrisReedIO\Socialment\Exceptions\AbortedLoginException;
use ChrisReedIO\Socialment\Facades\Socialment;
use ChrisReedIO\Socialment\Models\ConnectedAccount;
use ChrisReedIO\Socialment\SocialmentPlugin;
use Exception;
use Filament\Facades\Filament;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Session;
use JetBrains\PhpStorm\Deprecated;
use Laravel\Socialite\Facades\Socialite;
use Laravel\Socialite\Two\AbstractProvider;
use Laravel\Socialite\Two\InvalidStateException;
use Symfony\Component\HttpFoundation\RedirectResponse;

Expand All @@ -25,15 +28,15 @@ class SocialmentController extends BaseController
*/
public function redirect(string $provider): RedirectResponse
{
return Socialite::driver($provider)->redirect();
return $this->getProviderRedirect($provider);
}

public function redirectSpa(string $provider): RedirectResponse
{
// Store the referring url in the session
request()->session()->put('socialment.intended.url', request()->headers->get('referer'));

return Socialite::driver($provider)->redirect();
return $this->getProviderRedirect($provider);
}

public function redirectPanel(string $provider, string $panelId): RedirectResponse
Expand All @@ -43,7 +46,19 @@ public function redirectPanel(string $provider, string $panelId): RedirectRespon
request()->session()->put('socialment.intended.url', $referer);
}

return Socialite::driver($provider)->redirect();
return $this->getProviderRedirect($provider);
}

private function getProviderRedirect(string $providerName): \Illuminate\Http\RedirectResponse
{
/** @var AbstractProvider $provider */
$provider = Socialite::driver($providerName);
$providerConfig = App::make(SocialmentPlugin::class)->getProvider($providerName);
if (! empty($providerConfig['scopes'])) {
$provider->scopes($providerConfig['scopes']);
}

return $provider->redirect();
}

public function callback(string $provider): RedirectResponse
Expand Down
8 changes: 7 additions & 1 deletion src/SocialmentPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ public function getProviders(): array
return array_merge(config('socialment.providers'), $this->providers);
}

public function getProvider(string $provider): array
{
return $this->getProviders()[$provider];
}

public function register(Panel $panel): void
{
$panel->renderHook('panels::auth.login.form.before', function () {
Expand Down Expand Up @@ -217,11 +222,12 @@ public function executePostLogin(ConnectedAccount $account): void
}
}

public function registerProvider(string $provider, string $icon, string $label): static
public function registerProvider(string $provider, string $icon, string $label, array $scopes = []): static
{
$this->providers[$provider] = [
'icon' => $icon,
'label' => $label,
'scopes' => $scopes,
];

return $this;
Expand Down
2 changes: 1 addition & 1 deletion src/SocialmentServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function configurePackage(Package $package): void

public function packageRegistered(): void
{
//
$this->app->singleton(SocialmentPlugin::class, fn () => new SocialmentPlugin());
}

public function packageBooted(): void
Expand Down

0 comments on commit 49fce43

Please sign in to comment.