Meteor how to get access to Facebook Graph Api for both Server and Client side

守給你的承諾、 提交于 2019-12-21 20:09:06

问题


accounts-facebook package provides only logIn and logOut functionality.

meteor-fbgraph gives access to fbgraph on server side.

facebook-sdk gives access to fbgraph on client side.

The problem is that facebook-sdk doesn't use anything provided with Accounts-ui, such as Accounts.onLogin event or Accounts.ui.config. After click on {{> loginButtons}} when user is logged in only Meteor.user() does log out, facebook-sdk still has it's AccessToken and remains logged in. In the result half of the application remains logged in (client) and half logged out (server).

Here is my workaround by pairing Accounts with FB events, but I think it's not a proper solution.

Accounts.onLogin(function(){
  FB.login();
  AccountsOnLogout(function(){
    FB.logout();
  });
});

function AccountsOnLogout(callback){
  var waitForLogout = setInterval(function() {
    if (!(Meteor.user())) {
      console.log("logged out");
      callback();
      clearInterval(waitForLogout);
    }
  }, 1000);
}

Do You have any better idea how to get to fbGraph on client side?


回答1:


I am using only "kinda workaround" for you, cause I would be manipulating and caching respond data on the server anyway. So I just call methods and use that server side.

Facebook = (accessToken) ->
  @fb = Meteor.npmRequire 'fbgraph'
  @accessToken = accessToken
  @fb.setAccessToken @accessToken
  @options =
    timeout: 3000
    pool:
      maxSockets: Infinity
    headers:
      connection: "keep-alive"
  @fb.setOptions @options


FBQuery = (query, method, fbObject) ->
  if typeof method is 'undefined' then method = 'get'
  console.log "query is: " + query
  data = Meteor.sync((done) ->
    fbObject[method](query, (err, res) ->
      done(null, res)
    )
  )
  data.result


Meteor.methods(
  getUserData: ->
    fb = new Facebook(Meteor.user().services.facebook.accessToken)
    FBQuery '/me', 'get', fb

  getUserEvents: ->
    fb = new Facebook(Meteor.user().services.facebook.accessToken)
    FBQuery '/' + Meteor.user().services.facebook.id + '/events', 'get', fb

  getUserGroups: ->
    fb = new Facebook(Meteor.user().services.facebook.accessToken)
    FBQuery '/' + Meteor.user().services.facebook.id + '/groups?fields=name&limit=1000', 'get', fb
)

And client side

Template.home.events(
  'click #btn-user-data': (e) ->
    Meteor.call('getUserData', (err, data) ->
      $('#result').text(JSON.stringify(data, undefined, 4))
    )
)


来源:https://stackoverflow.com/questions/30911944/meteor-how-to-get-access-to-facebook-graph-api-for-both-server-and-client-side

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!