How to authenticate with google using firebase in React ?
Last Updated :
25 Jul, 2024
The following approach covers how to authenticate with Google using firebase in react. We have used firebase module to achieve so.
Creating React Application And Installing Module:
Step 1: Create a React myapp using the following command:
npx create-react-app myapp
Step 2: After creating your project folder i.e. myapp, move to it using the following command:
cd myapp
Project structure: Our project structure will look like this.

Step 3: After creating the ReactJS application, Install the firebase module using the following command:
npm install [email protected] --save
Step 4: Go to your firebase dashboard and create a new project and copy your credentials.
const firebaseConfig = {
apiKey: "your api key",
authDomain: "your credentials",
projectId: "your credentials",
storageBucket: "your credentials",
messagingSenderId: "your credentials",
appId: "your credentials"
};
Step 5: Initialize the Firebase into your project by creating Firebase.js file with the following code.
Firebase.js
import firebase from 'firebase';
const firebaseConfig = {
// Your credentials
};
firebase.initializeApp(firebaseConfig);
var auth = firebase.auth();
var provider = new firebase.auth.GoogleAuthProvider();
export {auth , provider};
Step 6: Go to your firebase dashboard and Enable the google sign-in method as shown below.

Step 7: Now install the npm package i.e. react-firebase-hooks using the following command.
npm i react-firebase-hooks
This package helps us to listen to the current state of the user.
Step 8: Create two files i.e. login.js and main.js with the following code.
login.js
import React from 'react';
import {auth , provider} from './firebase.js';
const Login = () => {
// Sign in with google
const signin = () => {
auth.signInWithPopup(provider).catch(alert);
}
return (
<div>
<center>
<button style={{"marginTop" : "200px"}}
onClick={signin}>Sign In with Google</button>
</center>
</div>
);
}
export default Login;
Main.js
import React from 'react';
import {auth} from './firebase';
const Mainpage = () => {
// Signout function
const logout = () => {
auth.signOut();
}
return (
<div>
Welcome
{
auth.currentUser.email
}
<button style={{"marginLeft" : "20px"}}
onClick={logout}>
Logout
</button>
</div>
);
}
export default Mainpage;
Step 8: Finally Import all required files in App.js file as shown below.
App.js
JavaScript
import React from 'react';
import {auth} from './firebase';
import {useAuthState} from 'react-firebase-hooks/auth';
import Login from './login';
import Mainpage from './main';
function App() {
const [user] = useAuthState(auth);
return (
user ? <Mainpage/> : <Login/>
);
}
export default App;
Step to Run Application: Run the application using the following command from the root directory of the project:
npm start
Output: Now open your browser and go to http://localhost:3000/, you will see the following output:
Email Authentication using React & Firebase
Similar Reads
How to authenticate firebase with GitHub in ReactJS ? The following approach covers how to authenticate firebase with GitHub in react. We have used the firebase module to achieve so.Creating React Application And Installing Module:Step 1: Create a React app using the following command:npx create-react-app gfgappStep 2: After creating your project folde
2 min read
Google SignIn using Firebase Authentication in ReactJS Firebase simplifies mobile and web app development by offering pre-built features like user authentication (email/password, Google Sign-In, etc.) without the need to build complex backends. This saves time and resources for developers.In this article, we will discuss about the Google Sign-In feature
5 min read
How to authenticate phone number using firebase in ReactJS ? User authentication is a critical aspect of many web applications, and phone number authentication has become increasingly popular for its convenience and security. Firebase, a powerful platform developed by Google, offers various authentication methods, including phone number authentication. Prereq
3 min read
How to get current date and time in firebase using ReactJS ? This article provides a detailed walkthrough on obtaining the current date and time in a Firebase environment using ReactJS. Readers will gain insights into the implementation process, enabling date and time functionalities into their React projects powered by Firebase.Prerequisites:Node JS or NPMRe
2 min read
How to Build a React App with User Authentication? To implement user authentication in a React app, you can use Auth0, a popular authentication service that simplifies the process of handling login, logout, and user profile management. With Auth0, you can avoid managing authentication from scratch, which saves time and ensures security. In this arti
3 min read
How to Use Google Fonts in React? Adding the Google fonts in React app enhances the typography. It provides a wide range of font styles that are easy to integrate and use in the react components.Google Fonts is a widely used popular library of open-source web fonts that provides a vast collection of typefaces to enhance the visual a
5 min read