Open In App
Related Articles

Anonymous authentication in firebase using ReactJS

Improve Article
Save Article
Like Article

The following approach covers how to authenticate anonymously using firebase in react. This type of authentication is used to create and use temporary anonymous accounts to authenticate with Firebase. We have used the 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 firebase@8.3.1 --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.


import firebase from 'firebase';
const firebaseConfig = {
    // Your Credentials
var auth = firebase.auth();
export default auth;

Step 6: Go to your firebase dashboard and Enable the Anonymous 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.


import React from 'react';
import auth from './firebase.js';
const Login = () => {
    // Sign in Anonymously
    const signin = () => {
    return (
                <button style={{"marginTop" : "200px"}}
                onClick={signin}>Sign In Anonymously</button>
export default Login;


import React from 'react';
import auth from './firebase';
const Main = () => {
    // Signout function
    const logout = () => {
    return (
        <div style={{"marginTop" : "200px"}}>
            Anonymous Login Success
            <button style={{"marginLeft" : "20px"}}
export default Main;

Step 8: Finally import all required files in App.js file as shown below.


import React from 'react';
import auth from './firebase';
import {useAuthState} from 'react-firebase-hooks/auth';
import Login from './login';
import Main from './main';
function App() {
  const [user] = useAuthState(auth);
  return (
    user ? <Main/> : <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:

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 31 May, 2021
Like Article
Save Article
Similar Reads
Complete Tutorials