diff options
author | Xiangtian Dai <xiangtian@google.com> | 2017-10-03 09:58:47 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-10-03 09:58:47 -0700 |
commit | 4b1b3d62d8f72a19e0359642872edd3393451f47 (patch) | |
tree | af76ebec8f1c9047a771ab1a08a8118903491923 /Firebase/Auth | |
parent | bde743ed25166a0b320ae157bfb1d68064f531c9 (diff) |
Simplifies logic to post auth state change notifications, which is also applied more consistently now. (#325)
Diffstat (limited to 'Firebase/Auth')
-rw-r--r-- | Firebase/Auth/Source/FIRAuth.m | 24 | ||||
-rw-r--r-- | Firebase/Auth/Source/FIRAuth_Internal.h | 9 | ||||
-rw-r--r-- | Firebase/Auth/Source/FIRUser.m | 2 |
3 files changed, 20 insertions, 15 deletions
diff --git a/Firebase/Auth/Source/FIRAuth.m b/Firebase/Auth/Source/FIRAuth.m index 245e600..7b36ac6 100644 --- a/Firebase/Auth/Source/FIRAuth.m +++ b/Firebase/Auth/Source/FIRAuth.m @@ -239,6 +239,11 @@ static NSMutableDictionary *gKeychainServiceNameForAppName; */ FIRAuthKeychain *_keychain; + /** @var _lastNotifiedUserToken + @brief The user access (ID) token used last time for posting auth state changed notification. + */ + NSString *_lastNotifiedUserToken; + /** @var _autoRefreshTokens @brief This flag denotes whether or not tokens should be automatically refreshed. @remarks Will only be set to @YES if the another Firebase service is included (additionally to @@ -430,6 +435,7 @@ static NSMutableDictionary *gKeychainServiceNameForAppName; NSError *error; if ([strongSelf getUser:&user error:&error]) { [strongSelf updateCurrentUser:user byForce:NO savingToDisk:NO error:&error]; + _lastNotifiedUserToken = user.rawAccessToken; } else { FIRLogError(kFIRLoggerAuth, @"I-AUT000001", @"Error loading saved user when starting up: %@", error); @@ -1086,10 +1092,15 @@ static NSMutableDictionary *gKeychainServiceNameForAppName; } #endif -- (void)notifyListenersOfAuthStateChangeWithUser:(FIRUser *)user token:(NSString *)token { - if (user != _currentUser) { +/** @fn possiblyPostAuthStateChangeNotification + @brief Posts the auth state change notificaton if current user's token has been changed. + */ +- (void)possiblyPostAuthStateChangeNotification { + NSString *token = _currentUser.rawAccessToken; + if (_lastNotifiedUserToken == token || [_lastNotifiedUserToken isEqualToString:token]) { return; } + _lastNotifiedUserToken = token; if (_autoRefreshTokens) { // Shedule new refresh task after successful attempt. [self scheduleAutoTokenRefresh]; @@ -1117,7 +1128,11 @@ static NSMutableDictionary *gKeychainServiceNameForAppName; // whether the user is still current on other callbacks of user operations either. return YES; } - return [self saveUser:user error:error]; + if ([self saveUser:user error:error]) { + [self possiblyPostAuthStateChangeNotification]; + return YES; + } + return NO; } /** @fn setKeychainServiceNameForApp @@ -1336,6 +1351,7 @@ static NSMutableDictionary *gKeychainServiceNameForAppName; savingToDisk:(BOOL)saveToDisk error:(NSError *_Nullable *_Nullable)error { if (user == _currentUser) { + [self possiblyPostAuthStateChangeNotification]; return YES; } BOOL success = YES; @@ -1344,7 +1360,7 @@ static NSMutableDictionary *gKeychainServiceNameForAppName; } if (success || force) { _currentUser = user; - [self notifyListenersOfAuthStateChangeWithUser:user token:user.rawAccessToken]; + [self possiblyPostAuthStateChangeNotification]; } return success; } diff --git a/Firebase/Auth/Source/FIRAuth_Internal.h b/Firebase/Auth/Source/FIRAuth_Internal.h index 44a754c..4d87a08 100644 --- a/Firebase/Auth/Source/FIRAuth_Internal.h +++ b/Firebase/Auth/Source/FIRAuth_Internal.h @@ -75,15 +75,6 @@ NS_ASSUME_NONNULL_BEGIN */ - (nullable NSString *)getUID; -/** @fn notifyListenersOfAuthStateChange - @brief Posts the @c FIRAuthStateDidChangeNotification notification. - @remarks Called by @c FIRUser when token changes occur. - @param user The user whose tokens changed. - @param token The new access token associated with the user. - */ -- (void)notifyListenersOfAuthStateChangeWithUser:(nullable FIRUser *)user - token:(nullable NSString *)token; - /** @fn updateKeychainWithUser:error: @brief Updates the keychain for the given user. @param user The user to be updated. diff --git a/Firebase/Auth/Source/FIRUser.m b/Firebase/Auth/Source/FIRUser.m index c4396ad..0e56da4 100644 --- a/Firebase/Auth/Source/FIRUser.m +++ b/Firebase/Auth/Source/FIRUser.m @@ -518,7 +518,6 @@ static void callInMainThreadWithAuthDataResultAndError( callback(error); return; } - [_auth notifyListenersOfAuthStateChangeWithUser:self token:token]; callback(nil); }]; } @@ -820,7 +819,6 @@ static void callInMainThreadWithAuthDataResultAndError( callback(nil, error); return; } - [_auth notifyListenersOfAuthStateChangeWithUser:self token:token]; } callback(token, nil); }]; |