Repository: firebase/firebase-ios-sdk (language: cpp)

Issue:
[FirebaseAuth] issue on persisting user session with multi tenant project on 11.0.0
### Description

Forwarding issue from flutter https://github.com/firebase/flutterfire/issues/13251

The issue is that when we launch the app using SDK version 11.0.0 it does not load user session from previous app launch.
We get this weird error:

```
[[FirebaseAuth]] 11.0.0 - [FirebaseAuth][I-AUT000001] Error loading saved user when starting up: Error Domain=FIRAuthErrorDomain Code=17072 "The provided user's tenant ID does not match the Auth instance's tenant ID." UserInfo={FIRAuthErrorUserInfoNameKey=ERROR_TENANT_ID_MISMATCH, NSLocalizedDescription=The provided user's tenant ID does not match the Auth instance's tenant ID.}
```

Our native code only contains `FirebaseApp.configure()`

### Reproducing the issue

1. Launch the app (with a multitenant project).
2. Login into the app.
3. Kill the app.
4. Launch the app again.

### Firebase SDK Version

11.0.0

### Xcode Version

-

### Installation Method

Swift Package Manager

### Firebase Product(s)

Authentication

### Targeted Platforms

iOS

### Relevant Log Output

```shell
[[FirebaseAuth]] 11.0.0 - [FirebaseAuth][I-AUT000001] Error loading saved user when starting up: Error Domain=FIRAuthErrorDomain Code=17072 "The provided user's tenant ID does not match the Auth instance's tenant ID." UserInfo={FIRAuthErrorUserInfoNameKey=ERROR_TENANT_ID_MISMATCH, NSLocalizedDescription=The provided user's tenant ID does not match the Auth instance's tenant ID.}
```


### If using Swift Package Manager, the project's Package.resolved

_No response_

### If using CocoaPods, the project's Podfile.lock

<!--- Look below for instructions on how to share your Podfile.lock. --->

<details>
<summary>Expand <code>Podfile.lock</code> snippet</summary>
<br>

```yml

-- TBA

```

</details>



Discussion / hints:
I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.

Fix patch:
diff --git a/FirebaseAuth/CHANGELOG.md b/FirebaseAuth/CHANGELOG.md
index 33675257635..ca2be73ed87 100644
--- a/FirebaseAuth/CHANGELOG.md
+++ b/FirebaseAuth/CHANGELOG.md
@@ -2,11 +2,11 @@
 - [Fixed] Fixed crashes that could occur in Swift continuation blocks running in the Xcode 16
   betas. (#13480)
 - [Fixed] Fixed Phone Auth via Sandbox APNS tokens that broke in 11.0.0. (#13479)
-- [Fixed] Fix crash when fetching sign in methods due to unexpected nil.
+- [Fixed] Fixed crash when fetching sign in methods due to unexpected nil.
   Previously, fetching sign in methods could return both a `nil` array of sign
   in methods and a `nil` error. In such cases, an empty array is instead
   returned with the `nil` error. (#13550)
-
+- [Fixed] Fixed user session persistence in multi tenant projects. Introduced in 11.0.0. (#13565)
 
 # 11.1.0
 - [fixed] Fixed `Swift.error` conformance for `AuthErrorCode`. (#13430)
diff --git a/FirebaseAuth/Sources/Swift/Auth/Auth.swift b/FirebaseAuth/Sources/Swift/Auth/Auth.swift
index 6c6932973de..8effd15c101 100644
--- a/FirebaseAuth/Sources/Swift/Auth/Auth.swift
+++ b/FirebaseAuth/Sources/Swift/Auth/Auth.swift
@@ -1667,9 +1667,11 @@ extension Auth: AuthInterop {
           try self.internalUseUserAccessGroup(storedUserAccessGroup)
         } else {
           let user = try self.getUser()
-          try self.updateCurrentUser(user, byForce: false, savingToDisk: false)
           if let user {
             self.tenantID = user.tenantID
+          }
+          try self.updateCurrentUser(user, byForce: false, savingToDisk: false)
+          if let user {
             self.lastNotifiedUserToken = user.rawAccessToken()
           }
         }
@@ -1941,8 +1943,7 @@ extension Auth: AuthInterop {
     }
     if let user {
       if user.tenantID != nil || tenantID != nil, tenantID != user.tenantID {
-        let error = AuthErrorUtils.tenantIDMismatchError()
-        throw error
+        throw AuthErrorUtils.tenantIDMismatchError()
       }
     }
     var throwError: Error?
diff --git a/FirebaseAuth/Sources/Swift/Backend/AuthBackend.swift b/FirebaseAuth/Sources/Swift/Backend/AuthBackend.swift
index 5a5e6d0664a..b0e9a93fca5 100644
--- a/FirebaseAuth/Sources/Swift/Backend/AuthBackend.swift
+++ b/FirebaseAuth/Sources/Swift/Backend/AuthBackend.swift
@@ -484,8 +484,7 @@ private class AuthBackendRPCImplementation: NSObject, AuthBackendImplementation
         code: AuthErrorCode.maximumSecondFactorCountExceeded,
         message: serverDetailErrorMessage
       )
-    case "TENANT_ID_MISMATCH": return AuthErrorUtils
-      .tenantIDMismatchError()
+    case "TENANT_ID_MISMATCH": return AuthErrorUtils.tenantIDMismatchError()
     case "TOKEN_EXPIRED": return AuthErrorUtils
       .userTokenExpiredError(message: serverDetailErrorMessage)
     case "UNSUPPORTED_FIRST_FACTOR": return AuthErrorUtils


Test patch: