MacD
MacD

Reputation: 586

How To Store Firebase Users Under UID

I have been storing all my users in my firebase database using the push() method. This method generates a unique id for each user and stores them under this id. I am trying to change this so that my users are stored under a user.uid that is auto generated as well when you use the set() method, however, I'm having trouble storing them under this user uid.

I am using the following to add users to the database, what am I missing here?

firebase.database().ref().child('accounts').set({
                email: user.email,
                userId: firebase.auth().currentUser.uid

              })

this uses set() and stores the data like this:

{
  "accounts" : {
      "email" : "",
      "userId" : ""
    },
{
      "email" : "",
      "userId" : ""
    }
}

Using push() the structure looks like this: The users are stored based on the firebase generated push Id.

{
  "accounts" : {
    "-KRPSyO4B48IpBnHBTYg" : {
      "email" : "",
      "userId" : ""
    },
    "-KR4f4s5yvdr67g687nrth" : {
      "email" : "",
      "userId" : ""
    }
  }

What I'm looking for is this structure: Users are stored by the firebase generated userId..

{
      "accounts" : {
        "userId value" : {
          "email" : "",
          "userId" : ""
        },
        "userId value" : {
          "email" : "",
          "userId" : ""
        }
      }

Any help would be greatly appreciated!!

Upvotes: 1

Views: 5917

Answers (2)

azizkale
azizkale

Reputation: 1

you can retrieve the userId after signing up and then save it by userId

 const signUpFirebase = async(_, { email, password, name }) => {
const auth = await getAuth(firebaseApp);
let accessToken;
//creating and saving user in firebase authenticate
await createUserWithEmailAndPassword(auth, email, password).
then(async(userCredential) => {
    // Signed in 
    accessToken = await userCredential.user.stsTokenManager.accessToken;

    //getting userId from Firebase Authentication
    let userId;
    await auth.onAuthStateChanged((user) => {
        if (user) {
            // User logged in already or has just logged in.
            userId = user.uid;
        } else {
            // User not logged in or has just logged out.
        }
    });

    //saving in firebaseDB
    const db = await getDatabase();
    await set(ref(db, 'users/' + userId), {
        name: name,
        email: email,
        userId: userId
    });

    return accessToken;
}).catch((error) => {
    const errorCode = error.code;
    const errorMessage = error.message;
    return errorMessage;
});
if (accessToken == null)
        return "Please try again"
    else 
        return accessToken; 
}

Upvotes: 0

Frank van Puffelen
Frank van Puffelen

Reputation: 598728

You're not yet creating a node for the uid itself in your path. Change it to:

var uid = firebase.auth().currentUser.uid;
firebase.database().ref().child('accounts').child(uid).set({
  email: user.email,
  userId: uid
})

Upvotes: 7

Related Questions