Installation
Add the bundle to your composer.json:
composer require masando/edemy-fbbundle
Register the bundle in config/bundles.php (if using Symfony-style bundles):
return [
// ...
Masando\eDemyFbBundle\eDemyFbBundle::class => ['all' => true],
];
Configuration Publish the default config:
php artisan vendor:publish --provider="Masando\eDemyFbBundle\eDemyFbBundle" --tag="config"
Update config/edemy_fb.php with your Facebook App credentials (App ID, Secret, etc.).
First Use Case: Login with Facebook
Add the login route to your routes/web.php:
use Masando\eDemyFbBundle\Routing\FacebookController;
Route::get('/login/facebook', [FacebookController::class, 'login']);
Route::get('/login/facebook/callback', [FacebookController::class, 'callback']);
Use the FacebookAuthenticator service in a controller:
use Masando\eDemyFbBundle\Services\FacebookAuthenticator;
public function handleFacebookLogin(FacebookAuthenticator $authenticator) {
$user = $authenticator->authenticate();
// Handle user (e.g., create/update in DB, redirect)
}
Authentication Flow
return $authenticator->redirectToFacebook();
$userData = $authenticator->getUserFromFacebook();
$user = User::updateOrCreate(
['email' => $userData['email']],
[
'name' => $userData['name'],
'facebook_id' => $userData['id'],
]
);
Graph API Integration Fetch user data or publish actions:
use Masando\eDemyFbBundle\Services\FacebookGraph;
public function fetchUserPosts(FacebookGraph $graph, $userId) {
return $graph->get('/' . $userId . '/posts');
}
Webhooks
Configure webhooks in config/edemy_fb.php:
'webhooks' => [
'verify_token' => 'your_verify_token',
'callback_url' => '/facebook/webhook',
],
Handle events in a controller:
public function handleWebhook(Request $request, FacebookWebhookHandler $handler) {
$handler->process($request->input());
}
$this->app->bind('facebook.authenticator', function ($app) {
return new FacebookAuthenticator($app['config']['edemy_fb']);
});
Route::middleware(['auth:facebook'])->group(function () {
// Protected routes
});
FacebookUserAuthenticated).App Secret Mismatch
app_secret in config/edemy_fb.php matches your Facebook App settings.php artisan config:clear if changes aren’t reflected.Webhook Verification
hub.challenge and hub.mode in the webhook endpoint to avoid spoofing.if ($request->input('hub.mode') !== 'subscribe' || $request->input('hub.challenge')) {
abort(403);
}
Deprecated API Calls
default_graph_version in config to v18.0 if needed.CORS Issues
Enable Logging
Add to config/edemy_fb.php:
'debug' => env('APP_DEBUG', false),
Check logs in storage/logs/laravel.log for OAuth errors.
Token Expiry Handle expired tokens by implementing a refresh logic:
try {
$authenticator->getAccessToken();
} catch (TokenExpiredException $e) {
$authenticator->refreshToken();
}
Custom User Mappers Override the default user mapping by binding a custom mapper:
$this->app->bind('facebook.user_mapper', function () {
return new CustomFacebookUserMapper();
});
Additional Permissions
Extend the default permissions (e.g., email, public_profile) in the config:
'auth' => [
'permissions' => ['email', 'public_profile', 'user_birthday'],
],
Custom Graph API Methods
Extend the FacebookGraph service by creating a decorator:
class CustomFacebookGraph extends FacebookGraph {
public function customMethod($path) {
return $this->call('/me/' . $path);
}
}
Bind it in a service provider:
$this->app->bind('facebook.graph', function ($app) {
return new CustomFacebookGraph($app['config']['edemy_fb']);
});
Environment Variables
Prefer using .env for sensitive data:
FB_APP_ID=your_app_id
FB_APP_SECRET=your_app_secret
Reference them in config/edemy_fb.php:
'app_id' => env('FB_APP_ID'),
'app_secret' => env('FB_APP_SECRET'),
Default Redirect URI
The bundle assumes /login/facebook/callback as the default redirect URI. Update it in config if needed:
'auth' => [
'redirect_uri' => '/custom/callback',
],
Ensure this URI is also added to your Facebook App’s Valid OAuth Redirect URIs.
How can I help you explore Laravel packages today?