Error: auth/popup-closed-by-user in Firebase Authentication – How to Resolve Automatically Closed Popup Issue?

35 Views Asked by At

The issue I'm facing is related to Firebase Authentication, specifically the "auth/popup-closed-by-user" error. When attempting to sign in using Google OAuth with Firebase signInWithPopup, the authentication popup closes automatically, resulting in this error. I need help resolving this issue as it prevents users from signing in successfully.

// Your JavaScript code here
import { initializeApp } from "https://www.gstatic.com/firebasejs/10.9.0/firebase-app.js";
import { getAuth, GoogleAuthProvider, signInWithPopup } from "https://www.gstatic.com/firebasejs/10.9.0/firebase-auth.js";

// Your Firebase configuration
const firebaseConfig = {
  apiKey: "AIzaSyBpSJx-LfnXfr8as3P2KlKOOxyYuNxK70s",
  authDomain: "slackbot-87889.firebaseapp.com",
  projectId: "slackbot-87889",
  storageBucket: "slackbot-87889.appspot.com",
  messagingSenderId: "802474881579",
  appId: "1:802474881579:web:ca13a53a1273098548f06e"
};

// Initialize Firebase
const app = initializeApp(firebaseConfig);
const auth = getAuth(app);
auth.languageCode = 'en';
const provider = new GoogleAuthProvider();

// Function to handle Google sign-in
const handleGoogleSignIn = async () => {
  try {
    // Sign in with Google using popup
    const userCredential = await signInWithPopup(auth, provider);
    // User signed in successfully
    console.log("User signed in:", userCredential.user);
    // Redirect to desired URL
    window.location.href = "https://61165f2d-6a51-4b11-b1ae-89faf120c23c-00-d5u1itnpfld1.pike.replit.dev/";
  } catch (error) {
    // Handle sign-in errors
    console.error("Error signing in:", error);
  }
};

document.addEventListener('DOMContentLoaded', function() {
  const googleLogin = document.getElementById('google-login-btn');
  googleLogin.addEventListener('click', function() {
    handleGoogleSignIn(); // Call the handleGoogleSignIn function on button click
  });
});

I tried implementing the Firebase signInWithPopup method for Google OAuth in my Django web application. I expected the authentication popup to stay open for the user to sign in, but it closed automatically, leading to the "auth/popup-closed-by-user" error.
0

There are 0 best solutions below