Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Wallet] Fix firebase initialization error on iOS after reinstalling the app #1423

Merged
merged 3 commits into from
Oct 23, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just want to confirm that it was intentional to throw here instead of returning undefined? This callback is what will run on the db to modify the data. I don't think the error will propagate to the app, but I could be wrong. I'm uncertain what happens if we throw here.

Copy link
Contributor Author

@jeanregisser jeanregisser Oct 22, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question!
It triggers a red screen in dev, and it's not captured by the saga promise flow.
It's logged by Sentry and crashes the app. Which is what I intended as I see it like a logic error which shouldn't happen.

}
})
Logger.info(TAG, 'Firebase Auth initialized successfully')
Expand Down