LoginSignup
15
15

More than 5 years have passed since last update.

Laravel 4でFacebook SDK 4.0.XのFacebookRedirectLoginHelperを使う

Last updated at Posted at 2014-06-10

Facebook PHP SDK 4.0.XのFacebookRedirectLoginHelperは、ネイティブセッションを使っているので、セッションを有効にしないと動かないという罠がある。Laravelだとネイティブセッション使って無くて困る。storeStateloadStateという関数でセッションを使っていて、コメントを読むとネイティブセッションを使いたくないなら継承してオーバーライドしろやと書いてあるので、そうする。

  /**
   * Stores a state string in session storage for CSRF protection.
   * Developers should subclass and override this method if they want to store
   *   this state in a different location.
   *
   * @param string $state
   *
   * @throws FacebookSDKException
   */

どっか自作クラスの置き場所を作るなどする場合は(app/libsとか)、autoloadのためにcomposer.jsonに追記してdump-autoload。

  :
"autoload": {
  "classmap": [
    "app/commands",
    "app/controllers",
    "app/models",
    "app/database/migrations",
    "app/database/seeds",
    "app/libs",
    "app/tests/TestCase.php"
  ]
  :

その中でstoreStateloadStateをオーバーライドする。

<?php

use Facebook\FacebookRedirectLoginHelper;

class LaravelFacebookRedirectLoginHelper extends Facebook\FacebookRedirectLoginHelper {
    protected function storeState($state)
    {
        Session::put('FBRLH_state', $state);
    }

    protected function loadState()
    {
        return $this->state =  Session::get('FBRLH_state');
    }
}

それでFacebookRedirectLoginHelperの代わりにLaravelFacebookRedirectLoginHelperを使ってログイン処理を書けばよい。

参考: http://stackoverflow.com/questions/23501811/new-facebook-sdk-4-throw-exception-about-php-session-active-in-laravel

15
15
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
15
15