0

I want to get the user id on my newly created user so I could name my document to that uid, but it says null.

here's my code

addDriver(){
    var $this = this
    secondaryApp.auth().createUserWithEmailAndPassword($this.driver.email, $this.driver.password).then(function(user){
        $this.$db.collection("driver").doc(user.uid)
            .set({
                fname: $this.driver.fname,
                lname: $this.driver.lname,
                contact_number: $this.driver.cnum,
                license_number: $this.driver.license,
                email: $this.driver.email,
                password: $this.driver.password,
            })
            secondaryApp.auth().signOut();
        $this.formHeaderStatus = $this.$db.auth().currentUser
    })
},

Also, am I doing this right? coz I'm logged in as an admin? and I want to create a new user without logging myself out.

aminography
  • 21,986
  • 13
  • 70
  • 74
Jay
  • 25
  • 4
  • There is no way to create a new user account in the client-side Firebase Authentication SDK without signing that user in. And only a single user can be signed in at once, so this will automatically sign out the existing user. The client-side SDK has no concept of "an admin". For administrative functionality such as this, you should be using the Admin SDK in a trusted environment (such as your development machine, a server you control, or Cloud Functions). See https://stackoverflow.com/q/37517208 and specifically my answer: https://stackoverflow.com/a/37614090/209103 – Frank van Puffelen Nov 06 '19 at 12:50
  • You already seem to be doing what is suggested in the answers I linked. Please focus on keeping your question minimal, as it allows us to focus on what you actually need (which I answered below). – Frank van Puffelen Nov 06 '19 at 12:56

1 Answers1

0

I am not entirely sure but I believe the problem lies when using the promise returned by createUserWithEmailAndPassword.

From what I have gathered it should be like this :

addDriver(){
    var $this = this
    secondaryApp.auth().createUserWithEmailAndPassword($this.driver.email, $this.driver.password).then(function(**data**){
        $this.$db.collection("driver").doc(**data.user.uid**)
            .set({
                fname: $this.driver.fname,
                lname: $this.driver.lname,
                contact_number: $this.driver.cnum,
                license_number: $this.driver.license,
                email: $this.driver.email,
                password: $this.driver.password,
            })
            secondaryApp.auth().signOut();
        $this.formHeaderStatus = $this.$db.auth().currentUser
    })
},

Hope it helps, if not, let me know.

But as Franck said, if you want to sign a new user up while staying logged-in, you should use the Admin-SDK in your back end or Cloud Functions

Dokt
  • 199
  • 6
  • Ohhh.. heck. big help. i thought you could just put anything inside the .then(**var**) thing, it works now mang! thanks for this – Jay Nov 06 '19 at 14:43