💻Samir's late night thoughts

Unlocking the Power of React and Firebase: A Guide to Secure Authentication

Secure Authentication with React and Firebase: A Comprehensive Guide

React and Firebase are two powerful technologies that have revolutionized web development. React, an open-source JavaScript library, is used to build interactive user interfaces, while Firebase, a mobile and web application development platform, provides a variety of services such as authentication, real-time database, and cloud functions. Together, they offer developers a comprehensive toolkit to build dynamic and secure web applications.

In this guide, we will explore the power of React and Firebase by focusing on one of its most critical components: secure authentication. Authentication is the process of verifying the identity of a user or a device to grant access to a system. It is a fundamental requirement for any web application that deals with sensitive user data.

In this blog, we will cover the basics of authentication and how it works with React and Firebase. We will also discuss the different types of authentication methods available and provide a step-by-step guide on how to implement secure authentication in your React-Firebase application. Whether you are a beginner or an experienced developer, this guide will help you unlock the power of React and Firebase and take your web application to the next level.

Intro : React Firebase Hook Library

React Firebase Hook is an open-source npm library that simplifies the process of integrating Firebase services into your React application. It provides a set of hooks that allow you to easily access Firebase authentication, database, and storage functionalities from your components. With React Firebase Hook, you can write cleaner, more organized code and speed up your development process. Whether you are a beginner or an experienced developer, this library can save you time and effort when working with Firebase and React.

Step 1: Install Firebase Hook Library and Setup Firebase Configuration

npm i react-firebase-hooks
import { initializeApp } from 'firebase/app';
import { getAuth } from 'firebase/auth';

const firebaseConfig = {
  apiKey: process.env.REACT_APP_FIREBASE_API_KEY,
  authDomain: process.env.REACT_APP_FIREBASE_AUTHDOMAIN,
  projectId: process.env.REACT_APP_FIREBASE_PROJECTID,
  storageBucket: process.env.REACT_APP_FIREBASE_STORAGEBUCKET,
  messagingSenderId: process.env.REACT_APP_FIREBASE_MESSAGINGSENDERID,
  appId: process.env.REACT_APP_FIREBASE_APPID,
  measurementId: process.env.REACT_APP_FIREBASE_MEASUREMENTID,
};

const app = initializeApp(firebaseConfig);

export const auth = getAuth(app);

Step 2: Add these hooks in login component

import React, { useState, useEffect } from 'react';
import { auth } from '../../features/config/firebase-config';
import { useSignInWithEmailAndPassword } from 'react-firebase-hooks/auth';

function Login() {
  const [signInWithEmailAndPassword, user, loading, error] = useSignInWithEmailAndPassword(auth);
  const handleLogin = () =>{
     signInWithEmailAndPassword(email, password);
  }

  return (
    <>
     <YourLoginFormComponent>
        {Your Login Form Codes}
        <Button onClick={handleLogin}>Login</Button>
    </YourLoginFormComponent>
  );
}

export default Login;

Step 3: Protect your routes by checking if user is signed in or not

import { Outlet } from 'react-router-dom';
import Login from '../pages/Auth/login';
import { useIdToken } from 'react-firebase-hooks/auth';
import { auth } from '../features/config/firebase-config';

const ProtectedRoutes = () => {
  const [user, loading] = useIdToken(auth);

  if (loading) return <YourLoadingComponent />;
  return user ? <Outlet /> : <Login />;
};

export default ProtectedRoutes;

For other use cases connect these hooks to their functional components

import React, { useState, useEffect } from 'react';
import { auth } from '../../features/config/firebase-config';
import { useSendPasswordResetEmail,useCreateUserWithEmailAndPassword } from 'react-firebase-hooks/auth';

function FirebaseFunctional() {
  const [sendPasswordResetEmail, sending, error] = useSendPasswordResetEmail(auth);
  const [createUserWithEmailAndPassword,user] = useCreateUserWithEmailAndPassword(auth);

  const handleResetPassword = () =>{
    sendPasswordResetEmail(email);
  }
  const handleCreate = () =>{
    useCreateUserWithEmailAndPassword(email,password);
  }

  return (
    <>
     <YourResetPasswordComponent>
        {Your Reset Form Codes}
        <Button onClick={handleResetPassword}>Reset</Button>
    </YourResetPasswordComponent>
     <YourRegisterComponent>
        {Your Registration Form Codes}
        <Button onClick={handleCreate}>Register</Button>
    </YourRegisterComponent>
  );
}

export default FirebaseFunctional;