65

I am using firebase and its google auth tool , everything works fine the user data is getting saved in the database but i get a error every time the popup window appears (Cross-Origin-Opener-Policy policy would block the window.closed call)

enter image description here enter image description here

i am using next js and do help me resolve the error thank you

4
  • can you share the code link or create a reusable codebase? it might be because of a configuration issue, package versions, or even Chrome version. I never had such an issue with Firebase
    – Yilmaz
    Jun 24, 2023 at 1:49
  • Same error here, I was trying to access from localhost because thats what has always worked, but was getting the CORS error. I then switched from localhost to the localhost IP (which I needed to add to firebase authorized domains), and it worked, and now it works for both localhost and the localhost ip. Strange.
    – Blake
    Jun 24, 2023 at 7:38
  • Did you manage to figure this out? Have the same issue, although using Angular.
    – Spock
    Jun 25, 2023 at 14:33
  • 6
    Ugh, same. Are all +20 of us doing something wrong or is there something buggy with Firebase? I'm using SvelteKit.
    – Beyondo
    Jul 30, 2023 at 0:38

7 Answers 7

8

This error appears in the firebaseui npm package.

The first important thing to understand is that sign-in problem might not be related to the displayed console error: Cross-Origin-Opener-Policy policy would block the window.close call.

signInFlow="redirect"

The easiest way to check that your code is working without the error is to switch temporarily from signInFlow="popup" to signInFlow="redirect", which is the default value in Firebase UI Auth.

If Auth does not work

