我正在使用Facebook-sdk bundle用于Laravel和一切工作正常,除了退出链接。当我单击注销时,我被重定向,一切看起来都很正常,但是当它加载页面回来时,我仍然登录?
这可能是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'];
}
}
谢谢你的帮助!干杯!
底层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('/');
}));