Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

155
Views
Create Firestore document only the first time a user signs in with google oAuth

I'm new to react and firebase and was trying to create a firestore document only during the creation of user.

Its working fine on the local provider as there sign in and sign up are two different things but when it comes to google o Auth both the options use same function.

So how do i create my initial documnet during creation of user via google o Auth as it just resets my entire document when i log out and log in back.

googleAuth function

    function googleAuth(provider) {

        return firebase
            .auth()
            .signInWithPopup(provider)
            .then(async function createUserDb(userCredentials) {
                await setDoc(doc(db, "users", userCredentials.user.uid), {myList: []},{ merge: true })
                })
    }

sign up and sign in functions

    function signUp(email, password) {
        return auth.createUserWithEmailAndPassword(email, password).then(
            async function createUserDb(userCredentials) {
                console.log(userCredentials.user.uid);
                await setDoc(doc(db, "users", userCredentials.user.uid), {myList: []})
    })
    }

    function signIn(email, password) {
        return auth.signInWithEmailAndPassword(email, password)
    }

my firestore users collection when a new user is created (google auth user here)

firestore user collection

it should save the data even after sign in (local auth user here)

enter image description here

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

after scraping through some posts found .additionalUserInfo.isNewUser method which checks if the user has created new account or is already a user which fixed my issue.

Thank you for any help that you guys provided.

    function googleAuth(provider) {

        return firebase
            .auth()
            .signInWithPopup(provider)
            .then(async function createUserDb(userCredentials) {
                if(userCredentials.additionalUserInfo.isNewUser) {
                    await setDoc(doc(db, "users", userCredentials.user.uid), {myList: []},{ merge: true })
                } 
                })
    }
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!