Google Firebase Database for a Website

Overview

Scenario

In this scenario, you were recently hired as a new intern. The first project is to create a website and database that allows you to create a list of projects that you are working on. Your boss wants you to deploy it on Google Firebase hosting with a Firebase database.

Business Requirements

Project Requirements

Table of Contents

Instructions

Watch the video instead Firebase Database Setup Video

Step 1: Install Visual Studio Code and Node.js

Hopefully, you already have Visual Studio Code and Node.js installed on your computer. If not, ask ChatGPT how to install them and follow the instructions from ChatGPT.

Step 2: Open Your Existing Project in Firebase

  1. Sign into the Google Firebase console using your BYU-Idaho email address (the email address with numbers, for example: bos24011@byui.edu).
    • Trouble signing in? Open my.byui.edu, click “Profile”, scroll down to “Campus Email”, and use that email for the Google Firebase sign in

      my.byui.edu View Profile option my.byui.edu Profile Email
    • If that doesn’t work, use a personal Gmail account.
  2. Click on YourName Portfolio/Project

    Choose Firebase Project Image
  3. Click Firestore Database under Build (you may need to expand the Build section by clicking on it).

    Firebase.google.com Choose Firestore Database
  4. Click Create Database

    Firebase.google.com Create Database
  5. Select nam5 for the location of the United States (This should be selected by default)

    Firebase.google.com Choose nam5
  6. Click Next

    firebase.google.com Firestore Next
  7. Leave the defaults Start in production mode and then click Create

    firebase.google.com Start in Production Mode

Step 3: Create a Collection of Projects

To create your database for your website, you would design your database based on collections of what you will store. Some examples of collections might be users, projects, movies, etc. This was the video about it from the preparation material: https://www.youtube.com/embed/jm66TSlVtcc?start=1&rel=0

  1. If you don’t see the screen below, click🗘 on your browser. Click Start Collection

    firebase.google.com Create Collection
  2. Put the Collection ID as projects and click Next

    firebase.google.com Name Collection
  3. Click Auto-ID

    firebase.google.com Auto ID

    Clicking Auto-ID will auto generate a Document ID like this image below.

    firebase.google.com Auto ID Example
  4. Let’s add some fields. Type title for the field and leave the type as a string if that is the data type that you want.

    • FYI - Use string for text such as letters, words, and paragraphs.
    • FYI - Use number for digits such as 1, 23, 5555, 44.44, etc.
    • FYI - Use boolean for true or false
    • We will just use strings for now
    firebase.google.com Collection title Field
  5. Click Add Field

    firebase.google.com Collection Add Field
  6. Type description and select string

    firebase.google.com Collection description Field
  7. Make sure to click Save

    firebase.google.com Collection Save

    You should only see one item in your list for now. You will add more items through the website later.

    firebase.google.com Collection Example

Step 4: Add an app or website connection to the database

  1. Go to the Project Overview settings by clicking on the gear in the top left corner:

    firebase.google.com Firebase Project Overview
  2. Select Project settings

    firebase.google.com Firebase Project Settings
  3. Click Add App

    firebase.google.com Firebase Project Add App
  4. Select the icon for the HTML or Website web app (we need this to connect the html website code with our database):

    firebase.google.com Firebase Project HTML
  5. Put the name of your database, like portfoliodb (db is for database)

    firebase.google.com Firebase Project Database Name
  6. Click Register app

    firebase.google.com Firebase Project Register app
  7. Select Use a <script> tag - IN PRODUCTION, it would be better to use npm

    firebase.google.com Firebase Project Use Script Tag
  8. Click Continue to console. We will get the code later.

    firebase.google.com Firebase Project Continue to Console

