【问题标题】:Laravel facebook-sdk bundle logout not workingLaravel facebook-sdk 捆绑注销不起作用
【发布时间】:2013-03-24 09:29:22
【问题描述】:

我正在为 Laravel 使用 Facebook-sdk 捆绑包,一切正常,除了注销链接。当我点击注销时,我被重定向并且一切看起来都在工作,但是当它重新加载页面时,我仍然登录?

这可能是 Laravel 的问题吗?它是否以不同的方式存储会话?

我已经建立了这个类,但正如我所说,我认为这不是问题,因为一切正常,除了注销会话没有被清除。

代码:

class Fb{

    // -----------------------------------------------------------------------
    // Variables

    private $ioc; // IOC container

    public $state; // If logged or not

    public $data; // Data that came from request
    public $settings = array("name,gender");


    // -----------------------------------------------------------------------
    // Logical functions
    public function __construct(){
        $this->ioc = IoC::resolve('facebook-sdk');

        if ($this->getUser()) {
            try {
                $this->request();
                $this->state = true;
            } catch (FacebookApiException $e) {
                error_log($e);
        }
        }else{
            $this->state = false;
        }
    }

    public function getUser(){
        return $this->ioc->getUser();
    }

    public function request(){
        $this->data = $this->ioc->api("/me?fields=".implode($this->settings));
    }

    public function debug(){
        return dd($this->data);
    }

    // -----------------------------------------------------------------------
    // Login & Logout links

    public function login(){
        return $this->ioc->getLoginUrl();
    }

    public function logout(){
        return $this->ioc->getLogoutUrl();
    }

    // -----------------------------------------------------------------------
    // Get data via SDK

    // Name
    public function name(){
        return $this->data['name'];
    }

    // Picture
    public function picture($w=50,$h=50){
        return "https://graph.facebook.com/". $this->data['id'] ."/picture?width=$w&height=$h";
    }

    // Gender
    public function gender(){
        return $this->data['gender'];
    }

}

感谢您的帮助! 干杯!

【问题讨论】:

    标签: php facebook laravel


    【解决方案1】:

    底层 facebook php sdk 使用内置的 php 会话(默认情况下)来存储持久性信息,例如经过身份验证的 facebook 用户 ID。 但是 sdk 不会自行销毁这些信息,因为很难判断什么时候应该自动发生。

    您可以使用 facebook sdk 对象上的 destroySession 方法清除此持久信息。调用此方法的最佳位置是注销 url 的重定向返回端点,因为这是访问者在 facebook 完成自己的注销后直接到达的地方。

    这看起来像:

    // method on Fb class
    public function destroySession() {
        // just forward the call down to the sdk object
        $this->ioc->destroySession();
    }
    

    您可能希望设置一条用户在注销后到达的路线并将其传递给getLogoutUrl(),如下所示:

    // method on Fb class
    public function logout(){
        // tell explicity where to send the user when facebook is done, otherwise the current url will be used
        return $this->ioc->getLogoutUrl(array('next' => URL::to_route('after_logout')));
    }
    

    并且有这样的路线:

    Route::get('after_logout', array('as' => 'after_logout', 'do' => function() {
        $fb = new Fb();
        // call the session clearing
        $fb->destroySession();
        // send the user to its merry way
        return Redirect::to('/');
    
    }));
    

    【讨论】:

      猜你喜欢
      • 2012-03-28
      • 2015-11-08
      • 2014-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多