Firestore How to Add User Document to Collection at Sign-in with FirebaseUI


#1

I’m trying to load a new document as uid into a Firestore Collection users when a new user signs in.

I am using FirebaseUI + VueJS and trying to populate the data using the signInSuccessWithAuthResult callback. (If there is a better way, please do tell).

Below is one of may attempts w/o success. Your help with code examples will be greatly appreciated.

firebase-config.js:

import firebase   from 'firebase';
import firebaseui from 'firebaseui'

import { usersCollection } from '@/services/registerFirebase'

export const FBuiConfig = {
	signInSuccessUrl: '/dashboard',
	signInFlow: 'popup',

	callbacks: {
		signInSuccessWithAuthResult: function(authResult, redirectUrl) {
			var newUser = authResult.user;
			addUser(newUser);
			return false;
		},

		uiShown: function() {
			document.getElementById('app-drawer').style.display = 'none';
			document.getElementById( 'app-toolbar').style.display = 'none';
		},
	},

	signInOptions: [
		firebase.auth.GoogleAuthProvider.PROVIDER_ID,
		{
			provider: firebase.auth.EmailAuthProvider.PROVIDER_ID,
			requireDisplayName: true,
			forceSameDevice: true,
		},

		firebaseui.auth.AnonymousAuthProvider.PROVIDER_ID,
	],

	tosUrl: '/tos',
	privacyPolicyUrl: function() {
		window.location.assign('/pp');
	}
};

function addUser(newUser) {
	usersCollection.add({
		displayName,
		photoURL,
		email,
		lastLoginAt,
		createdAt,
	})
	.then(function (docRef) {
		alert('Yea-baby!. Wrote ' + newUser.displayName + ' to Firestore')
	})
	.catch(function (error) {
		alert.error('Error adding document: ', error)
	})
}