Bob
Bob

Reputation: 27

Trying to route certain type of users to different home screen Swift

I am creating an application with two different types of user. I want one type of user to go to one home screen and another type of user to go to a different home screen. This is done based on an attribute called "accountType" that is set to "reporter" by default when the user is created and can be changed by an admin at a later time. I am having trouble writing the if statement to get this to happen:

//create the user in Firebase
    if let email = emailAddressTextField.text, let pass = passwordTextField.text{
        Auth.auth().createUser(withEmail: email, password: pass, completion: { (user, error) in
            if let u = user{
                //user is found, go to home screen
                var lastName = self.lastNameTextField.text
                var firstName = self.firstNameTextField.text
                var accountType = "reporter"
                self.ref.child("Users").child((user?.uid)!).setValue(["email": email, "first_name": firstName, "last_name": lastName, "accountType": accountType])

                let userID = Auth.auth().currentUser?.uid

//trying to get the correct type of user to the correct home screen

                if self.ref.child("Users").child((userID?.uid)!).child("accountType") = "reporter" {
                    self.performSegue(withIdentifier: "goToHome1", sender: self)
                }
                else {
                    self.performSegue(withIdentifier: "goToHome2", sender: self)
                    }

            }

Upvotes: 0

Views: 302

Answers (1)

Dravidian
Dravidian

Reputation: 9955

What you are doing is only defining a Firebase Path, you also have to observe the value in your database referring to that path, Try this :-

 self.ref.child("Users").child((userID?.uid)!).child("accountType").observeSingleEvent(of: .value, with: {(Snapshot) in

        if let accountT = Snapshot.value as? String{

            // You have retrieved the account type
            // Now Segue

            if accountT == "reporter"{

                self.performSegue(withIdentifier: "goToHome1", sender: self)

            }else{

                self.performSegue(withIdentifier: "goToHome2", sender: self)

            }

        }else{

            // There was no node found in the database
            // named 'accountType'

        }


    }, withCancel: {(Error) in

        // There was some error with the firebase call
        // Handle that error

        print(Error.localizedDescription)


    })

Upvotes: 1

Related Questions