'SwiftUI, Firebase : How I can handle the completion come from Login result?

First, this is the AuthService I made

import Foundation
import FirebaseAuth

class AuthService {
    
    static let instance = AuthService()
    
    var isSignIn : Bool = false
    
    func signUpWithEmail(email : String, password : String, completion : @escaping (_ result : Bool) -> ()) {
        Auth.auth().createUser(withEmail: email, password: password) { [weak self] result, error in
            if let error = error {
                print("error to make account")
                completion(false)
            }
            completion(true)
        }
    }
}

Next one is the View related with the Auth,

import SwiftUI

struct SignUpView: View {
    
    @Environment(\.presentationMode) var presentationMode
    
    @State private var email : String = ""
    @State private var password : String = ""
    @State private var firstName : String = ""
    @State private var lastName : String = ""
    @State private var userName : String = ""
    @Binding var showMainView : Bool
    
    
    var body: some View {
        NavigationView {
            ScrollView {
                VStack(alignment : .leading) {
                    textFieldStyle(title: "E-mail", bindingText: $email)
                    
                    textFieldStyle(title: "Password", bindingText: $password)
                    
                    textFieldStyle(title: "Last name", bindingText: $lastName)
                    
                    textFieldStyle(title: "First name", bindingText: $firstName)
                    
                    textFieldStyle(title: "User name", bindingText: $userName)
                    
                    Button(action: {
                        AuthService.instance.signUpWithEmail(email: email, password: password) { result in
                            if result {
                                presentationMode.wrappedValue.dismiss()
                                showMainView.toggle()
                            } else {
                                print("cannot close the view")
                            }
                        }
                    }, label: {
                        Text("Sign Up")
                            .font(.title3.bold())
                            .foregroundColor(.white)
                            .frame(maxWidth : .infinity)
                            .frame(height : 60)
                            .background(.blue)
                            .cornerRadius(12)
                            .shadow(color: .gray.opacity(0.2), radius: 12, x: 1, y: 1)
                    })
                }//vst
                .padding()
                
                
            .navigationTitle("Sign Up ")
            .navigationBarTitleDisplayMode(.large)
            }
        }
    }
}

And this is the HomeView,

import SwiftUI
import AuthenticationServices

struct HomeView: View {
    
    @State private var showSignUpSheet : Bool = false
    @State private var showMainView : Bool = false
    
    var body: some View {
        NavigationView {
            VStack(spacing : 40) {
                HStack {
                    VStack(alignment : .leading) {
                        Text("Welcome to")
                            .font(.title)
                            .fontWeight(.bold)
                        
                        Text("CranMarket")
                            .font(.largeTitle)
                            .fontWeight(.heavy)
                            .foregroundColor(.blue)
                    }
                    Spacer()
                }
                .padding()
                
                Text("This is the application for the students and families who want to sell and buy some second use things! Start it right now!")
                    .padding()
                
                VStack(spacing : 0) {
                    appleSignInButton
                    googleSignInButton
                    emailSignInButton
                    
                    HStack {
                        Spacer()
                        Button(action: {
                            showSignUpSheet.toggle()
                        }, label: {
                            Text("Don't you have account yet?")
                                .font(.headline)
                        })
                        .fullScreenCover(isPresented: $showMainView) {
                            MainView()
                        }
                        .sheet(isPresented: $showSignUpSheet) {
                            SignUpView(showMainView: $showMainView)
                        }
                    }
                    .padding(.horizontal)
                }
            }//vst
        }//nav
    }
}

And this is the picture of result when I failed to sign up,

enter image description here

If you see the picture, the view is not dismissed and navigationLink is triggered. But I can't understand why that is triggered because the result of signup is 'false' with completion.

How I can handle this situation? and why is the view not working?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source