Skip to content

Instantly share code, notes, and snippets.

Created December 2, 2012 14:17
  • Star 13 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
meteor login with facebook
/* Imagine you want to develop a facebook canvas app.
This snippet will do all the auth dance without requiring the user to click on login.
But if the user is not logged in facebook, it will ask for the user to login.
Tip: Open the browser console to see debug messages.
Followed instructions of Facebook:
It's is possible to improve this code with more error handling.
if (Meteor.is_client) {
(function(d, debug){
var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
if (d.getElementById(id)) {return;}
js = d.createElement('script'); = id; js.async = true;
js.src = "//" + (debug ? "/debug" : "") + ".js";
ref.parentNode.insertBefore(js, ref);
}(document, /*debug*/ false));
window.fbAsyncInit = function() {
// init the FB JS SDK
appId : 'YOUR_APP_ID', // App ID from the App Dashboard
channelUrl : '//localhost:3000/channel.html', // Channel File for x-domain communication for localhost debug
// channelUrl : '//', // Channel File for x-domain communication
status : true, // check the login status upon init?
cookie : true, // set sessions cookies to allow your server to access the session?
xfbml : true // parse XFBML tags on this page?
function call_facebook_login(response){
FB.api('/me', function(fb_user){
var access_token = response.authResponse.accessToken;'facebook_login', fb_user, access_token, function(error, user_id){
if (!error){
Accounts._makeClientLoggedIn(user_id, access_token);
function checkLoginStatus(response) {
if(response && response.status == 'connected') {
console.log('User is authorized');
// Now Personalize the User Experience
console.log('Access Token: ' + response.authResponse.accessToken);
} else {
console.log('User is not authorized');
// Login the user
FB.login(function(response) {
if (response.authResponse) {
console.log('Welcome! Fetching your information.... ');
} else {
console.log('User cancelled login or did not fully authorize.');
}, {scope: 'email,friends_likes,friends_birthday'});
if (Meteor.is_server) {
facebook_login: function(fbUser, accessToken) {
var options, serviceData, userId;
serviceData = {
accessToken: accessToken,
options = {
profile: {
return userId = Accounts.updateOrCreateUserFromExternalService('facebook', serviceData, options).id;
Copy link

avital commented Feb 27, 2013

On client.js line 35 do the following instead, using the 'facebook' branch on Github:

var state =;
Meteor.http.get('/_oauth/facebook', {
  params: {
    state: state,
    access_token: access_token
}, function() {
   Meteor.apply('login', [{oauth: {state: state}}], {wait: true}, function(e, result) {
      Accounts._makeClientLoggedIn(, result.token);   

Copy link

Well, that didn't work. I will wait for you to come back from vacation :)

Copy link

atulmy commented Aug 2, 2015

Does anyone have solution on this? @gabrielhpugliese did you manage to login user automatically?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment