/firebase-tutorials

How to use Firebase auth with React?

Learn how to integrate Firebase Auth into your React app with a step-by-step guide—from project setup to coding a custom authentication component.

Matt Graham, CEO of Rapid Developers

Book a call with an Expert

Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.

Book a free No-Code consultation

How to use Firebase auth with React?

 

Step 1: Set Up a New React App

 

To start using Firebase Authentication in your React app, you'll first need to set up a new React application. You can use Create React App to set this up:


npx create-react-app firebase-auth-demo
cd firebase-auth-demo

 

Step 2: Install Firebase

 

Next, you'll need to install Firebase in your React app. This will allow you to use Firebase Authentication and other Firebase services:


npm install firebase

 

Step 3: Set Up a Firebase Project

 

  1. Go to the Firebase Console.
  2. Click "Add project" and follow the setup instructions to create a new Firebase project.
  3. Once the project is created, navigate to the project dashboard.

 

Step 4: Configure Firebase in Your React App

 

  1. In the Firebase Console, go to Project Settings.
  2. Under the "Your apps" section, click on the web icon </> to register a new web app.
  3. Follow the setup instructions to get your Firebase configuration.

Once you have the configuration, create a firebase-config.js file in your React app's src directory and add the following code:


// src/firebase-config.js
import { initializeApp } from "firebase/app";
import { getAuth } from "firebase/auth";

const firebaseConfig = {
  apiKey: "YOUR_API_KEY",
  authDomain: "YOUR_AUTH_DOMAIN",
  projectId: "YOUR_PROJECT_ID",
  storageBucket: "YOUR_STORAGE_BUCKET",
  messagingSenderId: "YOUR_MESSAGING_SENDER\_ID",
  appId: "YOUR_APP_ID"
};

const app = initializeApp(firebaseConfig);
export const auth = getAuth(app);

 

Step 5: Create an Authentication Component

 

Create a new component called Auth.jsx to handle user authentication:


// src/Auth.jsx
import React, { useState } from "react";
import { auth } from "./firebase-config";
import { 
  createUserWithEmailAndPassword, 
  signInWithEmailAndPassword, 
  signOut 
} from "firebase/auth";

function Auth() {
  const [registerEmail, setRegisterEmail] = useState("");
  const [registerPassword, setRegisterPassword] = useState("");
  const [loginEmail, setLoginEmail] = useState("");
  const [loginPassword, setLoginPassword] = useState("");
  const [user, setUser] = useState(null);

  const register = async () => {
    try {
      const userCredential = await createUserWithEmailAndPassword(
        auth,
        registerEmail,
        registerPassword
      );
      setUser(userCredential.user);
    } catch (error) {
      console.error(error.message);
    }
  };

  const login = async () => {
    try {
      const userCredential = await signInWithEmailAndPassword(
        auth,
        loginEmail,
        loginPassword
      );
      setUser(userCredential.user);
    } catch (error) {
      console.error(error.message);
    }
  };

  const logout = async () => {
    await signOut(auth);
    setUser(null);
  };

  return (
    

Register

setRegisterEmail(event.target.value)} /> setRegisterPassword(event.target.value)} />

Login

setLoginEmail(event.target.value)} /> setLoginPassword(event.target.value)} />

User Logged In:

{user?.email}
); } export default Auth;

 

Step 6: Use the Authentication Component

 

Finally, use the Auth component in your App.js file:


// src/App.js
import React from "react";
import "./App.css";
import Auth from "./Auth";

function App() {
  return (
    

Firebase Auth with React

); } export default App;

 

Now you have a working Firebase Authentication setup in your React app! You can expand upon this example by adding additional authentication providers, setting up password resets, or securing routes in your application.

Want to explore opportunities to work with us?

Connect with our team to unlock the full potential of no-code solutions with a no-commitment consultation!

Book a Free Consultation

Client trust and success are our top priorities

When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.

Rapid Dev was an exceptional project management organization and the best development collaborators I've had the pleasure of working with. They do complex work on extremely fast timelines and effectively manage the testing and pre-launch process to deliver the best possible product. I'm extremely impressed with their execution ability.

CPO, Praction - Arkady Sokolov

May 2, 2023

Working with Matt was comparable to having another co-founder on the team, but without the commitment or cost. He has a strategic mindset and willing to change the scope of the project in real time based on the needs of the client. A true strategic thought partner!

Co-Founder, Arc - Donald Muir

Dec 27, 2022

Rapid Dev are 10/10, excellent communicators - the best I've ever encountered in the tech dev space. They always go the extra mile, they genuinely care, they respond quickly, they're flexible, adaptable and their enthusiasm is amazing.

Co-CEO, Grantify - Mat Westergreen-Thorne

Oct 15, 2022

Rapid Dev is an excellent developer for no-code and low-code solutions.
We’ve had great success since launching the platform in November 2023. In a few months, we’ve gained over 1,000 new active users. We’ve also secured several dozen bookings on the platform and seen about 70% new user month-over-month growth since the launch.

Co-Founder, Church Real Estate Marketplace - Emmanuel Brown

May 1, 2024 

Matt’s dedication to executing our vision and his commitment to the project deadline were impressive. 
This was such a specific project, and Matt really delivered. We worked with a really fast turnaround, and he always delivered. The site was a perfect prop for us!

Production Manager, Media Production Company - Samantha Fekete

Sep 23, 2022