Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meteor access tokens

Tags:

oauth

meteor

When using Meteor, with the new authentication system, how can I get facebook access token, when logged with accounts-facebook package?

like image 884
zVictor Avatar asked Sep 24 '12 18:09

zVictor


Video Answer


1 Answers

get it either from a Meteor.method:

// server
Meteor.methods({
  getAccessToken : function() {
    try {
      return Meteor.user().services.facebook.accessToken;
    } catch(e) {
      return null;
    }
  }
}); 

// client
Meteor.call("getAccessToken", function(error, accessToken){
   console.log(accessToken);
})

or publish it:

//server
Meteor.publish("currentUserAccessToken", function(){
    var self = this;
    if (this.userId()){
        handle = Meteor.users.find(this.userId()).observe({
            added: function(user){
                self.set("currentUserAccessToken", user._id, {value: user.services.facebook.accessToken});
                self.flush();
            },
            changed: function(user){
                self.set("currentUserAccessToken", user._id, {value: user.services.facebook.accessToken});
                self.flush();
            }
        });

        this.onStop(function() {
            handle.stop();
        });
    }
});

//client
var AccessToken = new Meteor.Collection("currentUserAccessToken");
Meteor.subscribe("currentUserAccessToken");

//access the value
var accessToken = AccessToken.findOne().value;
like image 92
Lloyd Avatar answered Oct 18 '22 11:10

Lloyd