How to create Google login for your website by using Firebase Authentication

In this tutorial we will create a Google Sign-In button for your website. Here we will create live demo example for login with google and in this demo we will use Google APIs and Firebase Authentication to allow users to sign in to your app.

Now let’s see step by step integration Google Sign-In into your web app.

Step 1: Go to Firebase console and create new project.

Step 2: Set the project name

Step 3: Click on continue button

Step 4: Choose your Google Analytics Account and click on Create Project button.

Step 5: Now create Firebase App.

Step 6: Add Firebase to your web app.

Step 7: Copy below code which will be helpful for further coding.

Step 8: Create one file google-login.html and you can write below code.

<!DOCTYPE html>
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<script src="" ></script>
	<!-- Bootstrap theme -->
	<link rel="stylesheet" href="">
	<script src=""></script>
	<!-- Add icon library -->
	<link rel="stylesheet" href="">
	<title>How to create Firebase Google Login?</title>

<div class="container">	
  <div class="row">
  	 <div class="col-sm-4">
		<button type="button" id="google-login" name="google-login" class="btn btn-danger btn-lg btn-block"><i class="fa fa-google "></i> Login with Google</button>
	</div><!-- end col -->	
  </div><!--  end row -->		
<center>Developed by <a href="">Shinerweb</a></center>
	<script type="module">
	  // Import the functions you need from the SDKs you need
	  import { initializeApp } from "";
	  import { getAnalytics } from "";
	  import { getAuth, GoogleAuthProvider, signInWithPopup } from "";
	  // TODO: Add SDKs for Firebase products that you want to use

	  // Your web app's Firebase configuration
	  // For Firebase JS SDK v7.20.0 and later, measurementId is optional
	  const firebaseConfig = {
	  apiKey: "AIzaSyB4Z71YW8OGtcsh-tiazaqYC7BS05pTxi4",
	  authDomain: "",
	  projectId: "shinerweb-auth",
	  storageBucket: "",
	  messagingSenderId: "487776642853",
	  appId: "1:487776642853:web:25b4821e77c6de7e90dccc",
	  measurementId: "G-00FGRRF868"

	  // Initialize Firebase
	  const app = initializeApp(firebaseConfig);
	  const analytics = getAnalytics(app);
	  const auth = getAuth();

	  const provider = new GoogleAuthProvider();
	  //----- Google login code start	  
	  document.getElementById("google-login").addEventListener("click", function() {
		  signInWithPopup(auth, provider)
		  .then((result) => {
		    // This gives you a Google Access Token. You can use it to access the Google API.
		    const credential = GoogleAuthProvider.credentialFromResult(result);
		    const token = credential.accessToken;
		    // The signed-in user info.
		    const user = result.user;
		    alert("Welcome "+user.displayName);
		    // ...
		  }).catch((error) => {
		    // Handle Errors here.
		    const errorCode = error.code;
		    const errorMessage = error.message;
		    // The email of the user's account used.
		    const email =;
		    // The AuthCredential type that was used.
		    const credential = GoogleAuthProvider.credentialFromError(error);
		    // ...
	  //----- End

In above code we have created one simple login button as below:

<button type="button" id="google-login" name="google-login" class="btn btn-danger btn-lg btn-block"><i class="fa fa-google "></i> Login with Google</button>

Also we have included firebasejs libraries and write some JavaScript code which is required for Google authentication. Here we have use GoogleAuthProvider and signInWithPopup method for login with Google.

Step 9: Now enable your Google Login

Select the Google option from above list

Enable the Google login and select your support mail id and click on Save button.

Step 10:  Domains need to be authorized for OAuth redirects.

If you will not add your domain then it will give below message in console log.

Info: The current domain is not authorized for OAuth operations. This will prevent signInWithPopup, signInWithRedirect, linkWithPopup and linkWithRedirect from working. Add your domain ( to the OAuth redirect domains list in the Firebase console -> Authentication -> Settings -> Authorized domains tab.

Now everything is set you can test your project in browser.

Login with Google

Related Posts

Divyesh Patel

I'm Divyesh Patel, Web & App developer. I want to make things that make a difference. With every line of code, i strive to make the web a beautiful place.

Leave a Reply