Skip to content
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

✨ feat: Added support for custom scopes in social providers #58

Merged
merged 5 commits into from
May 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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