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

Custom #104

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# CHANGELOG

## 3.2.0 - November 12, 2015

- Added ability to create new instances of `LaravelFacebookSdk` with a [different app settings](https://github.com/SammyK/LaravelFacebookSdk/tree/3.0#working-with-multiple-apps).


## 3.1.0 - September 3, 2015

- Added [fillable fields](https://github.com/SammyK/LaravelFacebookSdk/tree/3.0#specifying-fillable-fields) feature.
Expand Down
78 changes: 32 additions & 46 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Laravel Facebook SDK

[![Build Status](https://img.shields.io/travis/SammyK/LaravelFacebookSdk.svg)](https://travis-ci.org/SammyK/LaravelFacebookSdk)
[![Latest Stable Version](https://img.shields.io/badge/Latest%20Stable-3.1-blue.svg)](https://packagist.org/packages/sammyk/laravel-facebook-sdk)
[![Latest Stable Version](https://img.shields.io/badge/Latest%20Stable-3.2-blue.svg)](https://packagist.org/packages/sammyk/laravel-facebook-sdk)
[![Total Downloads](https://img.shields.io/packagist/dt/sammyk/laravel-facebook-sdk.svg)](https://packagist.org/packages/sammyk/laravel-facebook-sdk)
[![License](https://img.shields.io/badge/license-MIT-lightgrey.svg)](https://github.com/SammyK/LaravelFacebookSdk/blob/master/LICENSE)

Expand All @@ -23,6 +23,7 @@ A fully unit-tested package for easily integrating the [Facebook SDK v5](https:/
- [Facebook Login](#facebook-login)
- [Saving Data From Facebook In The Database](#saving-data-from-facebook-in-the-database)
- [Logging The User Into Laravel](#logging-the-user-into-laravel)
- [Working With Multiple Apps](#working-with-multiple-apps)
- [Error Handling](#error-handling)
- [Testing](#testing)
- [Contributing](#contributing)
Expand Down Expand Up @@ -342,7 +343,7 @@ By default the JavaScript SDK will not set a cookie, so you have to explicitly e
FB.init({
appId : 'your-app-id',
cookie : true,
version : 'v2.4'
version : 'v2.5'
});
```

Expand Down Expand Up @@ -542,7 +543,7 @@ class Event extends Eloquent

Since the Graph API will return some of the fields from a request as other nodes/objects, you can reference the fields on those using Laravel's [`array_dot()` notation](http://laravel.com/docs/helpers#arrays).

An example might be making a request to the `/me/events` endpoint and looping through all the events and saving them to your `Event` model. The [Event node](https://developers.facebook.com/docs/graph-api/reference/v2.3/event) will return the [place.location fields](https://developers.facebook.com/docs/graph-api/reference/location/) as [Location nodes](https://developers.facebook.com/docs/graph-api/reference/location/). The response data might look like this:
An example might be making a request to the `/me/events` endpoint and looping through all the events and saving them to your `Event` model. The [Event node](https://developers.facebook.com/docs/graph-api/reference/event) will return the [place.location fields](https://developers.facebook.com/docs/graph-api/reference/location/) as [Location nodes](https://developers.facebook.com/docs/graph-api/reference/location/). The response data might look like this:

```json
{
Expand Down Expand Up @@ -725,6 +726,23 @@ class FacebookController {
```


## Working With Multiple Apps

If you have multiple Facebook apps that you'd like to use in the same script or you want to tweak the settings during runtime, you can create a new instance of `LaravelFacebookSdk` with the custom settings.

```php
Route::get('/example', function(SammyK\LaravelFacebookSdk\LaravelFacebookSdk $fb) {
// All the possible configuration options are available here
$fb2 = $fb->newInstance([
'app_id' => env('FACEBOOK_APP_ID2'),
'app_secret' => env('FACEBOOK_APP_SECRET2'),
'default_graph_version' => 'v2.5',
// . . .
]);
});
```


## Error Handling

The Facebook PHP SDK throws `Facebook\Exceptions\FacebookSDKException` exceptions. Whenever there is an error response from Graph, the SDK will throw a `Facebook\Exceptions\FacebookResponseException` which extends from `Facebook\Exceptions\FacebookSDKException`. If a `Facebook\Exceptions\FacebookResponseException` is thrown you can grab a specific exception related to the error from the `getPrevious()` method.
Expand Down Expand Up @@ -752,59 +770,27 @@ If your app is being served from within the context of an app canvas or Page tab

Although it's possible to disable this feature completely, it's certainly not recommended as CSRF protection is an important security feature to have on your site and it should be enabled on every route by default.

I followed a blog post that explained how to [disable CSRF protection for specific routes in Laravel 5](http://www.camroncade.com/disable-csrf-for-specific-routes-laravel-5/).

I edited my `app\Http\Middleware\VerifyCsrfToken.php` file and added an `excludedRoutes()` method to it. Then I just created an array of routes that were endpoints to my canvas app or page tab. My complete file looks like this:
Add an exception to your canvas endpoint to the `$except` array in your `app\Http\Middleware\VerifyCsrfToken.php` file.

```php
<?php namespace App\Http\Middleware;
<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken as BaseVerifier;
use Illuminate\Session\TokenMismatchException;

class VerifyCsrfToken extends BaseVerifier
{
/**
* Handle an incoming request.
* The URIs that should be excluded from CSRF verification.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*
* @throws TokenMismatchException
* @var array
*/
public function handle($request, Closure $next)
{
if ($this->isReading($request) || $this->excludedRoutes($request) || $this->tokensMatch($request)) {
return $this->addCookieToResponse($request, $next($request));
}

throw new TokenMismatchException;
}

/**
* Ignore CSRF on these routes.
*
* @param \Illuminate\Http\Request $request
* @return bool
*/
private function excludedRoutes($request)
{
$routes = [
'my-app/canvas',
'my-app/page-tab',
// ... insert all your canvas endpoints here
];

foreach($routes as $route){
if ($request->is($route)) {
return true;
}
}

return false;
}
protected $except = [
'my-app/canvas',
'my-app/page-tab',
// ... insert all your canvas endpoints here
];
}
```

Expand Down
26 changes: 22 additions & 4 deletions src/LaravelFacebookSdk/LaravelFacebookSdk.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php namespace SammyK\LaravelFacebookSdk;

use Illuminate\Config\Repository as Config;
use Illuminate\Routing\UrlGenerator as Url;
use Laravel\Lumen\Routing\UrlGenerator as Url;
use Facebook\Facebook;

class LaravelFacebookSdk extends Facebook
Expand All @@ -17,18 +17,36 @@ class LaravelFacebookSdk extends Facebook
protected $url;

/**
* @param Config $config_handler
* @param Url $url
* @param array $config
* @var array
*/
private $default_config;

/**
* @param Config $config_handler
* @param Url $url
* @param array $config
*/
public function __construct(Config $config_handler, Url $url, array $config)
{
$this->config_handler = $config_handler;
$this->url = $url;
$this->default_config = $config;

parent::__construct($config);
}

/**
* @param array $config
*
* @return LaravelFacebookSdk
*/
public function newInstance(array $config)
{
$new_config = array_merge($this->default_config, $config);

return new static($this->config_handler, $this->url, $new_config);
}

/**
* Generate an OAuth 2.0 authorization URL for authentication.
*
Expand Down
2 changes: 1 addition & 1 deletion src/LaravelFacebookSdk/LaravelUrlDetectionHandler.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php namespace SammyK\LaravelFacebookSdk;

use Illuminate\Routing\UrlGenerator;
use Laravel\Lumen\Routing\UrlGenerator;
use Facebook\Url\UrlDetectionInterface;

class LaravelUrlDetectionHandler implements UrlDetectionInterface
Expand Down
2 changes: 1 addition & 1 deletion src/config/laravel-facebook-sdk.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
'facebook_config' => [
'app_id' => env('FACEBOOK_APP_ID'),
'app_secret' => env('FACEBOOK_APP_SECRET'),
'default_graph_version' => 'v2.4',
'default_graph_version' => 'v2.5',
//'enable_beta_mode' => true,
//'http_client_handler' => 'guzzle',
],
Expand Down
13 changes: 12 additions & 1 deletion tests/LaravelFacebookSdkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

class LaravelFacebookSdkTest extends \PHPUnit_Framework_TestCase
{

/**
* @var \Illuminate\Config\Repository|\Mockery\MockInterface
*/
Expand Down Expand Up @@ -82,4 +81,16 @@ public function the_default_config_can_be_overwritten_by_passing_arguments_to_ge
$this->assertContains('scope=dance%2Ctotes', $login_url);
}

/** @test */
public function the_a_new_instance_can_be_generated_with_new_app_config()
{
$fb = $this->laravel_facebook_sdk->newInstance([
'app_id' => 'app2_id',
'app_secret' => 'app2_secret',
]);

$app_token = (string) $fb->getApp()->getAccessToken();

$this->assertEquals('app2_id|app2_secret', $app_token);
}
}