Home > Enterprise >  How to create users with Firebase 9 email and password and save that user's additional data in
How to create users with Firebase 9 email and password and save that user's additional data in

Time:12-12

I am struggling now with this question for couple of days. Can somebody provide precise solution based on my code. Please do not refer mi on firebase documentation because it is very unclear. I am not familiar with firebase. In my code I know the problem is somewhere in handleReg method. Curentlly, my user is being created. However, no data is writen in my firebase db collection. I need to achieve to have same doc id(uid) for the new users and his aditional data that i want to store in firebase db collection. Please somebody provide precise solution. It is very frustrating that Firebase documentation does not provide a clear explanation on how to do it. Also I check all stack overflow links. They are not offering solution to this question. Pleas Help

import React, {useState} from "react";
import { View, Button } from "react-native";
import { TextInput } from "react-native-paper";
import { doc, setDoc, collection, addDoc } from "firebase/firestore"; 
import { db } from "../firebase/firebase.authentication";
import { auth } from "../firebase/firebase.authentication";
import { createUserWithEmailAndPassword} from "firebase/auth";

export const RegisterScreen = () => {
  const [email, setEmail] = useState("");
   const [password, setpassword] = useState("");

   const HandleReg = () => {
        createUserWithEmailAndPassword(auth, email, password)
        .then(registredUser => {
            const {uid}= registredUser.user.uid
            const SetData = async ()=>{
                await setDoc(doc(db, "user", uid),{
                    name:"test"
                })  
            }              
        })
    
    }
    return (
        <>
        <View>
        <TextInput value={email}
        onChangeText={(text)=> setEmail(text)}
        />
        <TextInput
        value={password}
        onChangeText={(text)=> setpassword(text)}
        />
        
        <Button title="set" onPress={HandleReg}/>
        </View>
</>

    ); 
} 

And My Firebase js :

import {initializeApp} from "firebase/app"
import { getAuth} from "firebase/auth";
import {getFirestore } from "firebase/firestore"; 

const firebaseConfig = {
    apiKey: "xx",
    authDomain: "xx",
    projectId: "xx",
    storageBucket: "xx",
    messagingSenderId: "xx",
    appId: "xx"
  };

  const app = initializeApp(firebaseConfig);
  export const auth = getAuth(app);
  export const db = getFirestore(app);

CodePudding user response:

When it the SetData function being called? Try refactoring the function as shown below:

const HandleReg = async () => {
  const { user } = await createUserWithEmailAndPassword(auth, email, password)
  await setDoc(doc(db, "user", user.uid), { name:"test" }) 
  console.log('Document Added') 
}         
  • Related