Skip to content

Commit

Permalink
Store/update/clear session information inside native code to better m…
Browse files Browse the repository at this point in the history
…anage session.
  • Loading branch information
Christine Abernathy committed Mar 28, 2012
1 parent 71aead6 commit 90609f2
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 33 deletions.
34 changes: 22 additions & 12 deletions native/ios/FacebookConnectPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,12 @@ - (void) init:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options
NSString* appId = [arguments objectAtIndex:1];
self.facebook = [[Facebook alloc] initWithAppId:appId andDelegate: self];

// Check for any stored session read during init and update Facebook session information
if ([options objectForKey:@"accessToken"] && [options objectForKey:@"expiresIn"]) {
NSString *expTime = [options objectForKey:@"expiresIn"];
NSDate *expirationDate = [NSDate distantFuture];
if (expTime != nil) {
int expVal = [expTime intValue];
if (expVal != 0) {
expirationDate = [NSDate dateWithTimeIntervalSinceNow:expVal];
}
}
facebook.accessToken = [options objectForKey:@"accessToken"];
facebook.expirationDate = expirationDate;
// Check for any stored session update Facebook session information
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:@"FBAccessTokenKey"]
&& [defaults objectForKey:@"FBExpirationDateKey"]) {
self.facebook.accessToken = [defaults objectForKey:@"FBAccessTokenKey"];
self.facebook.expirationDate = [defaults objectForKey:@"FBExpirationDateKey"];
}

PluginResult* result = [PluginResult resultWithStatus:PGCommandStatus_OK];
Expand Down Expand Up @@ -194,6 +188,12 @@ - (NSDictionary*) responseObject
*/
- (void) fbDidLogin
{
// Store session information
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[self.facebook accessToken] forKey:@"FBAccessTokenKey"];
[defaults setObject:[self.facebook expirationDate] forKey:@"FBExpirationDateKey"];
[defaults synchronize];

[facebook requestWithGraphPath:@"me" andDelegate:self];

PluginResult* pluginResult = [PluginResult resultWithStatus:PGCommandStatus_OK messageAsDictionary:
Expand All @@ -206,13 +206,23 @@ - (void) fbDidLogin
}

- (void)fbDidLogout {
// Cleared stored session information
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults removeObjectForKey:@"FBAccessTokenKey"];
[defaults removeObjectForKey:@"FBExpirationDateKey"];
[defaults synchronize];
}

- (void)fbDidNotLogin:(BOOL)cancelled {
}

- (void)fbDidExtendToken:(NSString*)accessToken
expiresAt:(NSDate*)expiresAt {
// Updated stored session information
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:accessToken forKey:@"FBAccessTokenKey"];
[defaults setObject:expiresAt forKey:@"FBExpirationDateKey"];
[defaults synchronize];
}

- (void)fbSessionInvalidated {
Expand Down
31 changes: 10 additions & 21 deletions www/pg-plugin-fb-connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,21 @@ PG.FB = {
elem.id = 'fb-root';
document.body.appendChild(elem);
}
// Check local storage session for initialization
var storedSession = {};
if (localStorage.getItem('pg_fb_session')) {
var checkSession = JSON.parse(localStorage.getItem('pg_fb_session'));
if (checkSession && checkSession.accessToken && checkSession.expirationTime) {
var nowTime = (new Date()).getTime();
if (checkSession.expirationTime > nowTime) {
var updatedExpiresIn = Math.floor((checkSession.expirationTime - nowTime) / 1000);
checkSession.expiresIn = updatedExpiresIn;
// Update expires in info in local storage
localStorage.setItem('pg_fb_session', JSON.stringify(checkSession));
storedSession = {"accessToken":checkSession.accessToken,
"expiresIn":checkSession.expiresIn};
}
}
}
PhoneGap.exec(function() {
var authResponse = JSON.parse(localStorage.getItem('pg_fb_session') || '{"expiresIn":0}');
var authResponse = JSON.parse(localStorage.getItem('pg_fb_session') || '{"expiresIn":0}');
if (authResponse && authResponse.expirationTime) {
var nowTime = (new Date()).getTime();
if (authResponse && authResponse.accessToken
&& authResponse.expirationTime && (authResponse.expirationTime > nowTime)) {
// Set auth response to simulate a successful login
if (authResponse.expirationTime > nowTime) {
// Update expires in information
updatedExpiresIn = Math.floor((authResponse.expirationTime - nowTime) / 1000);
authResponse.expiresIn = updatedExpiresIn;

localStorage.setItem('pg_fb_session', JSON.stringify(authResponse));
FB.Auth.setAuthResponse(authResponse, 'connected');
}
}
console.log('PhoneGap Facebook Connect plugin initialized successfully.');
}, (fail?fail:null), 'com.phonegap.facebook.Connect', 'init', [apiKey, storedSession]);
}, (fail?fail:null), 'com.phonegap.facebook.Connect', 'init', [apiKey]);
},
login: function(params, cb, fail) {
params = params || { scope: '' };
Expand Down

0 comments on commit 90609f2

Please sign in to comment.