Basic Firebase Setup using C3's built in Javascript.

3

Stats

2,494 visits, 3,255 views

Tools

Translations

This tutorial hasn't been translated.

License

This tutorial is licensed under CC BY 4.0. Please refer to the license text if you wish to reuse, share or remix the content contained within this tutorial.

Published on 4 Jun, 2019.

This tutorial is based off of Sparsha's tutorial found here: https://www.construct.net/en/tutorials/construct-firebase-2163

Please thank him for his work.

Setting up Firebase

  1. Create a new script in construct 3 and insert the code below.
  2. Observe the instructions in the configHTML variable and edit it accordingly.
  3. Reference the firebase documentation to understand how the firebase and db objects can be used to read/write database values and log players in.

Documentation

https://firebase.google.com/docs/firestore/manage-data/add-data

https://firebase.google.com/docs/firestore/query-data/get-data

https://firebase.google.com/docs/auth/web/start

Example Implementation

Template Code

var configHTML = 'YOUR FIREBASE CONFIG HERE, KEEP IT ON 1 LINE. EXAMPLE: var firebaseConfig = { apiKey: "EXAMPLE", authDomain: "EXAMPLE.EXAMPLE.COM",}'
	+'firebase.initializeApp(firebaseConfig); var db = firebase.firestore();';

runOnStartup(async runtime =>
{
	// Code to run before the first layout starts
	initializeFirebase();
});

function initializeFirebase(){
	var script1= document.createElement('script');
	var script2= document.createElement('script');
	var script3= document.createElement('script');
	script1.src= "https://www.gstatic.com/firebasejs/6.1.0/firebase-app.js";
	script2.src= "https://www.gstatic.com/firebasejs/6.1.0/firebase-auth.js";
	script3.src= "https://www.gstatic.com/firebasejs/6.1.0/firebase-firestore.js";
	document.getElementsByTagName('body')[0].appendChild(script1);
	document.getElementsByTagName('body')[0].appendChild(script2);
	document.getElementsByTagName('body')[0].appendChild(script3);
	script3.onload=function(){startFirebase()};
}

function startFirebase() {
	var script4= document.createElement('script');
	script4.innerHTML = configHTML;
	document.getElementsByTagName('body')[0].appendChild(script4);
}
  • 1 Comments

  • Order by
Want to leave a comment? Login or Register an account!
  • Hi there,

    I can't figure out how to get this to connect. I keep getting a token error on the var firebaseConfig and when I remove the var configHTML it lets me load the app but the authentication doesn't create an account when clicked.

    Any pointers?

    Regards