invertase / react-native-firebase

🔥 A well-tested feature-rich modular Firebase implementation for React Native. Supports both iOS & Android platforms for all Firebase services.
https://rnfirebase.io
Other
11.64k stars 2.21k forks source link

[🐛] After logging out or reinstalling the app, the authorized user remains.(In iPhone 7) #4667

Closed Maildes closed 3 years ago

Maildes commented 3 years ago

Issue

I am implementing a Firebase auth module to enable phone verification in my application. I configure everything according to the documents and everything works (I get SMS). After updating to version 10.1.0 everything works fine on android and ios but when i try to check the code on iPhone 7 it finds the previous authorized user and does not let login the new user. I don't get any error other than invalid code.

SMS verification function:

  const clickCheckCode = async () => {
    setLoading(true);
    try {
      /** Validate code */
      setConfBtnIsDis(true);

      /** if (!isUserAuth) */
      let currentUserAuth = auth().currentUser;

      console.log(currentUserAuth);

      if (currentUserAuth === null) await confirm.confirm(value)
      else return;

      /** Get user by request */
      getUserTokenByPhone(userPhone, userCountry);

      setLoading(false);
    } catch (error) {
      Alert.alert('Error', 'Invalid code!', [{text: 'OK'}], {cancelable: true});
      setLoading(false);
    }
};

console.log(currentUserAuth) output:

Click To Expand

``` { _auth: { _app: { _name: '[DEFAULT]', _deleted: false, _deleteApp: [Function: bound deleteApp], _options: { projectId: 'revcicom', databaseURL: 'https://revcicom.firebaseio.com', androidClientID: '1043740910299-6nvj8lg75u6tqp731enrrlipm0on1j25.apps.googleusercontent.com', messagingSenderId: '1043740910299', appId: '1:1043740910299:ios:4529cff16eaf42b418a2fe', clientId: '1043740910299-q2hflvo7p37nmnga2mi6itm5kidnro2r.apps.googleusercontent.com', storageBucket: 'revcicom.appspot.com', apiKey: 'AIzaSyBoaWU_TzqLS75aKr1jZGBJsP4ITaJZQoI' }, _automaticDataCollectionEnabled: true, _initialized: true, _nativeInitialized: true }, _nativeModule: { addAuthStateListener: [Function], removeAuthStateListener: [Function], addIdTokenListener: [Function], removeIdTokenListener: [Function], setAppVerificationDisabledForTesting: [Function], useUserAccessGroup: [Function], signOut: [Function], signInAnonymously: [Function], signInWithEmailAndPassword: [Function], signInWithEmailLink: [Function], createUserWithEmailAndPassword: [Function], delete: [Function], reload: [Function], sendEmailVerification: [Function], verifyBeforeUpdateEmail: [Function], updateEmail: [Function], updatePassword: [Function], updatePhoneNumber: [Function], updateProfile: [Function], getIdToken: [Function], getIdTokenResult: [Function], signInWithCredential: [Function], confirmPasswordReset: [Function], applyActionCode: [Function], checkActionCode: [Function], sendPasswordResetEmail: [Function], sendSignInLinkToEmail: [Function], signInWithCustomToken: [Function], signInWithPhoneNumber: [Function], verifyPhoneNumber: [Function], confirmationResultConfirm: [Function], linkWithCredential: [Function], unlink: [Function], reauthenticateWithCredential: [Function], fetchSignInMethodsForEmail: [Function], setLanguageCode: [Function], useDeviceLanguage: [Function], verifyPasswordResetCode: [Function], useEmulator: [Function], APP_USER: { '[DEFAULT]': { phoneNumber: '+380957757075', isAnonymous: false, email: null, refreshToken: 'AG8BCndhTNBjr7oxmQv4gbJgRgC7qmBks3oPZOCUvV5ve7Bwz6ssYS0laV_EG0T0hzrTpYryaqc0fb1J5ubodIF-GBOjIdMMLf8H-0viiXms8BQFLfYhSl2C4GspE6WKk5YlMMEXfnTdmohdAbb98U6ospUq5j2r_9DF6cGz6HIFL66evkot0IHYCph-41_WTniIiiYkkfZ4', metadata: { creationTime: 1602585051899, lastSignInTime: 1605170079846 }, uid: 'vwjRU3XtpLbhtUiwAYnw6B5HpBI2', photoURL: null, providerId: 'firebase', emailVerified: false, providerData: [ { providerId: 'phone', phoneNumber: '+380957757075' } ], displayName: null } }, APP_LANGUAGE: {}, getConstants: [Function] }, _customUrlOrRegion: undefined, _config: { statics: { AppleAuthProvider: [Function: AppleAuthProvider], EmailAuthProvider: [Function: EmailAuthProvider], PhoneAuthProvider: [Function: PhoneAuthProvider], GoogleAuthProvider: [Function: GoogleAuthProvider], GithubAuthProvider: [Function: GithubAuthProvider], TwitterAuthProvider: [Function: TwitterAuthProvider], FacebookAuthProvider: [Function: FacebookAuthProvider], OAuthProvider: [Function: OAuthProvider], PhoneAuthState: { CODE_SENT: 'sent', AUTO_VERIFY_TIMEOUT: 'timeout', AUTO_VERIFIED: 'verified', ERROR: 'error' } }, version: '10.1.1', namespace: 'auth', nativeModuleName: 'RNFBAuthModule', nativeEvents: [ 'auth_state_changed', 'auth_id_token_changed', 'phone_auth_state_changed' ], hasMultiAppSupport: true, hasCustomUrlOrRegionSupport: false, ModuleClass: [Function: FirebaseAuthModule] }, _user: [Circular], _settings: null, _authResult: true, _languageCode: undefined }, _user: { phoneNumber: '+380957757075', isAnonymous: false, email: null, refreshToken: 'AG8BCndhTNBjr7oxmQv4gbJgRgC7qmBks3oPZOCUvV5ve7Bwz6ssYS0laV_EG0T0hzrTpYryaqc0fb1J5ubodIF-GBOjIdMMLf8H-0viiXms8BQFLfYhSl2C4GspE6WKk5YlMMEXfnTdmohdAbb98U6ospUq5j2r_9DF6cGz6HIFL66evkot0IHYCph-41_WTniIiiYkkfZ4', metadata: { creationTime: 1602585051899, lastSignInTime: 1605170079846 }, uid: 'vwjRU3XtpLbhtUiwAYnw6B5HpBI2', photoURL: null, providerId: 'firebase', emailVerified: false, providerData: [ { providerId: 'phone', phoneNumber: '+380957757075' } ], displayName: null } } ``` --- **`react-native info` output:** ``` System: OS: macOS 10.15.5 CPU: (4) x64 Intel(R) Core(TM) i5-5350U CPU @ 1.80GHz Memory: 271.21 MB / 8.00 GB Shell: 3.2.57 - /bin/bash Binaries: Node: 12.18.1 - /usr/local/bin/node Yarn: Not Found npm: 6.14.5 - /usr/local/bin/npm Watchman: 4.9.0 - /usr/local/bin/watchman Managers: CocoaPods: 1.9.3 - /usr/local/bin/pod SDKs: iOS SDK: Platforms: iOS 13.7, DriverKit 19.0, macOS 10.15, tvOS 13.4, watchOS 6.2 Android SDK: Not Found IDEs: Android Studio: Not Found Xcode: 11.7/11E801a - /usr/bin/xcodebuild Languages: Java: Not Found Python: 2.7.16 - /usr/bin/python npmPackages: @react-native-community/cli: ^4.13.0 => 4.13.0 react: ^17.0.1 => 17.0.1 react-native: ^0.63.4 => 0.63.4 react-native-macos: Not Found npmGlobalPackages: *react-native*: Not Found SMMs-MacBook-Air:mobile marketolog$ ```

