Skip to content

Commit

Permalink
add facebook login example (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
slimani-dev authored Apr 2, 2023
1 parent 4e407bc commit 8c7fcf9
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions docs/examples/facebook_login.md
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"
}
```

0 comments on commit 8c7fcf9

Please sign in to comment.