Guide for Firebase demo
Create and initialize project in Firebase 🛠
- Create the project with normal account in the console
- Note that the UPorto accounts seems to not have the firebase module
- Enable the anonymous login in the Authentication
-
Create the Real time DB
- Create some entries (add children to child)
- Look at the access rules (see Firebase: Real time DB Rules)
-
In the console for the Real time DB add the
Rules
:{ "rules": { ".read": "auth != null", ".write": false, "users": { "$uid" : { ".write": "auth != null && auth.uid == $uid" } } } }
Install the demo web app 🔨
-
Download the sample project for RT and unzip it. Have a look 👀 at
index.html
andscripts/main.js
. - Add an App to the project on the Firebase project's overview on the console. For this example, add a Web App with no hosting.
-
Copy the generated code to the
index.html
from the sample project. It should replace the current<script src=...>
at the end of the file. -
Add the libraries for authentication and accessing the RT DB (see Ways to add the Web SDKs to your app):
<!-- TODO: Add SDKs for Firebase products that you want to use https://firebase.google.com/docs/web/setup#available-libraries --> <script src="https://www.gstatic.com/firebasejs/8.2.10/firebase-auth.js"></script> <script src="https://www.gstatic.com/firebasejs/8.2.10/firebase-database.js"></script>
- Run a local web server with
npm
'shttp-server
for testing (you can use another web server).- Install NodeJS.
-
Install the package for the
http-server
:
npm install --global http-server
-
Run the following command on the dir where you extracted the sample project:
http-server -a 127.0.0.1
-
Open a web browser on http://127.0.0.1:8080/ (check the output of the
http-server
to confirm the URL)
Configure authentication 🔐
- Open the browser's web console (usually
Ctrl+Shift+i
) for debugging. - Try the login and check the errors on the web console:
- Fails due to
127.0.0.1
not being allowed by Firebase ⇒ add127.0.0.1
in the Authentication/Sign-In Method for authorized domains - Try login again
- Fails on Authenticator: see
scripts/main.js
line 387, of the sample app- Note that although Anonymous authentication was turned on, in
script/main.js
theGoogleAuthProvider
is the one used (see Anonym Authentication)
- Note that although Anonymous authentication was turned on, in
- ⇒ Enable Google Authenticator on the Firebase console for Authentication/Sign-In Method
- Fails due to
- Try Login again: it works 🎊 🎇
- See the Real time DB on the console.
- Check the
users
entries and see the new user added inusers
.
- Check the
Configure DB access rules 📜
- Create a post on the web interface of the sample App
- It does not succeed as the Real Time Database rules do not allow it.
-
⇒ Change the rules (see also the Examples for access), permitting write access to authenticated
users, i.e.:
{ "rules": { ".read": "auth != null", ".write": false, "users": { "$uid": { ".write": "auth != null && auth.uid == $uid" } }, "posts": { ".write": "auth!=null" }, "user-posts": { "$uid": { ".write": "auth != null && auth.uid == $uid" } }, "post-comments": { ".write": "auth != null" } } }
- Re-add the post and 🎊 🎇
- Check the contents.
- Add comments and stars.
Concurrency and updates 💨
- Change values on the and see the reflection on sample web app interface.
- Open the sample web app (http://127.0.0.1:8080) on another browser, a Private page or using Firefox's Containers.
- Login with a different account on the new instance of the sample web app.
- Add posts, comment, like, etc. and see the effects on the other places (1st sample web app and )
Code Highlights 🔎
Authentication 🔓
As mentioned above, the sample web App usesGoogleAuthProvider
in line 387. This is based on the firebaseui-web,
although it applies to other frameworks.You can authenticate with other forms, including:
Data Structure 📐
See the Structure your DB on Firebase's documentation.
Data Access 🚪
Check the function startDatabaseQueries
and note:
- getting the references to an entry on the Database with
ref
; - adding listeners for changes on those entries with
on
:- for
child_added
- for
child_changed
- for
child_removed
- these will make changes on the html's DOM accordingly. See the Listen for Value events.
- for
Note you should use the off()
on the reference as in line 316 to [detach the listeners].
The data read/write works offline, see Work with data offline
Install Firebase tools ⚒
If you want to install the Firebase tools (the cli (Command Line Interface)), follow steps below.
NodeJS is required.
-
Install firebase JS tools:
npm install -g firebase-tools
-
In Windows, it may lead to error in Execution per the MS Policies. To enable the Script to run do:
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
-
In Windows, it may lead to error in Execution per the MS Policies. To enable the Script to run do:
-
Login to firebase, using the link provided and the account used for creating the Project in Firebase.
firebase login
-
For starting the project and setting it to use Firebase, do on the project's root dir:
firebase init
- Follow instructions, namely checking the RT DB support and choosing the correct project (should be listed due to the previous login).
Refs 📚
Última alteração: sexta, 12 de março de 2021 às 15:37