Construct 2 & Firebase using JS

20

Stats

9,996 visits, 16,056 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 3 Apr, 2019. Last updated 28 Nov, 2021

Introduction

Firebase is an app development platform which provides API (Application Programming Interface) primarily for Database related services. This tutorial covers Authentication and Firebase Realtime Database services which are the most required services for Game Development to store user data.

Authentication

It stores user information and enables firebase to recognise users by using a SignUp/Login screen, or anonymous auth services. This tutorial only deals with creating accounts using Email and Password, and Google Account. Information on other modes of authentication can be found in the Official Firebase Docs- Web Authentication.

Firebase Realtime Database

It is a cloud-hosted database. Data is stored as JSON and synchronized in realtime to every connected users. Also a high score system can be built using this service.

For more information or to include more Firebase functions just follow the instructions in the Official Firebase Docs.

Index

00:58 Firebase Initialization Part I- Asynchronous Operations

02:18 Firebase Initialization Part II- JavaScript Code

04:17 Firebase Authentication

05:58 Firebase Realtime Database (including a Leaderboard System)

10:10 Construct 3 events

14:47 Security Rules for Firebase Realtime Database

Video

m.youtube.com/watch

Subscribe to Construct videos now

Code Snippets for each function-

Load Scripts using JavaScript-

function load(callback){
	var script= document.createElement('script');
	script.src= "address/sc.js";
	document.getElementsByTagName('head')[0].appendChild(script);
	script.onload=function(){callback()};
}
function process()
{
	//the script has been loaded, do anything here
}
load(process);

Firebase Authentication-

//Sign Up with email & password
firebase.auth().createUserWithEmailAndPassword(email, password);

//Sign In with email & password
firebase.auth().signInWithEmailAndPassword(email, password);

//Sign In with an existing Google account
var provider= new firebase.auth.GoogleAuthProvider();
firebase.auth().signInWithPopup(provider);

//Sign Out
firebase.auth().signOut();

Catch errors-

firebase.auth().signOut().catch(function(error){
	//Handle Errors
	var errorCode= error.code;
	var errorMsg= error.message;
})

Get Current User-

//Get current user
firebase.auth().onAuthStateChanged(function(firebaseUser){
	if(firebaseUser){
		//User is signed in
		var userID= firebase.auth().currentUser.uid;
		var userEmail= firebase.auth().currentUser.email;
	}
	else
		//User is signed out
})

Firebase Realtime Database-

//Set value to the Realtime-Database at a specific path
firebase.database().ref('path/').set({
	val: 'value',
})

//Read value of ‘val’ from the above example
var dbRef= firebase.database().ref('path/val');
dbRef.on('value', function(snapshot) {
	var storeval= snapshot.val();
})

Disabled Comments have been disabled by the owner.