Check the code on:

  • Missed JS Context, for example, <AuthProvider/> in React.

  • Subscribe to firebase/auth events and check that the user object is returned (React example):

    import { getAuth, onAuthStateChanged } from 'firebase/auth';
    
    
    const StyledFirebaseAuth = () => {   
      useEffect(() => {
    
      const unregisterAuthObserver = onAuthStateChanged(
       getAuth(),
       async (user) => {
         console.log('user', user);
       },
     );
    
    // the rest of code
    }
    
  • Check Firebase Sign-in method settings to allow specific providers Firebase Sign-in method

  • Check Firebase Authentication allowed domains Firebase Authentication allowed domains

  • Check your Firebase configuration for the web application:

     const firebaseConfig = {
       apiKey: "ABCDEF...",
       authDomain: "your-project-domain",
       projectId: "you-project",
       appId: "1:abcdef..."
     };
    

Trying get rid of the console error

Chrome blog suggests to use Cross-Origin-Opener-Policy: restrict-properties with Cross-Origin-Embedder-Policy: require-corp. Sadly, it will not work with Firebase UI.

The problem is with Cross-Origin-Embedder-Policy header, which is by default should be set to unsafe-none.

This header should be returned with the html page or omitted (the implicit default value). It is possible to set it up manually.

response headers

Please notice that to correctly test it, a server should be restarted and browser cache cleared or a new incognito window used.

Local development:

Below are React examples but similarly should work other frameworks.

  • For create-react-app and craco modify craco.config.js as:
     module.exports = {
       devServer: {
         headers: {
           'Cross-Origin-Embedder-Policy': 'unsafe-none'
         }
       }
     }
    
  • For Next.js modify next.config.js:
     module.exports = {
       async headers() {
         return [
           {
             source: "/login",
             headers: [
               {
                 key: "Cross-Origin-Embedder-Policy",
                 value: "unsafe-none",
               },
             ],
           },
         ];
       },
     };
    

Firebase hosting

Firebase allows to specify headers that are passed for the hosted files, including .html, .js. Modify firebase.json file, for example, for a Single Page Application (SPA):

{
  "hosting": [
    {
      "target": "web",
      "public": "web/build",
      "ignore": ["firebase.json", "**/.*", "**/node_modules/**"],
      "rewrites": [
        {
          "source": "**",
          "destination": "/index.html"
        }
      ],
      "headers": [
        {
          "source": "**/*",
          "headers": [
            {
              "key": "Cross-Origin-Embedder-Policy",
              "value": "unsafe-none"
            }
          ]
        }
      ]
    }
  ]
}

Although after my attempts to change Cross-Origin-Opener-Policy, Cross-Origin-Resource-Policy and Cross-Origin-Embedder-Policy to different values still the error log appears in the Chrome browser but the sign-in method is working.

For more strict security settings and the unsafe-none value please refer to the documentation.

6

This header helps you to stay away from malicious websites. It allows you to control how you open your tabs or windows from your website and restrict interactions with other sources.

please refer to this Google auth setup article regarding the cross-origin opener policy

https://developers.google.com/identity/gsi/web/guides/get-google-api-clientid#cross_origin_opener_policy

It recommends using same-origin-allow-popups header instead of same origin for Cross-Origin-Opener-Policy on pages where you use google sign in button.

1
  • 3
    It didn't work for me. So far I haven't been able to solve this issue.
    – innomatic
    Jul 28, 2023 at 19:13
1

Check your SCOPES for the API.

Ran into this issue authorizing the Google Calendar API. For some reason it was working with my dev account but any other account would throw the "cross origin opener policy policy would block the window" error.
Turns out I accidentally had "const SCOPES = 'https://www.googleapis.com/auth/calendar.readonly'. All I had to do was update the SCOPES to "https://www.googleapis.com/auth/calendar" and everything worked.

https://developers.google.com/calendar/caldav/v2/auth

0

Please check your firebaseConfig, you can find it in Project Settings/ General on your Firebase, if you have made some changes to the project name or so, it might have conflicts between your firebaseConfig and the updated one on Firebase

0

I had the same issue, and I changed the method from "signInWithPopup" to "signInWithRedirect". But with this method, you need to handle this redirect with "useEffect" hook. I would paste my code here just for the reference.

import { getRedirectResult, signInWithRedirect } from 'firebase/auth';
import { useEffect, useState } from 'react';
import { auth, provider } from '../../services/auth';

const Login = () => {
    const [isLoading, setIsLoading] = useState(false);

    useEffect(() => {
        setIsLoading(true)
        getRedirectResult(auth).then(response => {
            if (!response) return

            // Your code here
        }).catch(error => {
            console.error(error);
        }).finally(() => setIsLoading(false))
    }, []);


    const onClick = async () =>
        signInWithRedirect(auth, provider).catch(error => {
            console.error(error);
        })

    return <button onClick={onClick} disabled={isLoading}> {isLoading ? "Loading. Please wait!" : "Sign In With Google"} </button>
}

export default Login;
-2

If you look in the console you should see an error saying that the domain doesn't have permission to do google sign in. What you need to do is go to the firebase console -> authentication -> settings and click on "authorized domains" and add your domain

3
  • 1
    No errors like that were found, I even tried too add localhost there in firebase and even hosted and added the domain there but the same error pops up
    – Chandan h
    Jun 14, 2023 at 9:57
  • Hi, Comder. Welcome to SO. Rather than down-vote your answer, I want to just let you know two things. 1. While you have suggested something that causes certain firebase auth errors... this is not that error. (I've seen that one. This isn't it.) 2. You can delete answers you realize aren't very good, so you don't get too many down-votes. Have a look at: meta.stackoverflow.com/questions/263046/…
    – XML
    Aug 16, 2023 at 10:55
  • Ah, this actually was the solution for me. But it's not because I was trying to solve this error itself. The pop up was working on localhost (with the error) but when I could not get it to work on the prod env, this error was all I could go off of. This answer was the reminder I needed to adjust my firebase auth settings, thanks!
    – mabarif
    Aug 17, 2023 at 12:47
-4

Switching from Chrome to Firefox, worked for me. Maybe, It is an issue with Chrome.

3
  • 3
    Hi, Martin. Welcome to SO. Rather than down-vote your answer, I want to just let you know two things. 1. Your answer is very pleasant, like something one friend might say to another. But it's neither fact-based nor methodical, so not a good answer on SO. 2. You can delete answers you realize aren't very good. Have a look at: meta.stackoverflow.com/questions/263046/…
    – XML
    Aug 16, 2023 at 10:54
  • It could very well be a chrome issue/firebase bug. I notice this error is still shown in the console, even when the pop up works in production.
    – mabarif
    Aug 17, 2023 at 12:50
  • 1
    Well, as far as I can see he is right. This happens on Google Chrome but not on Firefox.
    – innomatic
    Dec 7, 2023 at 18:58

Not the answer you're looking for? Browse other questions tagged or ask your own question.