Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added session expired error handling.

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...
commit 1bff2f1bbf4258e70857e5864b40392e2bf938b1 1 parent db90ce0
Kamil Kraszewski authored Kamil Kraszewski committed
View
24 sample/Hackbook/Hackbook/RootViewController.m
@@ -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
@@ -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
View
5 src/FBRequest.m
@@ -242,6 +242,7 @@ - (void)failWithError:(NSError *)error {
if ([_delegate respondsToSelector:@selector(request:didFailWithError:)]) {
[_delegate request:self didFailWithError:error];
}
+ self.state = kFBRequestStateError;
}
/*
@@ -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 {
View
11 src/Facebook.h
@@ -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
View
40 src/Facebook.m
@@ -30,7 +30,7 @@
static NSString* kSDK = @"ios";
static NSString* kSDKVersion = @"2";
-static NSString *requestFinishedKeyPath = @"finished";
+static NSString *requestFinishedKeyPath = @"state";
static void *finishedContext = @"finishedContext";
///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -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.
*
@@ -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];
@@ -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];
+ }
}
/**
Please sign in to comment.
Something went wrong with that request. Please try again.