iosfan iosfan - 2 months ago 21
Swift Question

Firebase How to verify an email to log in belongs to which category in database?

I am working on new

Firebase
Log in. There are two categories in database. "Cooks" and "Customer". "Cooks" has a CookViewController and "Customers" has a customerViewController. I need to know which category this email belongs to so after logging in,
segue
to
CookViewController
or
CustomerViewController
. Below is code for sign in, but I have no idea how to tell which category the email belongs to. Thanks!

@IBAction func loginPressed(sender: AnyObject) {

if let email = emailField.text where email != "", let password = passwordField.text where password != "" {

FIRAuth.auth()?.signInWithEmail(email, password: password, completion: { (user, err) in

if err != nil {

print(err)

} else {

// Here I need to know whcih view Controller to Segue to:

self.performSegueWithIdentifier(identifier: String, sender: self)

}

}


Data Structure as below:

enter image description here

Answer

How about making your JSON like :-

yourApp:{
 cooks:{
       email1:true,
       email3:true,
       email4:true,
       email7:true,
       email8:true},
 customers:{
       email2:true,
       email12:true,
       email13:true,
       email4:true,
       email8:true},
 users:{
       uid1:{
          email : blah@blah.com,
          isCook: true
            },

       uid2:{
          email : burp@blah.com,
          isCook: false
            }....
          } 
        }

So you can either check in the cooks section or the customer section or the uid under users section itself and then relating to the isCook node, if he is ->Go to cookViewController, else go to customerViewController

Easiest way would be to check in the uid:-

 FIRDatabase.database().reference().child("users/\(FIRAuth.auth()!.currentUser!.uid)/isCook").observeSingleEventOfType(.Value,withBlock:{(snap) in 
    if let isThisUserCook = snap.value as? Bool //or String{
           if isThisUserCook == true{

                //segue to cookVC
              }else{
                //segue to customerVC

                 }
            }
      })