Home > OS >  Close Full Screen Cover after Sign in through Firebase in Swift UI app
Close Full Screen Cover after Sign in through Firebase in Swift UI app

Time:04-02

So I need help creating an async-await for a firebase sign-in button. I need the full-screen cover I have to disappear when the message that Firebase has authenticated the user has gone through. Basically, I have a Full-Screen Cover that is awaiting a response from the cloud saying that the user has been authenticated and the full-screen cover can now close. (Basically a login button on the sign up view) The problem is that when you click login once the full-screen cover doesn't close because the information from the cloud does not go through fast enough which means you have to click the login button twice so that the full-screen cover successfully closes.

Button:

Button(action: {
                        guard !email.isEmpty, !password.isEmpty else {
                            return
                        }
                       
                         fireViewModel.signIn(email: email, password: password)
                        if fireViewModel.signedIn { presentationMode.wrappedValue.dismiss() }
                    }, label: {
                        Text("Login")
                    })

Code for Firebase Sign in:

func signIn(email: String, password: String) {
    auth.signIn(withEmail: email, password: password) { result, error in
        guard result != nil, error == nil else {
            print(error ?? "")
            return
        }
        
        DispatchQueue.main.async {
            // Success
            self.signedIn = true
        }
    }
}

Can anyone help me do that? I am new to swift and have no experience with async-await... Thank you!

CodePudding user response:

you could try this, using a completion handler, such as:

func signIn(email: String, password: String, completion: @escaping (Bool) -> Void) {
    auth.signIn(withEmail: email, password: password) { result, error in
        guard result != nil, error == nil else {
            print(error ?? "")
            completion(false)  // <-- here
            return
        }
        DispatchQueue.main.async {
            // Success
            self.signedIn = true
            completion(true)  // <-- here
        }
    }
}

and

Button(action: {
    guard !email.isEmpty, !password.isEmpty else {
        return
    }
        fireViewModel.signIn(email: email, password: password) { isSignedIn in
            if isSignedIn {
                presentationMode.wrappedValue.dismiss()
            } else {
                // do something else
            }
        }
}, label: {
    Text("Login")
})
    

Adjust the logic to your needs.

  • Related