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

Users unexpectedly logged out when opening the app after some time #9869

Closed
sergiocampama opened this issue May 31, 2022 · 11 comments · Fixed by #9898
Closed

Users unexpectedly logged out when opening the app after some time #9869

sergiocampama opened this issue May 31, 2022 · 11 comments · Fixed by #9898
Assignees
Milestone

Comments

@sergiocampama
Copy link
Contributor

[REQUIRED] Step 1: Describe your environment

  • Xcode version: Xcode 13.4
  • Firebase SDK version: 9.0.0
  • Installation method: CocoaPods
  • Firebase Component: Auth
  • Target platform(s): iOS

[REQUIRED] Step 2: Describe the problem

We've been getting user reports that after updating to an app version that included 9.0.0, they've been getting logged out after closing the app and waiting some time, resulting in a degraded experience.

Steps to reproduce:

Sadly this is one of those issues that can't be reliably reproduced. We believe it's related to the Keychain and prewarming mitigation fix put in place in #9622, specifically to the part that checks whether [UIApplication isProtectedDataAvailable] returns true. There have been reports of isProtectedDataAvailable not returning true after application(_:didFinishLaunchingWithOptions:), such as https://sourcediving.com/solving-mysterious-logout-issues-on-ios-15-8b818c089466. (this also contains some reproduction instructions, like force closing the app, putting the phone to sleep, waiting 30 minutes, unlock the phone and open the app)

Reading the code change in #9622, if isProtectedDataAvailable is false, then it will wait for the UIApplicationProtectedDataDidBecomeAvailable notification, which might never arrive if isProtectedDataAvailable never changed to true. This would result in the Auth component not being up to date on user launch, which would result in our app deciding it is logged out.

Workaround

To work around this issue, we should be able to manually send the UIApplicationProtectedDataDidBecomeAvailable in application(_:didFinishLaunchingWithOptions:) at some point after initializing the Auth component, but it might be tricky to figure out the right place if Auth is abstracted into other app-specific components.

Should there be an option to deactivate this automatic mitigation/checks in light of isProtectedDataAvailable not working as expected in all cases? I'll leave that up to the Firebase team, but as it stands, 9.0.0 introduces a new authentication issue for us that wasn't there before (at least in our case), and for now, we'll just stay in 8.15.0 which does not contain this change.

@rosalyntan
Copy link
Member

Hi @sergiocampama, thanks for reporting this!

This definitely seems weird, as #9622 was intended to fix the issue described in the article you linked, where keychain access fails due to protected data not yet being available in didFinishLaunchingWithOptions. It seems like for your case, isProtectedDataAvailable is returning false even when protected data is in fact available.

To help with debugging, what are the iOS versions of the users you have that are experiencing this?

@sergiocampama
Copy link
Contributor Author

I was finally able to reproduce this in iOS 15.5, following those same steps:

  1. Open app
  2. Log in
  3. Force close the app
  4. Put phone into sleep
  5. Wait 30 min
  6. Unlock phone
  7. Open app

This does seem related to prewarming, which can also be random and dependent on the usage patterns of the app, so not sure if it'll reproduce as easily in other phones.

@rosalyntan
Copy link
Member

Hi @sergiocampama, two follow-up questions to help with debugging:

  1. Do you obtain the current user via the AuthStateDidChangeListener or the User#getIDToken API?
  2. Does your app use the UIScene lifecycle?

@sergiocampama
Copy link
Contributor Author

Hi @rosalyntan

  1. We obtain the current user first through Auth.auth().currentUser and then register the AuthStateDidChangeListener for future changes.
  2. No, we are still using AppDelegate lifecycles, specifically, we're calling the above API in func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?)

@rosalyntan
Copy link
Member

Thanks for the response @sergiocampama! Just want to confirm one more thing -- is your FirebaseApp.configure() call also in didFinishLaunchingWithOptions?

For context, I believe the prewarming issues in #8695 were caused by the undocumented behavior of didFinishLaunchingWithOptions being called as part of the prewarming sequence (before the device is unlocked) for apps that support scenes. However, if your app does not support scenes, then didFinishLaunchingWithOptions, should not be called during prewarming sequence, so it looks like something else might be going on.

@sergiocampama
Copy link
Contributor Author

Yes, FirebaseApp.configure() is called before currentUser (not directly in AppDelegate, but in the same stack as didFinishLaunchingWithOptions, i.e. not inside a callback invoked later in a different thread/stacktrace)

@matthewmoss
Copy link

matthewmoss commented Jun 9, 2022

Seeing the same issue here – we saw this issue very infrequently before Firebase 9.0.0. Since updating to 9.0.0 a few days ago, we've had multiple testers reporting the sign out issue. Seems like something is broke here. I am running 15.4.1 and just hit the issue myself.

@sergiocampama
Copy link
Contributor Author

Thanks @rosalyntan! Any idea on which version this might land in?

@rosalyntan
Copy link
Member

No problem -- it should be included in v9.2.0.

@morganchen12 morganchen12 added this to the 9.2.0 - M117 milestone Jun 15, 2022
@Claes34
Copy link

Claes34 commented Jun 22, 2022

Hello @sergiocampama, we faced a lot of logout issues with v9.0. Our app wakes up with significant locations even if the phone is locked, the keychain being probably inaccessible at this moment, making Auth.auth().currentUser nil when opening the app. We downgraded to 8.15 to hotfix the problem that had a huge impact on our customers. We are planning on updating to 9.2.0 soon though. Did this version solve the logout issue for you ? Thanks in advance.

@sergiocampama
Copy link
Contributor Author

we just updated to 9.2.0 and I can't reproduce the log out issue any more. we haven't released this new version, but given that it was easy to reproduce before, I'm more confident that it now works as expected

@firebase firebase locked and limited conversation to collaborators Jul 15, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
6 participants