0

Anyone know how I can get the user id for a certain user in Auth?

Let's say I am logged in with current user jalla@gmail.com, then I can get userid by

let userId = FIRAuth.auth()?.currentUser?.uid

But if I want to get the userId of the other user jarra@gmail.com while I am logged in as jalla@gmail.com.

How can I do that?

enter image description here

This is how I create a new user:

FIRAuth.auth()?.createUser(withEmail: txtUsername.text!, password: txtPassword.text!, completion: {
            user, error in

            if error != nil {
                print("error: " + error.debugDescription)
            }
            else{
                print("User created")
                self.login()
            }
        })

Which result in the authenticated user in the image attached.

I also have a Users table, but this has nothing to do with authenticating users for login.

enter image description here

The thing is that I need to programatically add new authenticated users. Which is ok, but how can I query data from the Authenticated table? I need to do it because I add the authenticated uid to each user roots in the database (see User table image)

adjuremods
  • 2,938
  • 2
  • 12
  • 17
TommyF
  • 381
  • 1
  • 7
  • 22

2 Answers2

0

For each user, create a record in your Firebase Database and store uid for each user so that you can retrieve that uid once you need it.

If you will query uid information by using the email information of the said user (i.e. jarra@gmail.com in your case), it might be best to store user information including uid under a key of respective user's email.

Ali Kayhan
  • 26
  • 3
  • Using email's as a key can lead to a whole slew of issues; email addresses change and if you are referring to users by their emails in other areas of the Firebase structure (which is common), that will break if a user email changes. It's best practice to separate dynamic data (like an email) from node keys, which must remain static - leveraging the Firebase assigned UID is the way to go. – Jay Oct 18 '16 at 17:30
0

If you are logging in code rather than the console, one option is to leverage a /users node.

With Firebase, it's common practice to have a /users node that stores information about your users.

users
  uid_001
    email: "dr@who.com"
    name:  "Dr. Who"
    fav_food: "Jelly Babies"
  uid_002
    email: "homer@simpsons.com"
    name: "Homer Simpson"
    fav_food: "Doughnuts"

The uid's used as the key to each user node are the ones created by Firebase when the user is created.

From there, it's a snap to query for the email address which will retrieve the node in question, the parent key being the uid of the user.

Jay
  • 34,438
  • 18
  • 52
  • 81
  • Yes, I have a users node like the one you have added. But the key of each subnode is the authenticated uid. So each time I create a new authenticated user programatically I need to get that new uid. To add in the subnode of the user node (like your uid_001 and did_002) – TommyF Oct 18 '16 at 20:59
  • @TommyF Oh, you've discovered one of my issues with Firebase 3. Check out this answer and a brilliant workaround from Andre [Firebase kicks out current user](http://stackoverflow.com/questions/37517208/firebase-kicks-out-current-user/38013551#38013551). The thing to do is to use another firebase account to create the user and then grab their uid so you can create the node in the /users node. – Jay Oct 18 '16 at 22:06
  • That looks interesting. But I can't figure out how to init a second account with swift. Do you know ;) – TommyF Oct 18 '16 at 23:26
  • I do believe that is javascript – TommyF Oct 19 '16 at 19:18
  • @TommyF Wow - yes it is, I just auto converted it in my brain. lol. What you need to do is to create a second FIRApp and then FIRAuth.authWithApp(). – Jay Oct 19 '16 at 21:37