Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix another place where should use removeItem() instead of set to undefined. #25

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
45 changes: 37 additions & 8 deletions openfb.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@
* @author Christophe Coenraets @ccoenraets
* @version 0.4
*/
var openFB = (function () {
var createOpenFB = function () {

var FB_LOGIN_URL = 'https://www.facebook.com/dialog/oauth',
FB_LOGOUT_URL = 'https://www.facebook.com/logout.php',

// By default we store fbtoken in sessionStorage. This can be overridden in init()
tokenStore = window.sessionStorage,

tokenKey = 'fbtoken',

fbAppId,

context = window.location.pathname.substring(0, window.location.pathname.indexOf("/",2)),
Expand Down Expand Up @@ -59,14 +61,40 @@ var openFB = (function () {
if (params.tokenStore) {
tokenStore = params.tokenStore;
}

if (params.tokenKey) {
tokenKey = params.tokenKey;
}

if (params.loginUrl) {
FB_LOGIN_URL = params.loginUrl;
}

if (params.logoutUrl) {
FB_LOGOUT_URL = params.logoutUrl;
}

if (params.runningInCordova) {
runningInCordova = true;
oauthRedirectURL = "https://www.facebook.com/connect/login_success.html";
logoutRedirectURL = "https://www.facebook.com/connect/login_success.html";
}

if (params.oauthRedirectURL) {
oauthRedirectURL = params.oauthRedirectURL;
}

if (params.logoutRedirectURL) {
logoutRedirectURL = params.logoutRedirectURL;
}
}

/**
* Checks if the user has logged in with openFB and currently has a session api token.
* @param callback the function that receives the loginstatus
*/
function getLoginStatus(callback) {
var token = tokenStore['fbtoken'],
var token = tokenStore[tokenKey],
loginStatus = {};
if (token) {
loginStatus.status = 'connected';
Expand Down Expand Up @@ -159,7 +187,7 @@ var openFB = (function () {
if (url.indexOf("access_token=") > 0) {
queryString = url.substr(url.indexOf('#') + 1);
obj = parseQueryString(queryString);
tokenStore['fbtoken'] = obj['access_token'];
tokenStore[tokenKey] = obj['access_token'];
if (loginCallback) loginCallback({status: 'connected', authResponse: {token: obj['access_token']}});
} else if (url.indexOf("error=") > 0) {
queryString = url.substring(url.indexOf('?') + 1, url.indexOf('#'));
Expand All @@ -177,10 +205,10 @@ var openFB = (function () {
*/
function logout(callback) {
var logoutWindow,
token = tokenStore['fbtoken'];
token = tokenStore[tokenKey];

/* Remove token. Will fail silently if does not exist */
tokenStore.removeItem('fbtoken');
tokenStore.removeItem(tokenKey);

if (token) {
logoutWindow = window.open(FB_LOGOUT_URL + '?access_token=' + token + '&next=' + logoutRedirectURL, '_blank', 'location=no');
Expand Down Expand Up @@ -213,7 +241,7 @@ var openFB = (function () {
xhr = new XMLHttpRequest(),
url;

params['access_token'] = tokenStore['fbtoken'];
params['access_token'] = tokenStore[tokenKey];

url = 'https://graph.facebook.com' + obj.path + '?' + toQueryString(params);

Expand Down Expand Up @@ -242,7 +270,8 @@ var openFB = (function () {
return api({method: 'DELETE',
path: '/me/permissions',
success: function () {
tokenStore['fbtoken'] = undefined;
/* Remove token. Will fail silently if does not exist */
tokenStore.removeItem(tokenKey);
success();
},
error: error});
Expand Down Expand Up @@ -280,4 +309,4 @@ var openFB = (function () {
getLoginStatus: getLoginStatus
}

}());
};