DEV Community

Ninad Vyas
Ninad Vyas

Posted on • Updated on

Firebase Google Auth & Firestore In React JS 🚀

Before getting started, make sure you have a basic understanding of React and JavaScript. Additionally, ensure that you have Node.js and npm (Node Package Manager) installed on your machine.

Step 1:
Set up a Firebase Project
To begin, head over to the Firebase website (firebase.google.com) and create a new project. This will provide you with a unique project ID and the necessary credentials to connect your React application to Firebase.

Step 2:
Install Firebase in Your React Application
In your React project's root folder, open the terminal and run the following command to install the Firebase SDK:

npm install firebase
Enter fullscreen mode Exit fullscreen mode

Step 3:
Configure Firebase in Your React Application
Create a new file called firebase.js in your React project's source folder. Inside this file, import the Firebase SDK and the required Firebase services using the following lines of code:

import firebase from 'firebase/app';
import 'firebase/auth';
Enter fullscreen mode Exit fullscreen mode

Step 4:
Initialize Firebase
In the firebase.js file, initialize Firebase by calling the initializeApp method and passing it your Firebase project's configuration object. This object can be found in your Firebase project settings and typically includes properties such as apiKey, authDomain, projectId, etc.

Step 5:
Enable Google Authentication
In the firebase.js file, create a new instance of the Google authentication provider using the following code:

const googleProvider = new firebase.auth.GoogleAuthProvider();

Enter fullscreen mode Exit fullscreen mode

This provider allows users to sign in with their Google accounts.

Step 6:
Implement User Authentication in Your React Components
Inside your React components, import the firebase object from firebase.js. You can then utilize the authentication functionality provided by Firebase.

To enable user registration, create a registration form where users can enter their email and password. Use the createUserWithEmailAndPassword method provided by Firebase to register the user.

To allow users to log in, create a login form where they can enter their credentials. Use the signInWithEmailAndPassword method to authenticate the user.

To provide Google login, add a button or link that triggers the signInWithPopup method, passing it the googleProvider instance.

Step 7:
Protect Routes and Actions Based on Authentication
Utilize Firebase Authentication to protect specific routes in your blog application.
Firebase provides methods to verify the user's authentication status, which can be used to control access to certain parts of your application.


By following these steps, you can successfully integrate Firebase Authentication with Google into your React application. Firebase Authentication provides a secure and streamlined approach to handle user registration, login, and route protection.


Firestore React JS

  1. Set up a Firebase project: Just like with Firebase Authentication, you need to create a Firebase project on the Firebase website (firebase.google.com). This will give you the necessary credentials to connect your React application to Firestore.

  2. Install Firebase in your React application: In your React project's root folder, open the terminal and run the command npm install firebase. This will install the Firebase SDK in your project.

  3. Configure Firebase in your React application: Create a new file called firebase.js (or any other name you prefer) in your React project's source folder. Import the Firebase SDK at the top of the file using import firebase from 'firebase/app', and then import the Firestore service using import 'firebase/firestore'.

  4. Initialize Firebase: In the firebase.js file, initialize Firebase by calling the initializeApp method and passing it your Firebase project's configuration object, just like we did for Firebase Authentication.

  5. Access Firestore in your React components: Import the firebase object from firebase.js in the components where you want to use Firestore. Use the firebase.firestore() method to access the Firestore functionality.

  6. Interact with Firestore:

  • To read data from Firestore, you can use the get method on a specific collection or document reference. This will return a promise that you can handle using .then() and .catch() or with async/await.

  • To write data to Firestore, you can use the set method on a document reference to create a new document or update an existing one. Alternatively, you can use the add method on a collection reference to add a new document with an automatically generated ID.

  • To listen to real-time updates, you can use the onSnapshot method on a collection or document reference. This allows you to receive updates whenever the data in Firestore changes.

Remember to handle errors and handle asynchronous operations appropriately when working with Firestore. Additionally, Firestore provides powerful querying capabilities that you can explore to fetch specific data based on certain criteria.

Top comments (0)