Skip to content

Instantly share code, notes, and snippets.

@gabrielhpugliese
Created December 2, 2012 14:17
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gabrielhpugliese/4188927 to your computer and use it in GitHub Desktop.
Save gabrielhpugliese/4188927 to your computer and use it in GitHub Desktop.
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: https://developers.facebook.com/docs/howtos/login/getting-started/
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'); js.id = id; js.async = true;
js.src = "//connect.facebook.net/en_US/all" + (debug ? "/debug" : "") + ".js";
ref.parentNode.insertBefore(js, ref);
}(document, /*debug*/ false));
window.fbAsyncInit = function() {
// init the FB JS SDK
FB.init({
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 : '//yoururl.com/channel.html', // 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?
});
FB.getLoginStatus(checkLoginStatus);
function call_facebook_login(response){
FB.api('/me', function(fb_user){
var access_token = response.authResponse.accessToken;
Meteor.call('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);
console.log(response)
call_facebook_login(response);
} else {
console.log('User is not authorized');
// Login the user
FB.login(function(response) {
if (response.authResponse) {
console.log('Welcome! Fetching your information.... ');
call_facebook_login(response);
} else {
console.log('User cancelled login or did not fully authorize.');
}
}, {scope: 'email,friends_likes,friends_birthday'});
}
}
}
}
if (Meteor.is_server) {
Meteor.methods({
facebook_login: function(fbUser, accessToken) {
var options, serviceData, userId;
serviceData = {
id: fbUser.id,
accessToken: accessToken,
email: fbUser.email
};
options = {
profile: {
name: fbUser.name
}
};
return userId = Accounts.updateOrCreateUserFromExternalService('facebook', serviceData, options).id;
}
});
}
@avital
Copy link

avital commented Feb 27, 2013

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

var state = Random.id();
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.id, result.token);   
   });
})

@gabrielhpugliese
Copy link
Author

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

@atulmy
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