Repository navigation
Conversation
…ctors is not stale after re-signing in `getAppMultiFactor` cached a `MultiFactor` per app and uid. The cache was only cleared in `signOut()` while `currentUser` was still non-null (firebase#12488). When the session ends without an explicit sign-out while signed in (e.g. the backend revokes the session after a second factor is unenrolled and the native SDK signs the user out on its own), the entry stayed. After the same user signed in again, `getEnrolledFactors()` returned the factors of the previous `FirebaseUser` instance until the process was killed. Return `currentUser.multiFactor` every time instead. Sessions, resolvers and assertions are already kept in their own maps keyed by id, so nothing relies on the cached instance. Fixes firebase#19021
There was a problem hiding this comment.
Code Review
This pull request removes the caching of MultiFactor instances per user ID (multiFactorUserMap) in the Android Firebase Auth plugin. Instead, currentUser.multiFactor is retrieved directly to prevent stale cached multi-factor data when a session ends without an explicit sign-out. Additionally, a new contributor has been added to the AUTHORS file. I have no feedback to provide as there are no review comments.
SelaseKay
left a comment
There was a problem hiding this comment.
Hi @tatsuhama, could you add tests for this changes?
…ns in again without signOut The native SDK signs out by itself when the session is no longer valid, so FirebaseAuth.signOut() is never called. The test makes that happen with a disabled user, changes the factors on the server, signs the same user in again and expects the current factors instead of the ones from the previous session.
|
Thanks for the review! I added an e2e test in ccbde5e.
To set this up I added Locally on an Android emulator, all 8 tests in the multi-factor group pass. With only the Kotlin change reverted, the new test fails with |
SelaseKay
left a comment
There was a problem hiding this comment.
Hi @tatsuhama, thanks for the PR. LGTM!
| return appMultiFactorUser.getOrPut(currentUser.uid) { currentUser.multiFactor } | ||
| // Do not cache the MultiFactor per uid. It belongs to a specific FirebaseUser instance, so a | ||
| // cached one becomes stale when the same user signs in again after the session ended without | ||
| // an explicit signOut() (e.g. the session was revoked after unenrolling a factor). |
There was a problem hiding this comment.
@tatsuhama, Nit: I think we can remove the comment.
Description
On Android,
FlutterFirebaseMultiFactor.getAppMultiFactorcached aMultiFactorper app and uid inmultiFactorUserMap. #12488 clears the entry insignOut(), but only whilecurrentUseris still non-null.When the session ends without an explicit sign-out while signed in, the entry is never removed. For example, unenrolling a second factor makes the backend revoke the session, and the native SDK signs the user out on its own. After the same user signs in again in the same process,
getEnrolledFactors()keeps returning the factors of the previousFirebaseUserinstance (including the unenrolled one) until the app is killed.user.reload()doesn't help.This PR stops caching the
MultiFactorand returnscurrentUser.multiFactorevery time. The now-unused cache and its cleanup insignOut()are removed. Sessions, resolvers and assertions are already kept in their own maps keyed by id, so nothing relied on the cached instance. The change is Android-only; iOS doesn't have this cache.Manual verification
The same three-line change applied to firebase_auth 6.7.1 (the code is identical there), on an Android emulator (Pixel 10 AVD):
Notifying auth state listeners about a sign-out event).getEnrolledFactors()no longer returns the TOTP factor. Before the change it still returned it until the app was restarted.I wrote this change with the help of Claude Code (an AI coding assistant) and reviewed it myself.
Related Issues
getEnrolledFactorsreturns stale factors after re-signing in, when the previous session ended by token revocation #19021getEnrolledFactorsto return an empty list if signing out in the same app session #12488Checklist
firebase_auth_multi_factor_e2e_test.dart. It makes the native SDK sign out by itself (a disabled user failsreload()), removes the factor on the Auth emulator, signs the same user in again and expects no enrolled factors.Expected: <0>, Actual: <1>). I didn't run the other suites locally.///). (No public API change; added a code comment explaining why the instance isn't cached.)melos run analyze) does not report any problems on my PR. (Randart analyzeon the changed test files: no issues.)Breaking Change