mikehardy commented 3 years ago

Interesting. You skipped most of the template which is maintainer-unfriendly. I would need to see a minimal reproduction App.js of this that could be dropped into a framework like that produced by https://github.com/mikehardy/rnfbdemo/blob/master/make-demo.sh I would be curious to see if you had logged out properly. I have a hunch firebase-ios-sdk stores some user information in the keychain You may try to reproduce this in a quickstart for the underlying SDK as I doubt our behavior differs from it at all, and this may in fact be intended behavior https://github.com/firebase/quickstart-ios/tree/master/authentication

Maildes commented 3 years ago

@mikehardy I'm sorry. I'm a beginner and this is the first time I have come here with a problem, I did not fully understand what information I need to provide specifically for my case. On real devices, everything works fine and on the emulator. This case of user preservation was detected only on the iPhone 7

mikehardy commented 3 years ago

The general idea in open source is that if you log an issue, follow the template - the maintainers know what information is required to troubleshoot

It's my understanding the iPhone 7 is a real device, so your statement:

On real devices, everything works fine and on the emulator. This case of user preservation was detected only on the iPhone 7

...contains an internal contradiction and I can't reason about it

The rest of my comment above stands, I'd reiterate that guidance

efstathiosntonas commented 3 years ago

Try putting this on your AppDelegate.m:

  NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    if (![defaults boolForKey:@"notFirstRun"]) {
        [defaults setBool:YES forKey:@"notFirstRun"];
        [defaults synchronize];
        [[FIRAuth auth] signOut:NULL];
   }

put it after

  if ([FIRApp defaultApp] == nil) {
    [FIRApp configure];
  }
mikehardy commented 3 years ago

cross referencing from FlutterFire (has same behavior) https://github.com/FirebaseExtended/flutterfire/issues/4661

Maildes commented 3 years ago

@mikehardy, @efstathiosntonas, It worked, thanks a lot for your help!

oarsheo commented 3 years ago

Try putting this on your AppDelegate.m:

  NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    if (![defaults boolForKey:@"notFirstRun"]) {
        [defaults setBool:YES forKey:@"notFirstRun"];
        [defaults synchronize];
        [[FIRAuth auth] signOut:NULL];
   }

put it after

  if ([FIRApp defaultApp] == nil) {
    [FIRApp configure];
  }

How to log out all Firebase apps ?

mikehardy commented 3 years ago

get the FIRAuth for a specific app is https://firebase.google.com/docs/reference/ios/firebaseauth/api/reference/Classes/FIRAuth#+authwithapp:

get all current apps is https://firebase.google.com/docs/reference/ios/firebasecore/api/reference/Classes/FIRApp#allapps

so get all apps, iterate logging out of them ?