Showing/hiding content from FB fans on my own site - facebook-like

So I'm adding the 'like' button to my site and I've got it to display "Thank you for liking example.com" and "You seemed to have unliked example.com" using edge.create;
FB.Event.subscribe('edge.create',
function(response) {
document.getElementById("demo").innerHTML=('Thank you for liking example.com');
}
);
FB.Event.subscribe('edge.remove',
function(response) {
document.getElementById("demo").innerHTML=('You seemed to have unliked example.com');
}
);
However this method only works when the button is clicked. If a visitor comes to my site and has already liked example.com I would like to show them a message without them having to click anything.
I know this is possible to do on a facebook page but what about on my own site. Any ideas?
Cheers

Certainly possible using FB.getLoginStatus
FB.getLoginStatus(function(response) {
if (response.authResponse) {
// logged in and connected user, someone you know
} else {
// no user session available, someone you dont know
}
});
http://developers.facebook.com/docs/reference/javascript/FB.getLoginStatus/
Keep in mind that this refers to a user being logged in to your app and / or Facebook. If someone is not logged in to Facebook, they will of course return FALSE and thus your message should reflect to have them connect OR login.

Related

How do I obtain user fields from Facebook login

I'm busy implementing Facebook login for my django site. I'm not sure if I'm on the right track or way off the ball at the moment. I have implemented the full code as seen in the FB doc example https://developers.facebook.com/docs/facebook-login/web. Basically inserted script on the template. I have the button and once logging in I have the "Welcome fetching your information" "successful login for Joshua Raphael" in my console.log.
How do I now obtain the required email, username and profile pic? Is it a part of the response object? Also if I have email login, is it possible to obtain the response email and check my current database to see if the user is already registered?
Prefer not to use pre-made django apps but if you highly recommend one I may try it.Thanks
I assume you are doing this:
FB.login(function(response) {
if (response.authResponse) {
console.log('Welcome! Fetching your information.... ');
FB.api('/me', function(response) {
console.log('Good to see you, ' + response.name + '.');
});
} else {
console.log('User cancelled login or did not fully authorize.');
}
});
you can console.log the response and see what all it contains, you will get the email, username and profilepic in it, also you can specify the fields if it is not there like this:
FB.api('/me', {fields: 'last_name'}, function(response) {
console.log(response);
});
more here
also make sure you have added email in the scope in login method. like this:
FB.login(function(response) {
// handle the response
}, {scope: 'public_profile,email'});

Ember.js - Can the root url link to two routes, one for authenticated users and one for guests?

Is it possible for the root path, example.com, to display a landing page(application/index) for not logged in users but a profile page (users/show) for users who are logged in?
Guest user -> example.com -> application/index
Authenticated user -> example.com -> users/show
I know that this goes against Ember's philosophy of the app state being reflected by the url, still, does anyone know if/how this situation is possible?
Cheers!
Yes, You can do it. Firstly, you should check the user logged in or not in "beforeModel" of "application" route. If user is logged in then transits it to "profile" page using "transitionTo" method otherwise transits it to "login" page.
beforeModel: function(transition) {
var user;
//put here method to check if user is logged in or not
if (!user) {
//if no user then transist to login
this.transitionTo('login');
} else {
//other wise to profile page
this.transitionTo('profilePage');
}
}

How to client side authentication with Emberjs

