FB.init({
appId : '435081376536904', // App ID
/* channelURL : '../../Vendor/channel.php', // Channel File */
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
oauth : true, // enable OAuth 2.0
xfbml : true // parse XFBML
});
window.fbAsyncInit = function() {
/**************
//alert('Aysnch loading complete');
// Checks whether the user is logged in
FB.getLoginStatus(function(response) {
if (response.authResponse) {
// logged in and connected user, someone you know
alert('You are connected');
} else {
// no user session available, someone you dont know
alert('You are disconnected');
}
});
// Replaced with the code below
FB.Event.subscribe('auth.authResponseChange', function(response) {
if (response.authResponse) {
// the user has just logged in
alert('You just logged in facebook from somewhere');
} else {
// the user has just logged out
alert('You just logged out from faceboook');
}
}); ***************/
};
// Outside fbAsynch - this WORKS
FB.Event.subscribe('auth.authResponseChange', function(response) {
if (response.authResponse) {
// the user has just logged in
// alert('You just logged into Alans facebook from somewhere.'.concat(response.authResponse));
//console.log(response);
//alert( 'Listern on '); // This always runs
if(response.status=='connected'){
var fbuser= response.authResponse.userID;
// because they're connected to us, log em in or auto register them!
//
var username='';
var name='';
var email='';
FB.api('/me', function(response) {
// console.log('Good to see you, ' + response.name );
// console.log(response);
name=response.name;
email = response.email;
username = response.username;
//});
//alert( 'User is Connected:'.concat(username));
// };
$.ajax({
type: 'POST',
url: '../Users/ajax_login',
data: { facebook_id: fbuser,
email : email,
username: username,
name : name,
first_name : response.first_name,
last_name : response.last_name
}
}).done(function( msg ) {
//alert( 'AJAX sent: ' + msg );
if (msg.substr(0,2)=='OK'){
//alert( 'Logged you in. ' + fbuser );
$('#UserLoginForm').hide();
var welcome_message='Welcome '+ name + '. Redirect to your home page?';
var home=confirm(welcome_message);
if (home===true){top.location.href = '../users/home';};
} else {
//alert( 'Could not log you in. ' + msg );
};
});
});
};
} else {
// the user has just logged out
// alert('You just logged out from faceboook');
}
});
// logs the user in the application and facebook - doesn't get executed
function login(redirection){
alert( 'Clicked login 2');
FB.login(function (response) {
if(response.authResponse) {
// user is logged in
// console.log('Welcome!');
if(redirection != null && redirection != ''){
top.location.href = redirection;
}
} else {
// user could not log in
console.log('User cancelled login or did not fully authorize.');
}
}, {scope: 'email'});
}
// logs the user out of the application and facebook
function logout(redirection){
FB.logout(function(response) {
// user is logged out
// redirection if any
if(redirection != null && redirection != ''){
top.location.href = redirection;
}
});
}
// Load the SDK Asynchronously
(function(d){
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.js';
ref.parentNode.insertBefore(js, ref);
}(document));
/* (function() {
var e = document.createElement('script'); e.async = true;
e.src = document.location.protocol
+ '//connect.facebook.net/en_US/all.js';
document.getElementById('fb-root').appendChild(e);
}());
*/