Home  >  Q&A  >  body text

How to extract Google OAuth user data and store it in Firebase Firestore using getRedirectResult method?

I'm using Firebase Authentication to authenticate users on my website with two methods: email/password and Google OAuth login.

Whenever a user registers via the email/password method, I use the following code to save the information they entered in the Cloud Firestore document:

createUserWithEmailAndPassword(auth, email, password)
        .then((userCredential) => {
            const user = userCredential.user;
            setDoc(doc(db,"accounts",user.uid), {
                user_id: user.uid,
                first_name: first_name,
                last_name: last_name,
                date_of_birth: dob,
                email: email,
                password: password,
                date_created: date
            })
        })
        .catch((error) => {
            //pass
        };

However, I don't know how to do the same thing when they log in with Google (I'm using the redirect method): I want to extract the user data from their Google account (generally, just their name , date of birth and their Gmail address). I've looked through the documentation for the getRedirectResult() method but I'm not sure how to proceed.

I need help with some basic code to pull this data after they register.

Thanks for any help as I've been going crazy with this for the past two days.

P粉215292716P粉215292716180 days ago377

reply all(1)I'll reply

  • P粉828463673

    P粉8284636732024-04-03 12:38:45

    Unable to obtain additional information from the OAuth provider in onAuthStateChanged.

    You must be logged in to your provider to obtain this information. So before you call Firebase. The results for the provider contain the scope value you requested in step 2: https://firebase.google.com/docs/auth/web/google-signin#handle_the_sign-in_flow_with_the_firebase_sdk

    reply
    0
  • Cancelreply