First of all I don't use Ruby nor Devise :) (all my searches led me to plugins that kind of rely on Devise)
I want to do pretty simple authentication with Ember, I have a REST backend that blocks requests without a proper cookie(user-pass) and i want Ember to watch when it gets 403 forbidden (won't let you to transition into protected URLs) and then pop up a user-login dialog.
So when a user tries to send a new message for example(lets say i've built a forum) Ember will fire the request and if it gets 403 it will block the transition and popup a login form and will retry the transition after the login have completed
Also is there a way to get the errors from ember-data and respond to them? (if a user tries to change an attribute he can't access i would like to inform him about it[Access denied or something like that])
I want to use custom errors that my server will send to ember data not just error numbers but words like "Sorry you can't change this before 12 PM"
You can simply listen to the response of your server and transition to your LOGIN (or whatever you call it) route. In my apps I happen to keep two types of routes (LOGIN and AUTHENTICATED). When they access the authenticated routes without logging in, they get a 401 unauthorized error and get transitioned to the LOGIN route.
// AuthenticatedRoute.js
redirectToLogin: function(transition) {
// alert('You must log in!');
var loginController = this.controllerFor('login');
loginController.set('attemptedTransition', transition);
this.transitionTo('login');
},
events: {
error: function(reason, transition) {
if (reason.status === 401) {
this.redirectToLogin(transition);
} else {
console.log(reason);
window.alert('Something went wrong');
}
}
},
model: function () {
return this.store.find('post');
},
So now when the user requests for post he gets a 401 and gets transitioned to LOGIN controller.
// LoginController.js
login: function() {
var self = this, data = this.getProperties('username', 'password');
// Clear out any error messages.
this.set('errorMessage', null);
$.post('/login', data).then(function(response) {
self.set('errorMessage', response.message);
if (response.success) {
alert('Login succeeded!');
// Redirecting to the actual route the user tried to access
var attemptedTransition = self.get('attemptedTransition');
if (attemptedTransition) {
attemptedTransition.retry();
self.set('attemptedTransition', null);
} else {
// Redirect to 'defaultRoute' by default.
self.transitionToRoute('defaultRoute');
}
}
});
}
The basic answer you need is capturing the events in the route and transitioning accordingly. I just happened to include the code for attempted transition as it comes in handy at times.

how to use facebook API getting friend's list in javascript

I am not familiar with using Facebook Javascript SDK.
My story is when I access to the website It displays all my friends(pic and name) on a webpage.
I registered Facebook API and App ID
I put website URL as http://localhost:81/ because I am testing my own local machine.
Do you have any good example website or good examples?
Please share with me
Thank you.
First your app should use required permissions like,
user_birthday, friends_birthday, user_location , friends_location...
( for more permissions)
Get info about current user:
FB.api('/me', function(response) {
// Stuff here
});
Get info about current user's friends:
FB.api('/me/friends', function(response) {
// Stuff here
});
you will get the response like,
{data: [{id: "FRIEND1_ID", name: "FRIEND1_NAME"}, {id: "FRIEND2_ID", name: "FRIEND2_NAME"}]....}
If you want get some more properties of your friends, use FIELDS parameter, like
FB.api('/me/friends', {fields: 'name,id,location,birthday'}, function(response) {
// Stuff here
});
If want to get individual user's friend info:
FB.api('/FRIEND1_ID', function(response) {
// Stuff here
});
Try this Example Site
Direct Access Method:
Log into Facebook under your name. If you need an "Access Token", Click on the Extended Permissions and click the "read_friendlist" check box to get your Access Token and then
submit your query. You may need to "Allow" access, so just follow the prompts given.
Voila! You now have the usernames for everyone on your Friends List. The "username" parameter in the query will give you the contact email "message to that person" and you append #facebook.com and send them a message. Very simple.
http://developers.facebook.com/tools/explorer?fql=SELECT%20username%20FROM%20user%20WHERE%20uid%20IN%20%28SELECT%20uid2%20FROM%20friend%20WHERE%20uid1%20%3D%20me%28%29%29%20ORDER%20BY%20name
-SAB

how to check login status of all users in facebook? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
response.authResponse is null
i have developed one facebook page with apps,
what i want is whenever any user visit my page, the user is served by one popup message, the popup will disappear only when user click "like" button, if user has already liked the page, popup will not appear, to do this i have write following code
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
var user_id = response.authResponse.userID;
var page_id = "app_id";
var fql_query = "SELECT uid FROM page_fan WHERE page_id = "+page_id+"and uid="+user_id;
var the_query = FB.Data.query(fql_query);
the_query.wait(function(rows) {
if (rows.length == 1 && rows[0].uid == user_id) {
alert("liked the page");
popup.hide() // hide the popup as user has clicked like button
}
else {
alert("not liked ");
}
});
}
else if (response.status === 'not_authorized') {
}
else
{
alert("not logged in");
}
});
the above code works fine with only one FB user, one which is admin of this page, for all other user it returns response.status === 'not_authorized' status, so i'm unable to retrieve the user login information, because in this case i'm getting null response.authResponse
do anyone have any idea, how to check login status for all users, not for admin only...??
thanks.
If you are talking about a page tab app and all you want to know is the "like" status of the visiting user then all you have to do is parse the signed_request passed to your app. You can read more about using and parsing Signed Requests here.
In the signed_request there is a user object - the information you need is in that object.