Table of Contents
Understanding Firebase
Setting up Firebase
Creating a Firebase Project
Creating a Web Firebase App
Enabling Authentication Providers
Creating a Firestore Database
User Authentication
Anonymous Authentication
Google Authentication
Monitoring Authentication State
Converting Guests to Permanent Users
Handling Account Merging Errors
Data Visualization and Realtime Data Streams
Data Modeling in NoSQL Databases
Streaming Data to the Visualization
Securing Your Database with Firebase Security Rules
Conclusion
Home Web Front-end CSS Tutorial Firebase Crash Course

Firebase Crash Course

Mar 26, 2025 am 11:24 AM

Firebase Crash Course

This front-end developer's guide unravels the mysteries of Firebase. We'll explore Firebase's capabilities, its benefits, and practical examples. But first, a brief history...

Eight years ago, Andrew Lee and James Tamplin launched Envolve, a real-time chat startup. Its success, fueled by celebrity users like Ricky Martin and Limp Bizkit, stemmed from its ease of use and rapid message delivery. Envolve was a simple chat widget—a script tag added to a page handled everything. It effectively provided a pre-built database and server for chat messages.

As Envolve grew, a surprising trend emerged: developers were using the (often invisible) widget not just for chat, but for diverse purposes—game data, high scores, app settings, and more. They leveraged the widget's real-time capabilities for seamless data synchronization, bypassing the need for complex back-end development.

This epiphany led to the creation of Firebase. The founders envisioned a platform empowering developers to build and scale applications swiftly, eliminating the burden of back-end infrastructure and allowing them to concentrate on the front-end.

Understanding Firebase

Is Firebase just a database? Not entirely. While initially a real-time cloud database, Firebase has evolved into a comprehensive platform encompassing infrastructure for developers and tools for marketers. Currently, it boasts 19 integrated products, each designed to support a specific aspect of application development and provide valuable insights into app performance and user behavior. These products can be used individually or collectively to form a complete back-end solution.

Here's a glimpse into Firebase's diverse offerings:

  • Hosting: Effortless deployment of website updates with every GitHub pull request.
  • Firestore: Real-time database functionality, even offline, without server management.
  • Auth: User authentication and management using various providers.
  • Storage: Secure storage for user-generated content (images, videos, etc.).
  • Cloud Functions: Serverless functions triggered by events (data creation, user signup, etc.).
  • Extensions: Pre-built functions with user interfaces (e.g., Stripe payments, translation services).
  • Google Analytics: Comprehensive user activity tracking and analysis.
  • Remote Config: Dynamic key-value store for feature flags and A/B testing.
  • Performance Monitoring: Detailed performance metrics and custom traces.
  • Cloud Messaging: Cross-platform push notifications.

This is just a fraction of Firebase's capabilities. You don't need to utilize every service; selecting the relevant tools for your project is perfectly acceptable. Let's delve into practical applications.

The following sections will guide you through setting up Firebase and demonstrate its features using embedded examples. This is a high-level overview, not a step-by-step tutorial. For detailed tutorials, leave a comment!

Setting up Firebase

This section is crucial if you plan to integrate the demo app with your own Firebase back end. Skip this if you're familiar with Firebase Projects.

Firebase is cloud-based, requiring initial account setup. However, development can occur offline using local emulators. This guide uses CodePen, necessitating a cloud connection. The process involves creating a Firebase project and retrieving the necessary configuration for front-end integration.

Creating a Firebase Project

Navigate to the Firebase Console. You can skip the Google Analytics setup for now.

Creating a Web Firebase App

Create a new web app and assign it a name. Firebase Projects can contain multiple apps. After creation, you'll receive a configuration object:

let firebaseConfig = {
  apiKey: "your-key",
  authDomain: "your-domain.firebaseapp.com",
  projectId: "your-projectId",
  storageBucket: "your-projectId.appspot.com",
  messagingSenderId: "your-senderId",
  appId: "your-appId",
  measurementId: "your-measurementId"
};
Copy after login

This configuration connects your front-end to Firebase. Including these properties in your front-end code is secure. Security mechanisms will be explained later.

Now, let's represent this app in code. This app acts as a container for shared logic and authentication across Firebase services. We'll use Firebase libraries from a CDN (though module bundlers are also supported).

// This pen adds Firebase via the "Add External Scripts" option in codepen
// https://www.gstatic.com/firebasejs/8.2.10/firebase-app.js
// https://www.gstatic.com/firebasejs/8.2.10/firebase-auth.js

// Create a Project at the Firebase Console
// (console.firebase.google.com)
let firebaseConfig = {
  apiKey: "your-key",
  authDomain: "your-domain.firebaseapp.com",
  projectId: "your-projectId",
  storageBucket: "your-projectId.appspot.com",
  messagingSenderId: "your-senderId",
  appId: "your-appId",
  measurementId: "your-measurementId"
};

