aboutsummaryrefslogtreecommitdiffhomepage
path: root/Firebase
diff options
context:
space:
mode:
authorGravatar Xiangtian Dai <xiangtian@google.com>2017-06-30 14:25:02 -0700
committerGravatar GitHub <noreply@github.com>2017-06-30 14:25:02 -0700
commitfb5aa974eedeada8de8ffb30c0378d528ae870f9 (patch)
treef1b004842280f2f24ab6dd34b7a88ceb04b9ef82 /Firebase
parent8743564d9f6d5a0ea893555ed17bedc786bc9aa9 (diff)
Moves most of FIRAuth initialization out of the calling thread. (#115)
* Adds an option to time initialization of FIRAuth instance in the sample app. * Changes deployment target from 10.3 to 8.0 for Auth samples. * Fixes some Xcode warnings in the Auth sample app. * Moves most of `FIRAuth` initialization to the work thread. * Fixes a typo. * Fixes accessing of public `currentUser` method from internal method. * Fixes tests for macOS. * Addresses review comments.
Diffstat (limited to 'Firebase')
-rw-r--r--Firebase/Auth/Source/FIRAuth.m88
-rw-r--r--Firebase/Auth/Source/FIRAuth_Internal.h6
-rw-r--r--Firebase/Auth/Source/FIRUser.m2
3 files changed, 64 insertions, 32 deletions
diff --git a/Firebase/Auth/Source/FIRAuth.m b/Firebase/Auth/Source/FIRAuth.m
index 9363069..7a25919 100644
--- a/Firebase/Auth/Source/FIRAuth.m
+++ b/Firebase/Auth/Source/FIRAuth.m
@@ -199,6 +199,11 @@ static NSMutableDictionary *gKeychainServiceNameForAppName;
@end
@implementation FIRAuth {
+ /** @var _currentUser
+ @brief The current user.
+ */
+ FIRUser *_currentUser;
+
/** @var _firebaseAppName
@brief The Firebase app name.
*/
@@ -315,6 +320,7 @@ static NSMutableDictionary *gKeychainServiceNameForAppName;
app.getTokenImplementation = ^(BOOL forceRefresh, FIRTokenCallback callback) {
dispatch_async(FIRAuthGlobalWorkQueue(), ^{
FIRAuth *strongSelf = weakSelf;
+ // Enable token auto-refresh if not aleady enabled.
if (strongSelf && !strongSelf->_autoRefreshTokens) {
FIRLogInfo(kFIRLoggerAuth, @"I-AUT000002", @"Token auto-refresh enabled.");
strongSelf->_autoRefreshTokens = YES;
@@ -346,15 +352,17 @@ static NSMutableDictionary *gKeychainServiceNameForAppName;
}];
#endif
}
- if (!strongSelf.currentUser) {
+ // Call back with 'nil' if there is no current user.
+ if (!strongSelf || !strongSelf->_currentUser) {
dispatch_async(dispatch_get_main_queue(), ^{
callback(nil, nil);
});
return;
}
- [strongSelf.currentUser internalGetTokenForcingRefresh:forceRefresh
- callback:^(NSString *_Nullable token,
- NSError *_Nullable error) {
+ // Call back with current user token.
+ [strongSelf->_currentUser internalGetTokenForcingRefresh:forceRefresh
+ callback:^(NSString *_Nullable token,
+ NSError *_Nullable error) {
dispatch_async(dispatch_get_main_queue(), ^{
callback(token, error);
});
@@ -378,33 +386,47 @@ static NSMutableDictionary *gKeychainServiceNameForAppName;
_listenerHandles = [NSMutableArray array];
_APIKey = [APIKey copy];
_firebaseAppName = [appName copy];
- NSString *keychainServiceName = [FIRAuth keychainServiceNameForAppName:appName];
- if (keychainServiceName) {
- _keychain = [[FIRAuthKeychain alloc] initWithService:keychainServiceName];
- }
- // Load current user from keychain.
- FIRUser *user;
- NSError *error;
- if ([self getUser:&user error:&error]) {
- [self updateCurrentUser:user byForce:NO savingToDisk:NO error:&error];
- } else {
- FIRLogError(kFIRLoggerAuth, @"I-AUT000001",
- @"Error loading saved user when starting up: %@", error);
- }
-
#if TARGET_OS_IOS
- // Initialize for phone number auth.
- _tokenManager =
- [[FIRAuthAPNSTokenManager alloc] initWithApplication:[UIApplication sharedApplication]];
+ UIApplication *application = [UIApplication sharedApplication];
+ #endif
+
+ // Continue with the rest of initialization in the work thread.
+ __weak FIRAuth *weakSelf = self;
+ dispatch_async(FIRAuthGlobalWorkQueue(), ^{
+ // Load current user from Keychain.
+ FIRAuth *strongSelf = weakSelf;
+ if (!strongSelf) {
+ return;
+ }
+ NSString *keychainServiceName =
+ [FIRAuth keychainServiceNameForAppName:strongSelf->_firebaseAppName];
+ if (keychainServiceName) {
+ strongSelf->_keychain = [[FIRAuthKeychain alloc] initWithService:keychainServiceName];
+ }
+ FIRUser *user;
+ NSError *error;
+ if ([strongSelf getUser:&user error:&error]) {
+ [strongSelf updateCurrentUser:user byForce:NO savingToDisk:NO error:&error];
+ } else {
+ FIRLogError(kFIRLoggerAuth, @"I-AUT000001",
+ @"Error loading saved user when starting up: %@", error);
+ }
- _appCredentialManager = [[FIRAuthAppCredentialManager alloc] initWithKeychain:_keychain];
+ #if TARGET_OS_IOS
+ // Initialize for phone number auth.
+ strongSelf->_tokenManager =
+ [[FIRAuthAPNSTokenManager alloc] initWithApplication:application];
- _notificationManager =
- [[FIRAuthNotificationManager alloc] initWithApplication:[UIApplication sharedApplication]
- appCredentialManager:_appCredentialManager];
+ strongSelf->_appCredentialManager =
+ [[FIRAuthAppCredentialManager alloc] initWithKeychain:strongSelf->_keychain];
- [[FIRAuthAppDelegateProxy sharedInstance] addHandler:self];
- #endif
+ strongSelf->_notificationManager = [[FIRAuthNotificationManager alloc]
+ initWithApplication:application
+ appCredentialManager:strongSelf->_appCredentialManager];
+
+ [[FIRAuthAppDelegateProxy sharedInstance] addHandler:strongSelf];
+ #endif
+ });
}
return self;
}
@@ -431,6 +453,14 @@ static NSMutableDictionary *gKeychainServiceNameForAppName;
#pragma mark - Public API
+- (FIRUser *)currentUser {
+ __block FIRUser *result;
+ dispatch_sync(FIRAuthGlobalWorkQueue(), ^{
+ result = _currentUser;
+ });
+ return result;
+}
+
- (void)fetchProvidersForEmail:(NSString *)email
completion:(FIRProviderQueryCallback)completion {
dispatch_async(FIRAuthGlobalWorkQueue(), ^{
@@ -1275,10 +1305,6 @@ static NSMutableDictionary *gKeychainServiceNameForAppName;
return YES;
}
-/** @fn getUID
- @brief Gets the identifier of the current user, if any.
- @return The identifier of the current user, or nil if there is no current user.
- */
- (nullable NSString *)getUID {
return _currentUser.uid;
}
diff --git a/Firebase/Auth/Source/FIRAuth_Internal.h b/Firebase/Auth/Source/FIRAuth_Internal.h
index 94363a1..5d1d13f 100644
--- a/Firebase/Auth/Source/FIRAuth_Internal.h
+++ b/Firebase/Auth/Source/FIRAuth_Internal.h
@@ -75,6 +75,12 @@ extern NSString *const FIRAuthStateDidChangeInternalNotificationTokenKey;
- (nullable instancetype)initWithAPIKey:(NSString *)APIKey
appName:(NSString *)appName NS_DESIGNATED_INITIALIZER;
+/** @fn getUID
+ @brief Gets the identifier of the current user, if any.
+ @return The identifier of the current user, or nil if there is no current user.
+ */
+- (nullable NSString *)getUID;
+
/** @fn notifyListenersOfAuthStateChange
@brief Posts the @c FIRAuthStateDidChangeNotification notification.
@remarks Called by @c FIRUser when token changes occur.
diff --git a/Firebase/Auth/Source/FIRUser.m b/Firebase/Auth/Source/FIRUser.m
index 112b870..cd168cf 100644
--- a/Firebase/Auth/Source/FIRUser.m
+++ b/Firebase/Auth/Source/FIRUser.m
@@ -734,7 +734,7 @@ static void callInMainThreadWithAuthDataResultAndError(
callInMainThreadWithAuthDataResultAndError(completion, authResult, error);
return;
}
- if (![authResult.user.uid isEqual:_auth.currentUser.uid]) {
+ if (![authResult.user.uid isEqual:[_auth getUID]]) {
callInMainThreadWithAuthDataResultAndError(completion, authResult,
[FIRAuthErrorUtils userMismatchError]);
return;