-
-
Save olivoil/4380058 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<script type="text/javascript" charset="utf-8"> | |
var FB = function(){ | |
//defaults and accessors | |
var initialized = false, | |
loggedIn = null, | |
shouldSucceed = true, | |
uid = '12345'; | |
function loggedOutStatus(){ | |
return { | |
perms: null, | |
session: null, | |
status: 'unknown' | |
}; | |
} | |
function loggedInStatus(){ | |
return { | |
status: "connected", | |
session: { | |
uid: uid, | |
session_key: "theSessionKey", | |
secret: "theSecret", | |
expires: 0, | |
base_domain: window.location.hostname, | |
access_token:"theAccessToken", | |
sig:"theSig"} | |
}; | |
} | |
var selectedPerms = '"{"extended":["status_update","photo_upload","video_upload","offline_access","email","create_note","share_item","publish_stream","contact_email"],"user":["manage_friendlists","create_event","read_requests","manage_pages"],"friends":[]}"'; | |
// begin api | |
function init(data){ | |
initialized = true; | |
loggedIn = true; | |
} | |
function login(callback){ | |
if(!initialized){ | |
warnBeforeCall('login'); | |
return; | |
} | |
if(loggedIn){ | |
console.log('FB.login() called when user is already connected.'); | |
} | |
if(shouldSucceed){ | |
loggedIn = true; | |
var response = loggedInStatus(); | |
response.perms = null; | |
callback(response); | |
}else{ | |
callback(loggedOutStatus()); | |
} | |
} | |
function logout(callback){ | |
if(!initialized){ | |
warnBeforeCall('logout'); | |
return; | |
} | |
if(!loggedIn){ | |
console.log('FB.logout() called without a session.'); | |
} | |
loggedIn = false; | |
callback(loggedOutStatus()); | |
} | |
function getLoginStatus(callback, perms){ | |
if(!initialized){ | |
warnBeforeCall('getLoginStatus'); | |
return; | |
} | |
if(!loggedIn || !shouldSucceed){ | |
callback(loggedOutStatus()); | |
return; | |
} | |
var response = loggedInStatus(); | |
if(typeof perms != 'undefined'){ | |
response.perms = selectedPerms; | |
} | |
callback(response); | |
} | |
function getSession(){ | |
if(!initialized){ | |
warnBeforeCall('getSession'); | |
return null; | |
} | |
if(!loggedIn || !shouldSucceed){ | |
return loggedOutStatus(); | |
}else{ | |
return loggedInStatus().session; | |
} | |
} | |
// end api | |
//begin helpers | |
function setUid(newUid){ | |
uid = newUid; | |
} | |
function succeed(){ | |
shouldSucceed = true; | |
} | |
function fail(){ | |
shouldSucceed = false; | |
} | |
//private | |
function warnBeforeCall(meth){ | |
console.log("FB."+meth+" called before FB.init"); | |
} | |
return { | |
getLoginStatus: getLoginStatus, | |
logout: logout, | |
login: login, | |
init: init, | |
getSession: getSession, | |
setUid: setUid, | |
succeed: succeed, | |
fail: fail | |
}; | |
}(); | |
FB.init({}); | |
FB.getLoginStatus(function(data){ | |
console.log('getLoginStatus', data); | |
}); | |
FB.logout(function(data){ | |
console.log('logout', data); | |
}); | |
FB.setUid('54321'); | |
FB.getLoginStatus(function(data){ | |
console.log('getLoginStatus', data); | |
}); | |
FB.fail(); | |
FB.login(function(data){ | |
console.log('login', data); | |
}); | |
FB.getLoginStatus(function(data){ | |
console.log('getLoginStatus', data); | |
}); | |
FB.succeed(); | |
FB.login(function(data){ | |
console.log('login', data); | |
}); | |
FB.getLoginStatus(function(data){ | |
console.log('getLoginStatus', data); | |
}, true); | |
console.log('getSession', FB.getSession()); | |
</script> | |
</head> | |
<body> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment