ailove-dev/abstract-social-bundle
Installation
composer require ailove-dev/abstract-social-bundle
Add to config/bundles.php (Symfony) or config/app.php (Laravel via bridge):
AiloveDev\AbstractSocialBundle\AbstractSocialBundle::class => ['all' => true],
Configuration Publish the default config:
php artisan vendor:publish --provider="AiloveDev\AbstractSocialBundle\AbstractSocialBundle" --tag="config"
Edit config/abstract_social.php to define supported providers (e.g., facebook, google).
First Use Case
Inject the SocialManager into a controller/service:
use AiloveDev\AbstractSocialBundle\Manager\SocialManager;
public function __construct(private SocialManager $socialManager) {}
public function handleLogin(Request $request) {
$provider = $this->socialManager->getProvider('facebook');
$user = $provider->getUser(); // Returns authenticated user data
// Proceed with user registration/login
}
Provider Integration
config/abstract_social.php:
'providers' => [
'facebook' => [
'client_id' => env('FACEBOOK_CLIENT_ID'),
'client_secret' => env('FACEBOOK_SECRET'),
'redirect_uri' => env('FACEBOOK_REDIRECT_URI'),
],
],
SocialManager to dynamically fetch providers:
$provider = $socialManager->getProvider('facebook');
$authUrl = $provider->getAuthorizationUrl(); // Generate OAuth URL
User Data Handling
$userData = $provider->getUser();
// Example output: ['id' => '123', 'name' => 'John Doe', 'email' => 'john@example.com']
$user = User::firstOrCreate(
['email' => $userData['email']],
['name' => $userData['name']]
);
Event Listeners
SocialAuthSuccess):
// In EventServiceProvider
protected $listen = [
'AiloveDev\AbstractSocialBundle\Events\SocialAuthSuccess' => [
'App\Listeners\HandleSocialLogin',
],
];
Custom Provider Abstraction
Extend AiloveDev\AbstractSocialBundle\Provider\AbstractProvider for unsupported services:
class TwitterProvider extends AbstractProvider {
protected $name = 'twitter';
// Override auth logic here
}
Register in config:
'providers' => [
'twitter' => \App\Providers\TwitterProvider::class,
],
Stateful Authentication
Use the SocialManager to store/validate state tokens:
$state = $socialManager->generateState();
// Store in session: session(['social_state' => $state])
$provider->setState($state); // Validate on callback
Provider Initialization
getProvider() returns null for undefined providers.config/abstract_social.php and their dependencies (e.g., league/oauth2-server) are installed.State Validation
$provider->setState() and compare on callback:
if ($provider->validateState(session('social_state'))) {
// Proceed
}
Configuration Overrides
class CustomFacebookProvider extends FacebookProvider {
protected $scopes = ['email', 'public_profile', 'user_friends'];
}
Enable Logging
Add to config/abstract_social.php:
'debug' => env('APP_DEBUG', false),
Logs OAuth flows to storage/logs/abstract_social.log.
Provider-Specific Errors
try {
$user = $provider->getUser();
} catch (\League\OAuth2\Client\Provider\Exception\IdentityProviderException $e) {
\Log::error($e->getResponse()->getBody());
}
Custom User Mappers
Override mapUserData() in a provider to transform raw data:
protected function mapUserData(array $data): array {
return [
'id' => $data['id'],
'name' => $data['name'],
'avatar' => $data['picture']['data']['url'],
];
}
Post-Auth Hooks
Dispatch events in your provider’s getUser() method:
$user = $this->mapUserData($data);
event(new SocialAuthSuccess($this->name, $user));
Rate Limiting
Implement AiloveDev\AbstractSocialBundle\Contracts\RateLimitable for providers with API limits:
class GitHubProvider extends AbstractProvider implements RateLimitable {
public function isRateLimited(): bool {
return $this->httpClient->getLastResponse()->getStatusCode() === 403;
}
}
How can I help you explore Laravel packages today?