Create the Main JavaScript File

Create an app.js file to handle Firebase Authentication and Realtime Database interactions:

// app.js
import { initializeApp } from "firebase/app";
import { getAuth, createUserWithEmailAndPassword, signInWithEmailAndPassword } from "firebase/auth";
import { getDatabase, ref, set, push, onValue } from "firebase/database";
import firebaseConfig from './firebase-config.js';

// Initialize Firebase
const app = initializeApp(firebaseConfig);
const auth = getAuth(app);
const database = getDatabase(app);

// Authentication functions
function signUp() {
const email = document.getElementById('email').value;
const password = document.getElementById('password').value;
createUserWithEmailAndPassword(auth, email, password)
.then((userCredential) => {
const user = userCredential.user;
console.log('Sign Up Successful:', user);
})
.catch((error) => {
console.error('Error signing up:', error.message);
});
}

function logIn() {
const email = document.getElementById('email').value;
const password = document.getElementById('password').value;
signInWithEmailAndPassword(auth, email, password)
.then((userCredential) => {
const user = userCredential.user;
console.log('Log In Successful:', user);
})
.catch((error) => {
console.error('Error logging in:', error.message);
});
}

// Realtime Database functions
function sendMessage() {
const message = document.getElementById('message').value;
const messagesRef = ref(database, 'messages/');
const newMessageRef = push(messagesRef);
set(newMessageRef, {
text: message
});
document.getElementById('message').value = '';
}

function displayMessages() {
const messagesRef = ref(database, 'messages/');
onValue(messagesRef, (snapshot) => {
const messages = snapshot.val();
const messagesDiv = document.getElementById('messages');
messagesDiv.innerHTML = '';
for (const key in messages) {
const message = messages[key].text;
const messageElement = document.createElement('p');
messageElement.textContent = message;
messagesDiv.appendChild(messageElement);
}
});
}

// Initialize display messages function
displayMessages();

// Expose functions to the global scope
window.signUp = signUp;
window.logIn = logIn;
window.sendMessage = sendMessage;

Explanation:

In the above code, The JavaScript file is used to interact with Firebase services, specifically Firebase Authentication and Firebase Realtime Database. It initializes Firebase using the provided configuration, sets up authentication functions for signing up and logging in users, and defines functions for sending and displaying messages in the real-time database. The functions are exposed to the global scope so they can be called from the HTML file.

Adding Firebase to Your JavaScript Project

Firebase is a powerful platform that offers a range of services to enhance our web application. From authentication to real-time database storage, Firebase provides everything you need to create a dynamic and engaging user experience.

In this guide, we will go through the process of setting up Firebase in a JavaScript project and integrating Firebase Authentication and Realtime Database into a simple web application.

Similar Reads

Introduction to Firebase

Firebase offers a suite of services that can be integrated into our application such as:...

Step 1: Set Up Your Firebase Project

Create a Firebase Project...

Step 2: Set Up Your JavaScript Project

Let’s Create a new directory for our project and follow it. Initialize a new Node.js project (if we are using Node.js) and install Firebase:...

Add Firebase SDK to Your Project

Add the Firebase SDK to our project by including the provided Firebase configuration snippet in a firebase-config.js file:...

Create the Main JavaScript File

Create an app.js file to handle Firebase Authentication and Realtime Database interactions:...

Running the Project

To view your project, you can use a local server. If you have Node.js installed, you can use a simple HTTP server like http-server:...

Step 3: Firebase Authentication

Firebase Authentication provides an easy way to handle user authentication with email/password, social media accounts, and more. In this step, we will focus on email/password authentication....

Step 4: Firebase Realtime Database

Firebase Realtime Database allows you to store and sync data in real-time. Let’s add functionality to send and display messages....

Conclusion

Overall, Firebase services, including Authentication, Firestore, Realtime Database, Cloud Storage, Cloud Functions and Hosting provide developers with a comprehensive toolkit for building modern web applications. By following the steps outlined in this guide, you can easily set up Firebase in your JavaScript project and using its features to create a seamless and interactive user experience. Whether you are looking to add user authentication or real-time data syncing, Firebase has you covered with its easy-to-use APIs and powerful functionality....