Do you like my tutorials?

Then consider supporting me on Ko-fi

Talking about Javascript.

Do you know Firebase? Firebase is a mobile platform that helps you quickly develop high-quality apps, grow your user base, and earn more money. Firebase is made up of complementary features that you can mix-and-match to fit your needs. I am finding Firebase very interesting, especially when it’s time to add some features to games. Today I will show you a brief overview of the service, showing you how to make the simplest user authentication ever: an anonymous authentication. Don’t be scared about anonymous authentication if you plan to fidelize your players as Firebase allows you to turn anonymous users into registered users at any time. So let’s get started with your first project, navigate to the console and click on “CREATE NEW PROJECT” Name your project, enter your location and click on “CREATE PROJECT” Today we are creating a web app, so select “Add Firebase to your web app” Once in the app dashboard, select “Auth” then “SET UP SIGN IN METHOD”. You will see all authentications methods are disabled, and we’ll enable “Anonymous” Enable it and save Your authentication now should be enabled. That’s what we are going to do now: you can authenticate and get an unique id which will remain the same even if you reload the page.
Signing out will delete your authentication unique id. Remember it’s an anonymous authentication. Look at the code:
<html>
<head>

<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script src="https://www.gstatic.com/firebasejs/live/3.0/firebase.js"></script>
<script>

     $(document).ready(function(){
          var config = {
               apiKey: "AIzaSyD4NczHDYCUHIeX8X-ZXxErwQZvdTLiflA",
               authDomain: "test-c2cc1.firebaseapp.com",
               databaseURL: "https://test-c2cc1.firebaseio.com",
               storageBucket: "test-c2cc1.appspot.com",
          };
          
          firebase.initializeApp(config);
          
          firebase.auth().onAuthStateChanged(function(user){
               if(user){
                    var uid = user.uid
                    $("#message").html('user authenticated with uid ' + uid + ' - <a id = "signout" href = "#">sign out</a>')
               }
               else{
                    $("#message").html('user not authenticated - <a id = "authenticate" href = "#">authenticate</a>');
               }
          
          })
          
          $(document).on("click", "#authenticate", function(){
               firebase.auth().signInAnonymously();                    
          });
          
          $(document).on("click", "#signout", function(){
               firebase.auth().signOut();                    
          })
                    
     });

</script>
    
</head>
<body>
     <div id = "message" style = "font: normal 20px Helvetica"></div> 
</body>
</html> 

Everything is inside these four methods: firebase.initializeApp(config);: creates and intializes a Firebase app. config is an object with configuration options. firebase.auth().signInAnonymously();: asynchronously signs in as an anonymous user. If there is already an anonymous user signed in, that user will be returned; otherwise, a new anonymous user identity will be created and returned. firebase.auth().signOut();: signs out the current user. firebase.auth().onAuthStateChanged(callback);: adds a listener for auth state changes and executes callback function. That’s all at the moment, next time I will show you how to integrate this features and more features in your games.

Never miss an update! Subscribe, and I will bother you by email only when a new game or full source code comes out.