From 20ad7449c757d9d73afd8a3afe7e80261ce8e166 Mon Sep 17 00:00:00 2001 From: Michael Katz Date: Fri, 20 May 2016 14:29:44 -0400 Subject: [PATCH] cherry pick Work around login crash #728 --- Source/LoggingAnalyticsTracker.swift | 2 +- Source/OEXAnalyticsTracker.h | 2 +- Source/OEXAuthentication.m | 12 +++++++++++- Source/OEXSession.m | 7 ++++++- Source/SegmentAnalyticsTracker.swift | 8 ++++---- Test/MockAnalyticsTracker.swift | 2 +- 6 files changed, 24 insertions(+), 9 deletions(-) diff --git a/Source/LoggingAnalyticsTracker.swift b/Source/LoggingAnalyticsTracker.swift index 3fd84a3671..6757bcd2d0 100644 --- a/Source/LoggingAnalyticsTracker.swift +++ b/Source/LoggingAnalyticsTracker.swift @@ -11,7 +11,7 @@ import UIKit class LoggingAnalyticsTracker: NSObject, OEXAnalyticsTracker { private let ANALYTICS = "ANALYTICS" - func identifyUser(user: OEXUserDetails) { + func identifyUser(user: OEXUserDetails?) { Logger.logInfo(ANALYTICS, "Identified User: \(user)") } diff --git a/Source/OEXAnalyticsTracker.h b/Source/OEXAnalyticsTracker.h index c9094ea186..ea09de0245 100644 --- a/Source/OEXAnalyticsTracker.h +++ b/Source/OEXAnalyticsTracker.h @@ -15,7 +15,7 @@ NS_ASSUME_NONNULL_BEGIN @protocol OEXAnalyticsTracker -- (void)identifyUser:(OEXUserDetails*)user; +- (void)identifyUser:(nullable OEXUserDetails*)user; - (void)clearIdentifiedUser; - (void)trackEvent:(OEXAnalyticsEvent*)event forComponent:(nullable NSString*)component withProperties:(NSDictionary*)properties; diff --git a/Source/OEXAuthentication.m b/Source/OEXAuthentication.m index 1727a18d4d..e13b4fdba4 100644 --- a/Source/OEXAuthentication.m +++ b/Source/OEXAuthentication.m @@ -9,6 +9,7 @@ #import "OEXAuthentication.h" #import "NSDictionary+OEXEncoding.h" +#import "NSError+OEXKnownErrors.h" #import "NSMutableDictionary+OEXSafeAccess.h" #import "NSString+OEXFormatting.h" @@ -189,7 +190,16 @@ + (void)handleSuccessfulLoginWithToken:(OEXAccessToken*)token completionHandler: if(httpResp.statusCode == 200) { NSDictionary* dictionary = [NSJSONSerialization JSONObjectWithData:userdata options:kNilOptions error:nil]; OEXUserDetails* userDetails = [[OEXUserDetails alloc] initWithUserDictionary:dictionary]; - [[OEXSession sharedSession] saveAccessToken:token userDetails:userDetails]; + if(token != nil && userDetails != nil) { + [[OEXSession sharedSession] saveAccessToken:token userDetails:userDetails]; + } + else { + // On the off chance that something messed up and we have nil + // for token or user details, + // stub in some error values + usererror = [NSError oex_unknownError]; + userresponse = [[NSHTTPURLResponse alloc] initWithURL:userresponse.URL statusCode:OEXHTTPStatusCode400BadRequest HTTPVersion:nil headerFields:nil]; + } } dispatch_async(dispatch_get_main_queue(), ^{ OEXWrapURLCompletion(completionHandler)(userdata, userresponse, usererror); diff --git a/Source/OEXSession.m b/Source/OEXSession.m index 9aee869f42..e912b0f29a 100644 --- a/Source/OEXSession.m +++ b/Source/OEXSession.m @@ -61,7 +61,12 @@ - (id)init { - (void)saveAccessToken:(OEXAccessToken*)token userDetails:(OEXUserDetails*)userDetails { [self.credentialStore clear]; [self.credentialStore saveAccessToken:token userDetails:userDetails]; - [self loadTokenFromStore]; + + self.token = token; + self.currentUser = userDetails; + if(token != nil && userDetails != nil) { + [[NSNotificationCenter defaultCenter] postNotificationName:OEXSessionStartedNotification object:nil userInfo:@{OEXSessionStartedUserDetailsKey : userDetails}]; + } } - (void)loadTokenFromStore { diff --git a/Source/SegmentAnalyticsTracker.swift b/Source/SegmentAnalyticsTracker.swift index 7a91d452ae..0f5b987a82 100644 --- a/Source/SegmentAnalyticsTracker.swift +++ b/Source/SegmentAnalyticsTracker.swift @@ -25,13 +25,13 @@ class SegmentAnalyticsTracker : NSObject, OEXAnalyticsTracker { } } - func identifyUser(user : OEXUserDetails) { - if let userID = user.userId { + func identifyUser(user : OEXUserDetails?) { + if let userID = user?.userId { var traits : [String:AnyObject] = [:] - if let email = user.email { + if let email = user?.email { traits[key_email] = email } - if let username = user.username { + if let username = user?.username { traits[key_username] = username } SEGAnalytics.sharedAnalytics().identify(userID.description, traits:traits) diff --git a/Test/MockAnalyticsTracker.swift b/Test/MockAnalyticsTracker.swift index 11a897a71e..c3f7ce7bac 100644 --- a/Test/MockAnalyticsTracker.swift +++ b/Test/MockAnalyticsTracker.swift @@ -61,7 +61,7 @@ class MockAnalyticsTracker : NSObject, OEXAnalyticsTracker { let eventStream = Sink() - func identifyUser(user: OEXUserDetails) { + func identifyUser(user: OEXUserDetails?) { currentUser = user }