简体   繁体   English

Laravel Facebook SDK束注销不起作用

[英]Laravel facebook-sdk bundle logout not working

I am using Facebook-sdk bundle for Laravel and everything works fine, except the logout link. 我使用Laravel的 Facebook-sdk捆绑包,除注销链接外,其他一切正常。 When I click logout, I get redirected and all looks like it is working, but when it loads the page back, I'm still logged in? 当我单击注销时,我将被重定向,并且看起来一切正常,但是当它重新加载页面时,我仍然登录吗?

Is this a Laravel problem maybe? 这可能是Laravel问题吗? Does it store sessions differently? 它存储会话的方式不同吗?

I've built this class, but as I said, I don't think this is a problem because all is working fine, except logout session is not getting cleared. 我已经建立了这个类,但是正如我所说的,我认为这不是问题,因为除了注销会话没有得到清除之外,其他所有组件都工作正常。

Code: 码:

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'];
    }

}

Thanks for any help! 谢谢你的帮助! Cheers! 干杯!

The underlying facebook php sdk uses the built in php sessions (by default) to store persistent information like the authenticated facebook user's id. 底层的Facebook php sdk使用内置的php会话(默认情况下)来存储持久性信息,例如经过身份验证的facebook用户ID。 However the sdk won't destroy this information on its own since it's difficult to tell when that should happen automatically. 但是,SDK不会自行销毁此信息,因为很难确定何时应自动发生。

You can clear this persisted information with the destroySession method on the facebook sdk object. 您可以使用facebook sdk对象上的destroySession方法清除此持久化信息。 The best place to call this method is on the logout url's redirect back endpoint, since that is where the visitor gets directly after facebook done with it's own logout. 调用此方法的最佳位置是注销URL的重定向回端点上,因为在Facebook完成其自身的注销后,访问者可以在此直接获得访问者。

This would look like: 看起来像:

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

You probably want to set up a route where users will arrive after logout and pass it into getLogoutUrl() like this: 您可能想要设置一条用户注销后将到达的路由,并将其传递到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')));
}

And have a route like this: 并有一条这样的路线:

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('/');

}));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM