Skip to content

Commit

Permalink
Added session expired error handling.
Browse files Browse the repository at this point in the history
Summary: Whenever we call an API that returns a "session expired" error we
should clear the session data (acessToken / expiring date) and notify user about
it.

Test Plan: Login to app, then log out using www, then try calling rest / graph
method.

Reviewers: yariv, jimbru, brent, toddkrabach, jonathan

Reviewed By: yariv

CC: lshepard, yariv, kamil, brent, jonathan, toddkrabach

Differential Revision: 378197
  • Loading branch information
Kamil Kraszewski authored and Kamil Kraszewski committed Dec 19, 2011
1 parent db90ce0 commit 1bff2f1
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 26 deletions.
24 changes: 16 additions & 8 deletions sample/Hackbook/Hackbook/RootViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,22 @@ - (void)fbDidLogout {
[self showLoggedOut];
}

/**
* Called when the session has expired.
*/
- (void)fbSessionInvalidated {
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:@"Auth Exception"
message:@"Your session has expired."
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil,
nil];
[alertView show];
[alertView release];
[self fbDidLogout];
}

#pragma mark - FBRequestDelegate Methods
/**
* Called when the Facebook API request has returned a response. This callback
Expand Down Expand Up @@ -430,14 +446,6 @@ - (void)request:(FBRequest *)request didLoad:(id)result {
- (void)request:(FBRequest *)request didFailWithError:(NSError *)error {
NSLog(@"Err message: %@", [[error userInfo] objectForKey:@"error_msg"]);
NSLog(@"Err code: %d", [error code]);

// Show logged out state if:
// 1. the app is no longer authorized
// 2. the user logged out of Facebook from m.facebook.com or the Facebook app
// 3. the user has changed their password
if ([error code] == 190) {
[self fbDidLogout];
}
}

@end
5 changes: 4 additions & 1 deletion src/FBRequest.m
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ - (void)failWithError:(NSError *)error {
if ([_delegate respondsToSelector:@selector(request:didFailWithError:)]) {
[_delegate request:self didFailWithError:error];
}
self.state = kFBRequestStateError;
}

/*
Expand Down Expand Up @@ -355,7 +356,9 @@ - (void)connectionDidFinishLoading:(NSURLConnection *)connection {
self.responseText = nil;
self.connection = nil;

self.state = kFBRequestStateComplete;
if (self.state != kFBRequestStateError) {
self.state = kFBRequestStateComplete;
}
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
Expand Down
11 changes: 10 additions & 1 deletion src/Facebook.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,13 @@
*/
- (void)fbDidLogout;

@end
/**
* Called when the current session has expired. This might happen when:
* - the access token expired
* - the app has been disabled
* - the user revoked the app's permissions
* - the user changed his or her password
*/
- (void)fbSessionInvalidated;

@end
40 changes: 24 additions & 16 deletions src/Facebook.m
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
static NSString* kSDK = @"ios";
static NSString* kSDKVersion = @"2";

static NSString *requestFinishedKeyPath = @"finished";
static NSString *requestFinishedKeyPath = @"state";
static void *finishedContext = @"finishedContext";

///////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -122,6 +122,19 @@ - (void)dealloc {
[super dealloc];
}

- (void)invalidateSession {
self.accessToken = nil;
self.expirationDate = nil;

NSHTTPCookieStorage* cookies = [NSHTTPCookieStorage sharedHTTPCookieStorage];
NSArray* facebookCookies = [cookies cookiesForURL:
[NSURL URLWithString:@"http://login.facebook.com"]];

for (NSHTTPCookie* cookie in facebookCookies) {
[cookies deleteCookie:cookie];
}
}

/**
* A private helper function for sending HTTP requests.
*
Expand Down Expand Up @@ -161,6 +174,12 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(N
if (context == finishedContext) {
FBRequest* _request = (FBRequest*)object;
FBRequestState requestState = [_request state];
if (requestState == kFBRequestStateError) {
[self invalidateSession];
if ([self.sessionDelegate respondsToSelector:@selector(fbSessionInvalidated)]) {
[self.sessionDelegate fbSessionInvalidated];
}
}
if (requestState == kFBRequestStateComplete || requestState == kFBRequestStateError) {
[_request removeObserver:self forKeyPath:requestFinishedKeyPath];
[_requests removeObject:_request];
Expand Down Expand Up @@ -388,22 +407,11 @@ - (BOOL)handleOpenURL:(NSURL *)url {
* settings screen on facebook.com.
*/
- (void)logout {
[_accessToken release];
_accessToken = nil;
[_expirationDate release];
_expirationDate = nil;

NSHTTPCookieStorage* cookies = [NSHTTPCookieStorage sharedHTTPCookieStorage];
NSArray* facebookCookies = [cookies cookiesForURL:
[NSURL URLWithString:@"http://login.facebook.com"]];
[self invalidateSession];

for (NSHTTPCookie* cookie in facebookCookies) {
[cookies deleteCookie:cookie];
}

if ([self.sessionDelegate respondsToSelector:@selector(fbDidLogout)]) {
[self.sessionDelegate fbDidLogout];
}
if ([self.sessionDelegate respondsToSelector:@selector(fbDidLogout)]) {
[self.sessionDelegate fbDidLogout];
}
}

/**
Expand Down

0 comments on commit 1bff2f1

Please sign in to comment.