I have a straightforward form that utilizes Vue.js with Firebase (specifically the package vue-firestore). I have methods to manage user registration in Firebase, change the displayName
value, and log out the current user. After this, I am registering some fields in a Firestore. However, I am struggling to save the uid of the created user to store in Firestore, as I cannot access the value of the variable in the createProvider
method.
Here is my Vue.js component:
import firebase from 'firebase/app';
import { db } from "../firebase";
import "firebase/auth";
export default {
data: () => ({
// item fields
nameProvider:"",
enderecoProvider:"",
numeroProvider:"",
cityProvider: "",
distritoProvider: "",
paisProvider:"",
complementoProvider: "",
telefoneProvider: "",
nomeContatoProvider: "",
emailProvider: "",
passwordProvider: "",
userId: "",
userIdTemp: "",
}),
firestore() {
return {
users: db.collection("users"),
};
},
methods: {
signUp() {
var that = this;
firebase.auth().createUserWithEmailAndPassword(this.emailProvider, this.passwordProvider)
.then((user) => {
this.userId = user.user.uid;
this.authState();
})
.catch((error) => {
console.log("ERROR", error);
});
},
authState() {
var that = this;
firebase.auth().onAuthStateChanged(function(user) {
if (user) {
user.updateProfile({
displayName: "2",
});
firebase.auth().signOut().then(() =>{
that.$swal({
icon: "success",
title: "Provider created successfully",
text: "For security reasons, please log in again"
}).then((result) => {
if (result.isConfirmed) {
that.$router.replace('/');
location.reload();
}
})
})
console.log(user);
} else {
// No user is signed in.
}
});
},
createProvider() {
if (this.$refs.providerForm.validate()) {
this.signUp();
this.$firestore.users.add({
nome: this.nameProvider,
telefone: this.telefoneProvider,
email: this.emailProvider,
endereco: this.enderecoProvider,
numero: this.numeroProvider,
cidade: this.cityProvider,
distrito: this.distritoProvider,
pais: this.paisProvider,
complemento: this.complementoProvider,
nomeContato: this.nomeContatoProvider,
level: "2",
uid: this.userId,
});
}
},
},
};
I am trying to pass the value of this.userId
saved in the signUp()
method to the createProvider()
method for registration in Firestore.