Open In App

How to authenticate firebase with GitHub in ReactJS ?

Last Updated : 08 Apr, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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 gfgapp

Step 2: After creating your project folder i.e. gfgapp, move to it using the following command:

cd gfgapp

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.

Firebase.js




import firebase from 'firebase';
  
const firebaseConfig = {
    // Your Credentials
  };
  
firebase.initializeApp(firebaseConfig);
  
var auth = firebase.auth();
var provider = new firebase.auth.GithubAuthProvider();
export {auth , provider};


Step 6: Register your app as a developer application on GitHub and get your app’s OAuth 2.0 Client ID and Client Secret.

For Authorization callback URL go to your authentication section and click on Github sign-in method. After that copy the callback URL.

Step 7: Now Enable the Github sign-in method by entering your Client ID and Client Secret. 

Step 8: 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 9: Create two files i.e. login.js and main.js with the following code.

login.js




import {auth , provider} from './firebase';
const Login = ()=>{
  
    // SignIn with GitHub
    const submit = ()=>{
        auth.signInWithPopup(provider).catch(alert);
    }
    return (
        <div>
            <center>
            <button onClick={submit}>
                SignIn with Github
            </button>
            </center>
        </div>
    )
}
export default Login;


main.js




import {auth} from './firebase';
const Main = ()=>{
    const logout = ()=>{
        auth.signOut();
    }
    return(
        <div>
            Welcome 
                 
            {
                auth.currentUser.email
            }
                 
            <button onClick={logout}>
                Logout
            </button>  
        </div>
    )
}
export default Main;


Step 10: Finally Import all required files in App.js file as shown below.

App.js




import './App.css';
import Login from './login';
import {auth} from './firebase';
import Main from './main';
import {useAuthState} from 'react-firebase-hooks/auth';
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:



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads