Rafael Moura
Rafael Moura

Reputation: 1327

Get UID and insert in object in database firebase Angular 6

I need to get UID that user created in Authentication and insert this UID in item created in Database Firebase, firt I create user in my auth.ts

createUser(user: any, path: string) {
return this.afu.auth.createUserWithEmailAndPassword(user.email, user.password)
                            .then((res) => { 
                              let id = res.uid;
                              this.teste(id, path);
                              return this.service.save(user, path)
                            });
}

in this service I'll send to another service to create one object relationship him now my userService.ts, so how to get this UID and insert in user/id atribute?

save(user: any, path: string) {
return new Promise((resolve, reject) => {
    return this.db.list(this.PATH + path)
            .push(user)
            .then(() => resolve())
   })
}

enter image description here

Upvotes: 0

Views: 2496

Answers (2)

Rafael Moura
Rafael Moura

Reputation: 1327

I did so in my code with base in your code friend and work to me thanks friend

createUser( user: any, path: string): Promise<any> {
return firebase.auth().createUserWithEmailAndPassword(user.email, user.password)
  .then( newUser => {
    firebase.database()
    .ref(`/users/${path}/`)
    .child(newUser.uid)
    .set(user);
  });
}

Upvotes: 0

user4799206
user4799206

Reputation:

If I hope I did not understand your question wrong.

Like mentioned here https://firebase.google.com/docs/auth/web/manage-users

var user = firebase.auth().currentUser;
var name, email, photoUrl, uid, emailVerified;

    if (user != null) {
      name = user.displayName;
      email = user.email;
      photoUrl = user.photoURL;
      emailVerified = user.emailVerified;
      uid = user.uid; 
    }

Now you got your current user's id.

now insert it to your table:

ref.child("something"+count).set({
  uid: this.uid,
  email: this.email,
  //and more
});

count stands for the id of the object for example:

{
  "0": {
      "uid": "22",
      "email": "[email protected]"
    },
   "1": {
      "date_of_birth": "33",
      "full_name": "[email protected]"
    }
} 

is your corresponding json file when you ask for something table.

Upvotes: 2

Related Questions