首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >自定义Facebook FOSFacebookBundle登录按钮

自定义Facebook FOSFacebookBundle登录按钮
EN

Stack Overflow用户
提问于 2013-01-31 10:39:36
回答 1查看 1.6K关注 0票数 3

有人知道如何自定义/更改FosFacebook登录按钮的图像?

代码语言:javascript
复制
{{ facebook_login_button({'autologoutlink': true}) }}

并在facebook登录页面上重定向,而不是弹出。

EN

回答 1

Stack Overflow用户

发布于 2013-01-31 12:12:51

这使用了一个小枝助手,并使用facebook呈现一个带有“”符号的模板。

如果您需要重定向到登录页面,请使用html链接,从php facebook sdk调用方法'getLoginUrl‘。

您可以通过服务'fos_facebook.api'寄存器通过FOSFacebookBundle访问它。

用代码示例更新

以下是我使用facebook登录的javascript代码“无摩擦”

代码语言:javascript
复制
<script>
//scopes string stored as container parameter
var fb_scope='{{ facebook_scope }}';
//connected in sf2 app ?
{% if app.user %}
    var isLogged=true;
{% else %}
    var isLogged=false;
{% endif %}
//var to store fb status
var fb_connected = false;

function onFbInit() {

  if (typeof(FB) != 'undefined' && FB != null ) {

      // fetch the status on load
      FB.getLoginStatus(handleSessionResponse);

      //my facebook connect link
      $('a.facebook_connect').on('click', function(e) {
        FB.login(function(response) {
           if (response.authResponse) {
             checkAccount();
           } else {
              onAuthRefusal();
           }
         }, {scope: fb_scope});
        e.preventDefault();
      });
      //my logout link
      $('#logout').on('click', function(e) {
        FB.logout(null);
      });

      //want to distinguish three status states
      FB.Event.subscribe('auth.statusChange', handleSessionResponse);
  }
}




function handleSessionResponse(response) {
    if (response.status === 'connected') {
        // the user is logged in and has authenticated your
        // app, and response.authResponse supplies
        // the user's ID, a valid access token, a signed
        // request, and the time the access token
        // and signed request each expire
        var uid = response.authResponse.userID;
        var accessToken = response.authResponse.accessToken;
        var expiresIn = response.authResponse.expiresIn;

        //already logged in sf2 app
        if(!isLogged){
            checkAccount();
        }
        fb_connected = true;

      } else if (response.status === 'not_authorized') {
        // the user is logged in to Facebook,
        // but has not authenticated your app
         fb_connected = false;
      } else {
        // the user isn't logged in to Facebook.
          fb_connected = false;
      }
}

function checkAccount(){
   //route defined in your security.yml  ( check_path value in your fos_facebook firewall )
   top.location.href  = '{{ path('_security_check_facebook' ) }}';
}

function onAuthRefusal(){
 //message, special page ?
 console.log('refused authentification, message to display ?');
}


</script>

{{ facebook_initialize({'xfbml': true, 'fbAsyncInit': 'onFbInit();', 'frictionlessRequests' : 'true'} ) }}

我还定义了重定向到facebook的路由( /login/facebook )

代码语言:javascript
复制
//controller action for /login/facebook pattern
function loginFacebookAction()
{
    //get facebook api service registered by FacebookBundle
    $api = $this->container->get('fos_facebook.api');
    //get scope provided in url or default
    $scope = $this->container->get('request')->get('scope', 'publish_stream');
    //generate call url that use facebook check_path (used by FacebookBundle for authentification)
    $callback = $this->container->get('router')->generate('_security_check_facebook',array(), true);
    $redirect_url = $api->getLoginUrl(array('scope' => $scope, 'redirect_uri' => $callback));

    return new RedirectResponse($redirect_url);
}

就这样了!

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14623778

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档