I'm using the script below when a user wants to register to my website, so when he approves the application the fields are being automatically filled and he just needs to submit the form.
But i'm stuck on the second part of the login mechanism, how do I check if a user is already logged in to his facebook account and he approved my application?
I keep the facebook id of each user in my users db.
Note: i'm using the user flow, not the server flow.
<script type="text/javascript">
var appId = "xxxxxxxxx";
开发者_如何学编程 if(window.location.hash.length == 0)
{
url = "https://www.facebook.com/dialog/oauth?client_id=" +
appId + "&redirect_uri=" + window.location +
"&response_type=token";
//window.open(url);
} else {
accessToken = window.location.hash.substring(1);
graphUrl = "https://graph.facebook.com/me?" + accessToken +
"&callback=displayUser"
//use JSON-P to call the graph
var script = document.createElement("script");
script.src = graphUrl;
document.body.appendChild(script);
}
function displayUser(user) {
myform = document.forms['regform'];
username = user.name;
first = username.split(" ");
myform.elements['fname'].value = first[0];
myform.elements['lname'].value = first[1];
myform.elements['username'].value = first[0]+first[1];
myform.elements['email'].value = user.email;
myform.elements['fbid'].value = user.id;
}
</script>
You may want to use the Facebook JS SDK: http://developers.facebook.com/docs/reference/javascript/
For example to check login status you can do:
FB.getLoginStatus(function(response) {
if (response.session) {
// logged in and connected user, someone you know
} else {
// no user session available, someone you dont know
}
});
精彩评论