Skip to content

Commit

Permalink
[Wallet] Fix firebase initialization error on iOS after reinstalling …
Browse files Browse the repository at this point in the history
…the app (#1423)
  • Loading branch information
jeanregisser committed Oct 23, 2019
1 parent 98b068c commit 82a97ef
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
31 changes: 31 additions & 0 deletions packages/mobile/ios/celo/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,18 @@
#import "RNFirebaseNotifications.h"
#import "RNFirebaseMessaging.h"

// Use same key as react-native-secure-key-store
// so we don't reset already working installs
static NSString * const kHasRunBeforeKey = @"RnSksIsAppInstalled";

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Reset keychain on first run to clear existing Firebase credentials
// Note: react-native-secure-key-store also does that but is run too late
// and hence can't clear Firebase credentials
[self resetKeychainIfNecessary];
[FIRApp configure];
[RNFirebaseNotifications configure];
RCTBridge *bridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:launchOptions];
Expand Down Expand Up @@ -68,4 +76,27 @@ - (void)application:(UIApplication *)application didRegisterUserNotificationSett
[[RNFirebaseMessaging instance] didRegisterUserNotificationSettings:notificationSettings];
}

// Reset keychain on first app run, this is so we don't run with leftover items
// after reinstalling the app
- (void)resetKeychainIfNecessary
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults boolForKey:kHasRunBeforeKey]) {
return;
}

NSArray *secItemClasses = @[(__bridge id)kSecClassGenericPassword,
(__bridge id)kSecAttrGeneric,
(__bridge id)kSecAttrAccount,
(__bridge id)kSecClassKey,
(__bridge id)kSecAttrService];
for (id secItemClass in secItemClasses) {
NSDictionary *spec = @{(__bridge id)kSecClass:secItemClass};
SecItemDelete((__bridge CFDictionaryRef)spec);
}

[defaults setBool:YES forKey:kHasRunBeforeKey];
[defaults synchronize];
}

@end
4 changes: 4 additions & 0 deletions packages/mobile/src/firebase/firebase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ export const initializeAuth = async (app: Firebase, address: string) => {
await userRef.child(user.user.uid).transaction((userData) => {
if (userData == null) {
return { address }
} else if (userData.address !== undefined && userData.address !== address) {
// This shouldn't happen! If this is thrown it means the firebase user is reused
// with different addresses (which we don't want) or the db was incorrectly changed remotely!
throw new Error("User address in the db doesn't match persisted address")
}
})
Logger.info(TAG, 'Firebase Auth initialized successfully')
Expand Down

0 comments on commit 82a97ef

Please sign in to comment.