// Create your Firebase app
let firebaseApp = firebase.initializeApp(firebaseConfig);
// The auth instance
console.log(firebaseApp.auth());
Copy after login

Next, enable the required Firebase services.

Enabling Authentication Providers

The examples utilize authentication for user sign-in and data security. Initially, authentication providers are disabled for security reasons. Enable Google and Anonymous sign-in methods in the Authentication tab. Remember to add CodePen as an authorized domain for testing purposes (but remove it in production).

Creating a Firestore Database

Create a Firestore database in "test mode." Security will be addressed later.

Now, let's explore real-world use cases.

User Authentication

App functionality often requires user accounts. Let's explore anonymous authentication and Google sign-in.

Anonymous Authentication

Firebase's anonymous authentication allows users to access the app without registration, providing a temporary userId for data association.

(Code example demonstrating anonymous sign-in and profile update omitted for brevity)

Google Authentication

Google sign-in works similarly to anonymous authentication.

(Code example demonstrating Google sign-in omitted for brevity)

Monitoring Authentication State

The onAuthStateChanged method tracks authentication changes, enabling UI updates based on login status.

(Code example demonstrating onAuthStateChanged omitted for brevity)

Converting Guests to Permanent Users

Guest accounts can be upgraded to permanent accounts using linkWithRedirect.

(Code example demonstrating account merging omitted for brevity)

Handling Account Merging Errors

Error handling is essential during account merging.

(Code example demonstrating error handling omitted for brevity)

Data Visualization and Realtime Data Streams

This section focuses on creating a pie chart and syncing it with Firestore data.

(Explanation of conic-gradient and CSS custom properties omitted for brevity)

(Code example demonstrating Firestore data retrieval and pie chart update omitted for brevity)

Data Modeling in NoSQL Databases

Firestore is a NoSQL document database with a hierarchical structure of collections and documents. Data modeling involves structuring data effectively using collections and sub-collections.

(Code examples demonstrating Firestore data retrieval and querying omitted for brevity)

Streaming Data to the Visualization

Firestore's .onSnapshot() method enables real-time data streaming.

(Code example demonstrating real-time data streaming omitted for brevity)

Securing Your Database with Firebase Security Rules

Security Rules control data access in Firebase. They are evaluated on the server for each request.

(Explanation of Security Rules and examples omitted for brevity)

Conclusion

This guide has covered user authentication, data modeling, real-time data synchronization, and database security using Firebase. Remember to explore additional Firebase resources for further learning. Firebase simplifies back-end management, allowing developers to focus on the front-end.

The above is the detailed content of Firebase Crash Course. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1267
29
C# Tutorial
1239
24
Google Fonts   Variable Fonts Google Fonts Variable Fonts Apr 09, 2025 am 10:42 AM

I see Google Fonts rolled out a new design (Tweet). Compared to the last big redesign, this feels much more iterative. I can barely tell the difference

How to Create an Animated Countdown Timer With HTML, CSS and JavaScript How to Create an Animated Countdown Timer With HTML, CSS and JavaScript Apr 11, 2025 am 11:29 AM

Have you ever needed a countdown timer on a project? For something like that, it might be natural to reach for a plugin, but it’s actually a lot more

HTML Data Attributes Guide HTML Data Attributes Guide Apr 11, 2025 am 11:50 AM

Everything you ever wanted to know about data attributes in HTML, CSS, and JavaScript.

A Proof of Concept for Making Sass Faster A Proof of Concept for Making Sass Faster Apr 16, 2025 am 10:38 AM

At the start of a new project, Sass compilation happens in the blink of an eye. This feels great, especially when it’s paired with Browsersync, which reloads

How We Created a Static Site That Generates Tartan Patterns in SVG How We Created a Static Site That Generates Tartan Patterns in SVG Apr 09, 2025 am 11:29 AM

Tartan is a patterned cloth that’s typically associated with Scotland, particularly their fashionable kilts. On tartanify.com, we gathered over 5,000 tartan

How to Build Vue Components in a WordPress Theme How to Build Vue Components in a WordPress Theme Apr 11, 2025 am 11:03 AM

The inline-template directive allows us to build rich Vue components as a progressive enhancement over existing WordPress markup.

While You Weren't Looking, CSS Gradients Got Better While You Weren't Looking, CSS Gradients Got Better Apr 11, 2025 am 09:16 AM

One thing that caught my eye on the list of features for Lea Verou's conic-gradient() polyfill was the last item:

A Comparison of Static Form Providers A Comparison of Static Form Providers Apr 16, 2025 am 11:20 AM

Let’s attempt to coin a term here: "Static Form Provider." You bring your HTML

See all articles