Skip to content

Commit

Permalink
New method to re-authorize based on an existing auth token
Browse files Browse the repository at this point in the history
so we don't have to send our users through the subpar already-authorized
login experience.  Store the token and expiration in window.localstorage
and try to reauthorize before you authorize normally.
  • Loading branch information
ex-nerd committed Jun 28, 2011
1 parent b2456e4 commit f5b1b46
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
Expand Up @@ -54,6 +54,8 @@ public PluginResult execute(String action, JSONArray args, String callbackId) {

if (action.equals("authorize")) {
this.authorize(first); // first arg is APP_ID
} else if (action.equals("reauthorize")) {
this.reauthorize(first, args); // first arg is APP_ID
} else if (action.equals("request")){
this.getResponse(first); // first arg is path
} else if (action.equals("getAccess")){
Expand Down Expand Up @@ -150,13 +152,41 @@ public void authorize(final String appid) {
final FacebookAuth fba = this;
Runnable runnable = new Runnable() {
public void run() {
fba.mFb = new Facebook(appid);
fba.mFb.setPlugin(fba);
if (fba.mFb == null) {
fba.mFb = new Facebook(appid);
fba.mFb.setPlugin(fba);
}
fba.mFb.authorize((Activity) fba.ctx, fba.permissions, new AuthorizeListener(fba));
};
};
this.ctx.runOnUiThread(runnable);
}

/**
* Validate an existing token and expiration, and use them for the Facebook
* session, rather than re-authenticating the app (which presents a rather
* user experience in mobile).
*
* @return true if ok, or print a stack trace
*/
public void reauthorize(final String appid, JSONArray args) {
Log.d("PhoneGapLog", "reauthorize");
final FacebookAuth fba = this;
final String access_token = args.optString(1, null);
final Long expires = args.optLong(2, -1);
try {
if (fba.mFb == null) {
fba.mFb = new Facebook(appid);
fba.mFb.setPlugin(fba);
}
if (access_token != null && expires != -1) {
fba.mFb.setAccessToken(access_token);
fba.mFb.setAccessExpires(expires);
}
} catch (JSONException e) {
e.printStackTrace();
}
this.success(new PluginResult(PluginResult.Status.OK, fba.mFb.isSessionValid()), this.callback);
}

class AuthorizeListener implements DialogListener {
Expand Down
4 changes: 4 additions & 0 deletions Android/Facebook/www/facebook.js
Expand Up @@ -14,6 +14,10 @@
PhoneGap.exec(callback, null, "FacebookAuth", "authorize", [app_id]);
};

Facebook.prototype.reauthorize = function(app_id, token, expires, callback) {
PhoneGap.exec(callback, null, "FacebookAuth", "reauthorize", [app_id, token, expires]);
};

Facebook.prototype.setPermissions = function(permissions, callback) {
PhoneGap.exec(callback, null, "FacebookAuth", "setPermissions", permissions);
};
Expand Down

0 comments on commit f5b1b46

Please sign in to comment.