Step 5: Link the database to the hosting

  1. Go to the Project Overview settings by clicking on the gear in the top left corner

    firebase.google.com Firebase Project Overview
  2. Select Project settings

    firebase.google.com Firebase Project Settings
  3. Under the General tab, scroll down to the bottom and click on Link to a Firebase Hosting site

    firebase.google.com Firebase Link to Site
  4. Select your site from the drop down and click Link

    firebase.google.com Link Your Site
  5. Copy the configuration by selecting the Config (option #2) in the image below

    firebase.google.com Configuration Options
  6. Click Copy to Clipboard (option #3) in the image above. We are going to copy that code into the config.js file that we will obtain in the next step.

Step 6: Put the database code in your website file (projects.html)

  1. In Visual Studio Code, open the project we have been working on - it might show up under Recent and find the public folder from last week.

    • Alternatively, you could follow the steps again from last week and open a New Visual Studio window with a new folder and make a new Firebase Project on Google.

    VS Code Project Selection

    ChatGPT generated some of these files. We are going to just download them and put them in your project to save time and possible variations from ChatGPT. If you would like to figure it out on your own, that would be great.

  2. Open Visual Studio Code and download this file for your operating system:

    • Copy the file into your project folder (make sure it is at the highest level in your project folder) and run it in the Visual Studio Code terminal

    • (Windows) Run it with: ./week5.bat

    • (Mac/Apple) Run the following command in VS code: chmod 755 ./week5.sh

    • (Mac/Apple) Run it with: ./week5.sh

    • week5.sh contains the following commands. If executing week5.bat/week5.sh on your computer didn’t work, you may need to execute the following commands:

      curl -O https://byui-cloud.github.io/itm101-course/week05/firestore.rules
      cd public
      curl -O https://byui-cloud.github.io/itm101-course/week05/config.js
      curl -O https://byui-cloud.github.io/itm101-course/week05/projects.html
      mkdir styles
      cd styles
      curl -O https://byui-cloud.github.io/itm101-course/week05/styles.css
      cd ..
      cd ..
                      
  3. Replace the contents of the file called config.js in your public folder and paste the code we obtained above that contains your API key and configuration for your Firestore database. It should look like this but with your URL and key:

    VS Code Firebase Config
    • In Visual Studio, double click the config.js file we just made in the public folder delete all of the contents of the file (or press CTRL + A to select all and then backspace/delete)

    • Then, paste the code from above into the file or click Edit > Paste in the menu (or press CTRL + V)

    • Save the file with File > Save in the menu (or press CTRL + S)

    • This HTML code is to provide the text and structure for the web page

Step 7: Check your firebase database security rules

  1. To add new projects/data to your database, the rules need to allow write or save for non-authenticated users (next week we will need authenticated). To fix it, go to the database by clicking on Firestore Database and then click on Rules

    firebase.google.com Firestore DB Rules
    • Remove the code that is there and paste this code:

      rules_version = '2';
      service cloud.firestore {
          match /databases/{database}/documents {
              match /projects/{document=**} {
                  allow read, write: if request.auth == null;
              }
          }
      }
                      
    firebase.google.com Firestore DB Config
  2. Paste your code and a Publish button should appear. Click Publish.

    firebase.google.com Firestore DB Config  Publish

    If it still doesn’t work, wait a few minutes and do a SHIFT + 🗘 by pressing SHIFT on your keyboard and click the refresh button in your browser while holding it.

Step 8: Run `firebase deploy` to save changes

  1. In your Visual Studio Code terminal, type firebase deploy (#1 in the image below) and hit enter each time as needed to save your changes in your code and push it up to Google Firebase Hosting.

    VS Code Firebase Deploy

    It should provide you with the URL for your site in the terminal after you run firebase deploy - see the Hosting URL in the red circle above and copy that URL and put it in your browser tab and go to it

  2. Make sure to add the name of your file (ex: projects.html) to the end of the URL like this: https://yourportfolioname.web.app/projects.html

    Firebase Website Link Example
  3. Refresh the URL in your web browser by clicking the refresh button. Hold the Shift key on your keyboard while you are clicking the refresh button to clear your cache.

    Firebase Refresh Cache

    It should look something like this below, but it could also look very different based on the code generated by ChatGPT. You should be able to add projects to the list and they should pop up at the bottom of the web page.

    Projects Page Example
  4. Add a project Name and a Project Description to test the functionality and click Add Project and you should see a popup like this below:

    Projects Page Popup

    If the project is added and appears below the Project’s title, you did it!

    Projects Page Project Added Example

Congratulations! You connected a Firebase Hosting with a Firebase Database!

Troubleshooting & Common Problems

Permission Errors or firebase deploy not working:

(Windows) Change the execution policy

If new projects don’t show up, it could be that your database is not connected.

If new projects don’t show up, it could be that your database permissions are not working.

Permission Errors or firebase installation not working:

If firebase doesn’t deploy

The website doesn’t load the styles, but only the words appear

If you cannot get node to finish working

If you cannot get firebase to finish setting up and installing

Learning More - Expand Your Database

Add more details to your database or make your site look better.

Add more fields/documents, etc.

Add a database to your portfolio page you made the previous week.

Learning More - MySQL Databases

ITM 111 Introduction to Databases and ITM220 SQL teach how to use SQL databases that many companies use