Yii 拡張機能 yii-facebook-opengraph でログインしようとしています。問題なくログイン ウィジェットをインストールします。
2 つの問題があります。
- 最初:ログインポップアップが閉じた後。ログインしたページにリダイレクトする方法がわかりません(ポップアップが閉じて同じページにとどまります)。userId をセッションに保存したいと思います。
解決: 編集: SFacebook.php メソッドの initJs .
if ($this->async) {
$init = "window.fbAsyncInit = function(){{$init};
// whenever the user logs in, we tell our login service
FB.Event.subscribe('auth.login', function() {
window.location = ' " . Yii::app()->baseUrl."xxxREDIRECT-URLxxx". "';
});
};
(function(d){
var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
js = d.createElement('script'); js.id = id; js.async = true;
js.src = '{$script}';
d.getElementsByTagName('head')[0].appendChild(js);
}(document));";
}
2: ページ内で Facebook API を呼び出しています。ユーザーがFacebookセッションを閉じると、Facebook APIを呼び出そうとするとエラーが発生する
ため、ユーザーがログインしているかどうかを次のようにすべてのページで確認します。try{ $userId = Yii::app()->session['userId']; if (($userId=="") || ($userId==0)){ try{ $user = Yii::app()->facebook->api('/me'); Yii::app()->session['userId'] = $user['id']; Yii::app()->session['userName'] = $user['name']; return true; }catch (Exception $e) { $this->redirect(array('Admin/Index')); //LOGIN PAGE } }else{ return true; } }catch (Exception $e) { $this->redirect(array('Admin/Index')); //LOGIN PAGE }