FakeEmpire
FakeEmpire

Reputation: 798

Add user data to database with firebase (React) (Google authentication)

I am trying to add user data to Firebase database on Sign-in. The user shows up in Authentication, but there is no data being posted to the actual database.

import firebase from 'firebase';

const config = {
  apiKey: "",
  authDomain: "",
  databaseURL: "",
};
firebase.initializeApp(config);


const signIn = () => {
  var provider = new firebase.auth.GoogleAuthProvider();
  var promise = firebase.auth().signInWithRedirect(provider)

  promise.then( result => {

    var user = result.user;

    firebase.database().ref('users/'+user.uid).set({
      email: user.email,
      name: user.displayName
    })

  }).catch( e => {
    //handle errors

  });

Upvotes: 0

Views: 1179

Answers (1)

bojeil
bojeil

Reputation: 30868

firebase.auth().signInWithRedirect(provider) will redirect the page to the OAuth provider and then back to the current page. To get the result and save the user, you need to call:

firebase.auth().getRedirectResult().then(result => {
   var user = result.user;
   if (user) {
     // Save user.
   }
}).catch(e => {
});

Upvotes: 1

Related Questions