ios – Firebase Auth signs out everytime app relaunches – SwiftUI & Firebase


My app keeps signing out every time I relaunch.

It used to run fine but since updating to Firebase 10.20.0 from >8.0.0 for VisionOS I am running into this issue.

appnameApp

@StateObject private var store = Store()

init() {
    // Initialise Firebase
    FirebaseApp.configure()

}

var body: some Scene {
    WindowGroup {
        ContentView()
            .environmentObject(store)
        }
    }
 }

ContentView

@EnvironmentObject var store: Store

switch store.userAuthState {
case .signedIn:
    AppTabView()
        .environmentObject(store)
    
case .signedOut:
    LoginView()
    
case .undefined:
    LoadingView()
    
}

enum UserAuthenticationState {
    case undefined, signedOut, signedIn
}

Store Object

class Store : ObservableObject {

private var db = Firestore.firestore()

// Session Properties
@Published var userAuthState: UserAuthenticationState = .signedOut

@Published var session: User?
@Published var isEmailVerified = false
@Published var userEmail: String?

init () {
    handle = Auth.auth().addStateDidChangeListener { [self] (auth, user) in
        if let user = user {
            print("StateDidChangeListener")
            self.userEmail = user.email
            self.session = user
            
            if let u = auth.currentUser {
                self.isEmailVerified = u.isEmailVerified
            }
            
            if self.profile == nil {
                self.fetchProfile(uid: user.uid)
            }
            
        } else {
            self.session = nil
            self.userAuthState = .signedOut
        }
    }
}

deinit {
    if let handle = handle {
        Auth.auth().removeStateDidChangeListener(handle)
    }
}
}

Target: VisionOS

SPM Version: 10.20.0

Latest articles

spot_imgspot_img

Related articles

Leave a reply

Please enter your comment!
Please enter your name here

spot_imgspot_img