'Verified users access home and unverified users stay in the login

I am checking if the user got a verification email after registration inside login function:

static func authenticate(withEmail email :String,
                         password:String,
                         completionHandler:@escaping (Result<Bool, EmailAuthError>) -> ()) {
    Auth.auth().signIn(withEmail: email, password: password) { (authResult, error) in
        // check the NSError code and convert the error to an AuthError type
        var newError:NSError
        if let err = error {
            newError = err as NSError
            var authError:EmailAuthError?
            switch newError.code {
            case 17009:
                authError = .incorrectPassword
            case 17008:
                authError = .invalidEmail
            case 17011:
                authError = .accoundDoesNotExist
            default:
                authError = .unknownError
            
            }
            completionHandler(.failure(authError!))
        } else {
            if (Auth.auth().currentUser!.isEmailVerified == true){
            completionHandler(.success(true))
                print ("Email Verified")
            }
            else {
                print ("Email not verified")
            }
        }
    }
}

This function will be called after clicking "Login" button where I reload user to make sure that (isEmailVerified) value updated.

Button(action: {
            //Sign In Action
            FBAuth.reloadUser()
            FBAuth.authenticate(withEmail: self.user.email, password: self.user.password) { (result) in
                switch result {
                case .failure(let error):
                    self.authError = error
                    self.showAlert = true
                case .success( _):
                    print ("Signed in.")
                }
            }
         
        }

The Content View where I am checking the user's login status, I did not update it after I added isEmailVerified to my code coz I have only three cases undefined, signedIn, signedOut, I don't know how to get out of this box:

struct ContentView: View {
@EnvironmentObject var userInfo : UserInfo
@State var shouldShowSignUp : Bool = false
var body: some View {
    Group {
      if shouldShowSignUp {
       SignupView()
        }
      else  {
        ZStack{
if userInfo.isUserAuthenticated == .undefined{ SignupView()}
else if userInfo.isUserAuthenticated == .signedIn && Auth.auth().currentUser!.isEmailVerified == true {HomeView()} 
else if userInfo.isUserAuthenticated == .signedIn && Auth.auth().currentUser!.isEmailVerified == false {LoginView()}
          
      SplashScreen()
            }
        }
    }
    
    .onAppear {
        self.userInfo.configureFirebaseStateDidChange()
    }
}}

UserInfo :

import Foundation
import FirebaseAuth

class UserInfo : ObservableObject{
enum FBAuthState{
    case undefined, signedOut, signedIn
}
@Published var isUserAuthenticated : FBAuthState = .undefined
@Published var user : FBUser = .init(uid: "", name: "", email: "")

var authStateDidChangeListenerHandle : AuthStateDidChangeListenerHandle?

func configureFirebaseStateDidChange (){
    authStateDidChangeListenerHandle = Auth.auth().addStateDidChangeListener({ (_,user) in
        guard let _ = user else {
            self.isUserAuthenticated = .signedOut
            return
        }
        self.isUserAuthenticated = .signedIn

    })
    
}
 
}

Everything is going fine. I get the correct value for isEmailVerified. My question is how to give verified users access to the homeView and unverified users they will get alert that their email needs to be verified and they will stay in the loginView.



Solution 1:[1]

In the menu File, Options you can alter the behaviour in case of circular references. Once you have done this, you can open your file and search for the circular references without problem:

enter image description here

Sources

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

Source: Stack Overflow

Solution Source
Solution 1 Dominique