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

Make sure currentUser is available upon startup #51

Merged
merged 3 commits into from
Feb 5, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
10 changes: 5 additions & 5 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ let package = Package(
.library(name: "SpeziFirebaseAccountStorage", targets: ["SpeziFirebaseAccountStorage"])
],
dependencies: [
.package(url: "https://github.com/StanfordSpezi/SpeziFoundation", from: "2.0.0"),
.package(url: "https://github.com/StanfordSpezi/Spezi", from: "1.7.1"),
.package(url: "https://github.com/StanfordSpezi/SpeziViews", from: "1.6.0"),
.package(url: "https://github.com/StanfordSpezi/SpeziAccount", from: "2.1.1"),
.package(url: "https://github.com/firebase/firebase-ios-sdk", from: "11.0.0"),
.package(url: "https://github.com/StanfordSpezi/SpeziFoundation", from: "2.1.0"),
.package(url: "https://github.com/StanfordSpezi/Spezi", from: "1.8.0"),
.package(url: "https://github.com/StanfordSpezi/SpeziViews", from: "1.9.0"),
.package(url: "https://github.com/StanfordSpezi/SpeziAccount", from: "2.1.2"),
.package(url: "https://github.com/firebase/firebase-ios-sdk", from: "11.8.0"),
.package(url: "https://github.com/apple/swift-atomics.git", from: "1.2.0")
] + swiftLintPackage(),
targets: [
Expand Down
9 changes: 4 additions & 5 deletions Sources/SpeziFirebaseAccount/FirebaseAccountService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -247,11 +247,10 @@
}
}

// The `Auth.auth().currentUser` is not available immediately. The init of `Auth` delays
// the retrieval of the keychain object.
// See https://github.com/firebase/firebase-ios-sdk/blob/main/FirebaseAuth/Sources/Swift/Auth/Auth.swift#L1646.
// To increase our chance that the initial check did run, we move this check to the end.
// Every call to Auth.auth() acquires a lock, so this might increase our chance that the initialization did complete successfully.
// Firebase v11.6.0 restore the v10 behavior where the currentUser is available immediately after startup.
// `currentUser` will sync to the Auth worker queue, see https://github.com/firebase/firebase-ios-sdk/pull/14141.
// The Auth.init kicked off loading the current user already above, so we might not need to wait that long here.
// But, if there is a user, it will definitely get loaded here.
checkForInitialUserAccount()

Task.detached { [logger, secureStorage, localStorage] in
Expand Down Expand Up @@ -529,7 +528,7 @@
try await mapFirebaseAccountError {
if modifications.modifiedDetails.contains(AccountKeys.userId) {
logger.debug("updateEmail(to:) for user.")
try await currentUser.updateEmail(to: modifications.modifiedDetails.userId)

Check warning on line 531 in Sources/SpeziFirebaseAccount/FirebaseAccountService.swift

View workflow job for this annotation

GitHub Actions / Build and Test Swift Package / Test using xcodebuild or run fastlane

'updateEmail(to:)' is deprecated: `updateEmail` is deprecated and will be removed in a future release. Use sendEmailVerification(beforeUpdatingEmail:) instead.

Check warning on line 531 in Sources/SpeziFirebaseAccount/FirebaseAccountService.swift

View workflow job for this annotation

GitHub Actions / Build and Test UI Tests / Test using xcodebuild or run fastlane

'updateEmail(to:)' is deprecated: `updateEmail` is deprecated and will be removed in a future release. Use sendEmailVerification(beforeUpdatingEmail:) instead.
}

if let password = modifications.modifiedDetails.password {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import SwiftUI
struct FirebaseAccountTestsView: View {
@Environment(Account.self)
var account
@Environment(AccountTestModel.self)
private var testModel

@State var viewState: ViewState = .idle

Expand All @@ -26,6 +28,9 @@ struct FirebaseAccountTestsView: View {

var body: some View {
List {
Section {
ListRow("User Present on Startup", value: testModel.accountUponConfigure ? "Yes" : "No")
}
if let details = account.details {
Section {
accountHeader(for: details)
Expand Down Expand Up @@ -96,3 +101,16 @@ struct FirebaseAccountTestsView: View {
}
}
}


#if DEBUG
#Preview {
NavigationStack {
FirebaseAccountTestsView()
}
.environment(AccountTestModel())
.previewWith {
AccountConfiguration(service: InMemoryAccountService(), configuration: .default)
}
}
#endif
24 changes: 24 additions & 0 deletions Tests/UITests/TestApp/Shared/TestAppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,29 @@ import SpeziFirestore
import SwiftUI


@Observable
final class AccountTestModel {
/// Flag to determine if an account was present upon the initial startup.
var accountUponConfigure = false

init() {}
}


class TestAppDelegate: SpeziAppDelegate {
private class InitialUserCheck: Module {
@Dependency(Account.self)
private var account
@Dependency(FirebaseAccountService.self)
private var service

@Model var model = AccountTestModel()

func configure() {
model.accountUponConfigure = account.signedIn
}
}

private class Logout: Module {
@Application(\.logger)
private var logger
Expand Down Expand Up @@ -71,6 +93,8 @@ class TestAppDelegate: SpeziAppDelegate {

Logout()

InitialUserCheck()

Firestore(settings: .emulator)
FirebaseStorageConfiguration(emulatorSettings: (host: "localhost", port: 9199))
}
Expand Down
33 changes: 33 additions & 0 deletions Tests/UITests/TestAppUITests/FirebaseAccountTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,39 @@ final class FirebaseAccountTests: XCTestCase { // swiftlint:disable:this type_bo
XCTAssert(app.staticTexts["Leland Stanford"].waitForExistence(timeout: 2.0))
XCTAssert(app.staticTexts["Biography, Bio"].exists)
}

@MainActor
func testAccountReadyUponStartup() async throws {
try await FirebaseClient.createAccount(email: "test@username.edu", password: "TestPassword", displayName: "Username Test")

let accounts = try await FirebaseClient.getAllAccounts()
XCTAssertEqual(accounts, [FirestoreAccount(email: "test@username.edu", displayName: "Username Test")])

let app = XCUIApplication()
app.launchArguments = ["--firebaseAccount"]
app.launch()

XCTAssertTrue(app.wait(for: .runningForeground, timeout: 2.0))

XCTAssert(app.buttons["FirebaseAccount"].waitForExistence(timeout: 2.0))
app.buttons["FirebaseAccount"].tap()

try app.login(username: "test@username.edu", password: "TestPassword")
XCTAssert(app.staticTexts["test@username.edu"].waitForExistence(timeout: 2.0))

app.terminate()
XCTAssertTrue(app.wait(for: .notRunning, timeout: 2.0))

app.launch()

XCTAssertTrue(app.wait(for: .runningForeground, timeout: 2.0))

XCTAssert(app.buttons["FirebaseAccount"].waitForExistence(timeout: 2.0))
app.buttons["FirebaseAccount"].tap()

XCTAssertFalse(app.staticTexts["User Present on Startup, No"].exists)
XCTAssert(app.staticTexts["User Present on Startup, Yes"].waitForExistence(timeout: 2.0))
}
}


Expand Down