Firebase
Get Started
If you want to use Firebase, please create a new account or login to your existing one.
[creativ_button url=”https://console.firebase.google.com/” icon=”user” label=”Firebase – Console” colour=”blue” colour_custom=”” size=”medium” edge=”rounded” target=”_blank”]
Create a project
First of all you need to create a new project (alternatively you can login to AdMob and export your existing app to Firebase)
Click on Create new Project and fill out all needed information.
Then confirm the dialog.
Now you are able to add Apps to your project by clicking the “Add App” button (upper right corner).
Then select the platform of your app.
Fill out all needed information for your choosen platform and fill out all needed information.
Auth
Here you can add and configure login handlers for instance
- Facebook Login
- Google Login
- Twitter Login
- Email / Password Login
- and so on
Email & Password
No further settings needed. Just activate this handler.
No further settings needed. Just activate this handler.
In order to activate the Facebook Login Handler you have to create a Facebook App first. If you need help with that, please visit this guide.
[creativ_button url=”https://shatter-box.com/knowledgebase/cocoonio/#cocoonfacebook” icon=”facebook-sign” label=”Create a Facebook App” colour=”blue” colour_custom=”” size=”medium” edge=”rounded” target=”_blank”]
If you already have a Facebook App, just login to Facebook Developers and copy the APP ID and the APP SECRET…
…and paste it at Firebase.
Then copy the …/auth/handler (blue marked) URL and paste it at your Facebook App.
In order to activate the Twitter Login Handler you have to create a Twitter App first.
[creativ_button url=”https://apps.twitter.com/” icon=”twitter-sign” label=”Create a Twitter App” colour=”blue” colour_custom=”” size=”medium” edge=”rounded” target=”_blank”]
Copy your APP ID and your APP SECRET and paste it at Firebase.
Then copy the Auth URL and paste it at your Twitter App (Settings Tab).
OAuth Redirection
If you are using Social Handlers (Facebook, Google, …) add your domain to the list below. Otherwise the login will be blocked and you’ll get this error message in the browser console.
So just add the domain where your game is running on to the list below.
Realtime Database
Firebase offers a realtime database for your game / app. The structure of it can be created in Construct 2 via the Firebase plugin. However to assure that only the data owner can read and write its data, we need to add some rules to the database.
Default
[js]{
“rules”: {
“.read”: “auth != null”,
“.write”: “auth != null”
}
}[/js]
Above shown is the default code for the rules. This says that only logged in user can access the data from the database.
New
[js]{
“rules”: {
“users”: {
“$user_id”: {
// grants write access to the owner of this user account
// whose uid must exactly match the key ($user_id)
“.write”: “$user_id === auth.uid”,
“.read”: “auth != null && auth.uid == $user_id”
}
}
}
} [/js]
But in order to differ the data for each user, we have to add following code (more information can be found in the docs of Firebase). So overwrite the default rule with the rules shown above.
Construct 2
At the moment there is now mobile Firebase plugin for Construct 2 available. Only for HTML5 games.
Live Demo
If you want to check out a live demo of the Firebase Service, please visit following link (Scirra Acarde).
[creativ_button url=”https://www.scirra.com/arcade/tutorial-games/game-template-14-firebase-cloud-data-game-account-9616″ icon=”search” label=”Game Templat 14 – Cloud Data Sync with Firebase (WEB)” colour=”blue” colour_custom=”” size=”medium” edge=”rounded” target=”_blank”]
Requirements
If you want to use Firebase you’ll need to add at least following plugins made by Rexrainbow.
Additions
Additionally you can download and install following plugins made by Rexrainbow.
Capx Examples
You can download a complete Firebase Template in the Scirra Store.
[creativ_button url=”https://goo.gl/eWJgWL” icon=”download-alt” label=”Firebase – Game Accounts” colour=”blue” colour_custom=”” size=”medium” edge=”rounded” target=”_blank”]
Firebase API
Configuration
In order to get your ID’s for the Firebase API v3 Plugin, select your Project in Firebase and click on the little gear icon (next to your Project Name) and then Project Settings.
Then scroll down and download the “google-services” JSON-file. There you’ll find all needed ID’s to configure the Firebase API v3 plugin.
Open the JSON-file with an editor of your choice.
Copy the corresponding ID’s from the JSON-file to the Firebase API v3 Plugin.
- (1) API Key: Copy the as it is from the JSON-File
- (2) Auth Domain: Copy it WITHOUT https://
- (3) Database URL: Copy it WITH https://
- (4) Storage Bucket: Copy it as it is
Hi, where i find
(4) Storage Bucket:
?
Hi,
Just see the screenshot above in the guide. You can download the json file with your ID’s out of your Firebase project.
Regards
Andy
Where exacly i can download json file ?
Just go to firebase and select your project. Then click on settings, there you’ll find the file 🙂
Regards
Andy
i see in my project
-Data
-simulator
-analytics
-login & auth
-hosting
-secrets
but no settings
It’s all described in the guide above where you find the JSON file.
https://shatter-box.com/knowledgebase/firebase-sync-data-with-your-construct-2-game/#firebase-api
Just click on the gear symbol shown on the image above, then select Project Settings and download the google-services.json file.
Just to add something that took me a while to figure out…the auth domain ends in *firebaseapp.com…if you see local host under OAuth redirect domains (Auth Tab) it’s the address below. Thanks for this tutorial.
Hello I’m new to firebase concept and I do no that How to use the firebase in construct 2? I am working on cricket project in construct 2 tell me that how can I make it as multiplayer one using firebase
Hi,
I haven’t made a game using Firebase for multiplayer, so I can’t tell you how to do it. It’s better if you ask that question in the scirra forum.
Regards
Andy
Thanks, that’s incredibly helpful.
I want to send variables (questionnaire answers) to the Firebase database. Any suggestion what the best way it to do so?
I figured it out finally, using the itemtables plugin, see https://www.scirra.com/tutorials/5015/getting-started-with-firebase
However, to make it work, you need to change the firebase usage rules to public: see https://firebase.google.com/docs/database/security/quickstart
{
“rules”: {
“.read”: true,
“.write”: true
}
}
Thank you for sharing 🙂
I never have used the itemtable plugin.
Regards
Andy
I’m getting an error of: “TypeError: Cannot read property ‘name’ of null(…)”
Claims there is an issue with Line 49 Col 27 of the runtime.js and cannot read “database”.
It might be a problem to bring to RexRainbow, but thought I’d try here first. I’ve played around with the account information entries and nothing seem to change the error, only deleting the V3 plugin works.
Okay, I ended up uninstalling all the Firebase plugins and then shutting down C2. Restarted C2 and added the V3 API plugin, no problem.
Of course then I added the Auth plugin and got an error but I think I know what is wrong there.