From 072c3ae93df4e6363ad1b6cfd239877578b5b00b Mon Sep 17 00:00:00 2001 From: rsgowman Date: Thu, 30 Nov 2017 13:36:48 -0500 Subject: Remove dependency on FirebaseAuth (#505) We now use the internal core functionality instead. --- Firestore/CHANGELOG.md | 1 + Firestore/Source/Auth/FSTCredentialsProvider.m | 60 ++++++++++++++------------ 2 files changed, 33 insertions(+), 28 deletions(-) (limited to 'Firestore') diff --git a/Firestore/CHANGELOG.md b/Firestore/CHANGELOG.md index 0aba04e..884af45 100644 --- a/Firestore/CHANGELOG.md +++ b/Firestore/CHANGELOG.md @@ -1,4 +1,5 @@ # Unreleased +- [changed] Firestore no longer has a direct dependency on FirebaseAuth. # v0.9.3 - [changed] Improved performance loading documents matching a query. diff --git a/Firestore/Source/Auth/FSTCredentialsProvider.m b/Firestore/Source/Auth/FSTCredentialsProvider.m index 7aa3671..57d79cb 100644 --- a/Firestore/Source/Auth/FSTCredentialsProvider.m +++ b/Firestore/Source/Auth/FSTCredentialsProvider.m @@ -16,8 +16,6 @@ #import "Firestore/Source/Auth/FSTCredentialsProvider.h" -#import -#import #import #import #import @@ -43,17 +41,12 @@ NS_ASSUME_NONNULL_BEGIN @end #pragma mark - FSTFirebaseCredentialsProvider -// TODO(mikelehen): Currently, we have a strong dependency on FIRAuth but we should ideally use -// only internal APIs on FIRApp instead. However, currently the FIRApp internal APIs don't expose -// the uid of the current user and don't expose an auth state change listener. So we use FIRAuth. @interface FSTFirebaseCredentialsProvider () @property(nonatomic, strong, readonly) FIRApp *app; -@property(nonatomic, strong, readonly) FIRAuth *auth; /** Handle used to stop receiving auth changes once userChangeListener is removed. */ -@property(nonatomic, strong, nullable, readwrite) - FIRAuthStateDidChangeListenerHandle authListenerHandle; +@property(nonatomic, strong, nullable, readwrite) id authListenerHandle; /** The current user as reported to us via our AuthStateDidChangeListener. */ @property(nonatomic, strong, nonnull, readwrite) FSTUser *currentUser; @@ -74,28 +67,40 @@ NS_ASSUME_NONNULL_BEGIN self = [super init]; if (self) { _app = app; - _auth = [FIRAuth authWithApp:app]; - _currentUser = [[FSTUser alloc] initWithUID:self.auth.currentUser.uid]; + _currentUser = [[FSTUser alloc] initWithUID:[self.app getUID]]; _userCounter = 0; // Register for user changes so that we can internally track the current user. FSTWeakify(self); - _authListenerHandle = [self.auth addAuthStateDidChangeListener:^(FIRAuth *auth, FIRUser *user) { - FSTStrongify(self); - if (self) { - @synchronized(self) { - FSTUser *newUser = [[FSTUser alloc] initWithUID:user.uid]; - if (![newUser isEqual:self.currentUser]) { - self.currentUser = newUser; - self.userCounter++; - FSTVoidUserBlock listenerBlock = self.userChangeListener; - if (listenerBlock) { - listenerBlock(self.currentUser); - } - } - } - } - }]; + _authListenerHandle = [[NSNotificationCenter defaultCenter] + addObserverForName:FIRAuthStateDidChangeInternalNotification + object:nil + queue:nil + usingBlock:^(NSNotification *notification) { + FSTStrongify(self); + if (self) { + @synchronized(self) { + NSDictionary *userInfo = notification.userInfo; + + // ensure we're only notifiying for the current app. + FIRApp *notifiedApp = userInfo[FIRAuthStateDidChangeInternalNotificationAppKey]; + if (![self.app isEqual:notifiedApp]) { + return; + } + + NSString *userID = userInfo[FIRAuthStateDidChangeInternalNotificationUIDKey]; + FSTUser *newUser = [[FSTUser alloc] initWithUID:userID]; + if (![newUser isEqual:self.currentUser]) { + self.currentUser = newUser; + self.userCounter++; + FSTVoidUserBlock listenerBlock = self.userChangeListener; + if (listenerBlock) { + listenerBlock(self.currentUser); + } + } + } + } + }]; } return self; } @@ -140,8 +145,7 @@ NS_ASSUME_NONNULL_BEGIN } else { FSTAssert(self.authListenerHandle, @"UserChangeListener removed twice!"); FSTAssert(_userChangeListener, @"UserChangeListener removed without being set!"); - [self.auth removeAuthStateDidChangeListener:self.authListenerHandle]; - + [[NSNotificationCenter defaultCenter] removeObserver:self.authListenerHandle]; self.authListenerHandle = nil; } _userChangeListener = block; -- cgit v1.2.3