HomeiOS Developmentios - Why is my variable reverting again to nil after being...

ios – Why is my variable reverting again to nil after being set in swift?


I am utilizing Firebase to register and login (electronic mail/password) customers. The registration circulation is for a person to enroll with the shape within the app, confirm their electronic mail (completed by firebase), after which wait to have their account activated manually.

When the person registers, it creates a brand new doc in ‘Customers’ assortment, with the knowledge they offered at registration, and by default units IsActiveUser to false.

When a person efficiently logs in, two checks are made – one which Auth.auth().currentuser?.isEmailVerified is true (this half works), and one other retrieves the person’s data from the ‘Customers’ assortment, and checks if the sphere isActiveUser is true (this half would not).

I’ve added two print() statements to trace the variable, and it really works when the doc is retrieved by docRef.getDocument, however then reverts to nil exterior previous to the if assertion verifications.

struct LoginView:View {
    @State non-public var isActive: Bool?

    Button("Login", motion: {
        Auth.auth().signIn(withEmail: userEmail, password: userPass) { authresult, error in
            if let e = error {
                //show error
            } else {
                // Person has right person/move
                let userID = Auth.auth().currentUser?.uid
                let docRef = db.assortment("Customers").doc(userID!)
                docRef.getDocument { (doc, error) in
                    if let doc = doc, doc.exists {
                        print(doc.information()!)
                        isActive = doc.information()!["isActiveUser"]! as? Bool
                        print("Inside if/let: (isActive)") //this appears to work, and pulls the right data
                    } else {
                        print("Doc doesn't exist")
                    }
                }
                print("Earlier than validation (isActive)") //right here it prints nil
                
                if Auth.auth().currentUser?.isEmailVerified == false {
                    // E mail deal with must be verified. Alert person and indicators out. This works.
                } else if isActive == false {
                    // Account has not been activated by administrator. Alert person and indicators out. This doesn't work.
                } else {
                    // Person is efficiently logged in.
                }
            }
        }
    }).buttonStyle(BrandButtonStyle())
        .alert(isPresented: $showingAlert) {
            Alert(title: Textual content(errorTitle), message: Textual content(errorText), dismissButton: .default(Textual content("OK")))
        }

}

Output:

Earlier than validation nil
["userEmail": [email protected], "userID": moAyFkkhd9gXDpnRZwadHc5rtL22, "isActiveUser": 0, "userName": Foo Bar]
Inside if/let: Elective(false)

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments