-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4e407bc
commit 8c7fcf9
Showing
1 changed file
with
67 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,67 @@ | ||
# Facebook Login Example | ||
|
||
This example demonstrates how to implement Facebook Login using the Laravel Facebook Graph SDK, based on the original example for [Facebook SDK V6 for PHP](https://github.com/joelbutcher/facebook-php-graph-sdk/blob/6.x/docs/examples/facebook_login.md). | ||
|
||
## Example | ||
|
||
In this example, we will be using two routes: one to generate the Facebook Login URL and one for the callback where Facebook will redirect the user after the login dialog is called. | ||
|
||
> Before we start, make sure that you have added the **Persistent Data Handlers** in the `AppServiceProvider`, You can find an example of how to do this [here](./docs/examples/persistent_data_storage.md) | ||
## Route for generating Facebook Login URL | ||
|
||
```php | ||
|
||
use JoelButcher\Facebook\Facades\Facebook as FacebookFacade; | ||
|
||
... | ||
|
||
Route::get('facebook/login', function () { | ||
// set the permissions (scopes) | ||
// by default the email and public_profile permission are added | ||
// in the HandlesAuthentication trait | ||
|
||
$scopes = ['pages_manage_posts', 'pages_read_engagement', 'pages_show_list']; | ||
|
||
$loginUrl = FacebookFacade::getRedirect(route('facebook.callback'), $scopes); | ||
|
||
echo '<a href="' . $loginUrl . '">Log in with Facebook!</a>'; | ||
}); | ||
``` | ||
|
||
## Route for Facebook callback | ||
|
||
```php | ||
use JoelButcher\Facebook\Facebook; | ||
use JoelButcher\Facebook\Facades\Facebook as FacebookFacade; | ||
|
||
... | ||
|
||
Route::get('facebook/callback', function () { | ||
// this is how to get the token | ||
// make sur to save the token in the database | ||
// because you can't use this methods again unless | ||
// you repeat the long proccess | ||
|
||
$token = FacebookFacade::getToken(); | ||
|
||
|
||
// this is how to use the token | ||
|
||
$fb = app(Facebook::class); | ||
$fb->getFacebook()->setDefaultAccessToken($token); | ||
|
||
return $fb->getUser(); | ||
})->name('facebook.callback'); | ||
|
||
``` | ||
|
||
This will return a JSON object with the user's name and ID: | ||
|
||
|
||
```json | ||
{ | ||
"name":"Facebook User", | ||
"id":"XXXXXXXXXXXXXXXX" | ||
} | ||
``` |