Google Sign-In在Next.js中与Firebase不工作,因为出现了Cross-Origin-Opener-Policy错误。

huangapple go评论93阅读模式
英文:

Google Sign-In Not Working in Next.js with Firebase due to Cross-Origin-Opener-Policy Error

问题

I am developing a web application using Next.js and Firebase. I have successfully implemented Google sign-in in my application, but I've encountered an issue which is preventing the sign-in process from completing as expected.

When I attempt to sign in, a popup window appears as intended. However, in the console, I see the following error:

Cross-Origin-Opener-Policy policy would block the window.closed call.

Because of this error, the sign-in process doesn't complete, and the user is not properly signed in.

Some further context: I'm testing this functionality in a development environment, on localhost. The issue is active on Chrome, but not on Firefox.

Could this error be due to a misconfiguration of the Cross-Origin-Opener-Policy? If so, how can I correctly configure this policy for Google Sign-in with Firebase in a Next.js application?

英文:

I am developing a web application using Next.js and Firebase. I have successfully implemented Google sign-in in my application, but I've encountered an issue which is preventing the sign-in process from completing as expected.

When I attempt to sign in, a popup window appears as intended. However, in the console, I see the following error:

Cross-Origin-Opener-Policy policy would block the window.closed call.

Because of this error, the sign-in process doesn't complete and the user is not properly signed in.

Some further context: I'm testing this functionality in a development environment, on localhost. The issue is active on Chrome, but not on Firefox.

Could this error be due to a misconfiguration of the Cross-Origin-Opener-Policy? If so, how can I correctly configure this policy for Google Sign-in with Firebase in a Next.js application?

Google Sign-In在Next.js中与Firebase不工作,因为出现了Cross-Origin-Opener-Policy错误。

答案1

得分: 3

Yes, it has likely to do with the COOP configurations of your page, the Login page, and how they interact.
当两个页面的COOP不相同时,它们会进入不同的浏览上下文组,这可能会阻止一些交互,例如window.closed方法。

It's difficult to give an exact solution without seeing your code and implementation, however you could try to modify your COOP so that it matches the Login page's COOP.
在没有看到您的代码和实现的情况下,很难给出确切的解决方案,但您可以尝试修改您的COOP,使其与登录页面的COOP匹配。
This could be same-origin or same-origin-allow-popups.
这可以是same-originsame-origin-allow-popups

Those headers can be set in the NextJS Config: https://nextjs.org/docs/pages/api-reference/next-config-js/headers
这些标头可以在NextJS配置中设置:https://nextjs.org/docs/pages/api-reference/next-config-js/headers

in your case:
在您的情况下:

module.exports = {
  async headers() {
    return [
      {
        source: "/(.*)",
        headers: [
          {
            key: "Cross-Origin-Opener-Policy",
            value: "same-origin", // "same-origin-allow-popups"
          },
        ],
      },
    ];
  },
};
英文:

Yes, it has likely to do with the COOP configurations of your page, the Login page, and how they interact.
When two pages don't have the same COOP, they end up in separate browsing context groups, which may block some interactions like the window.closed method.

It's difficult to give an exact solution without seeing your code and implementation, however you could try to modify your COOP so that it matches the Login page's COOP.
This could be same-origin or same-origin-allow-popups.

Those headers can be set in the NextJS Config: https://nextjs.org/docs/pages/api-reference/next-config-js/headers

in your case:

module.exports = {
  async headers() {
    return [
      {
        source: "/(.*)",
        headers: [
          {
            key: "Cross-Origin-Opener-Policy",
            value: "same-origin", // "same-origin-allow-popups"
          },
        ],
      },
    ];
  },
};

答案2

得分: 2

这似乎是一个长期未解决的问题。然而,请尝试以下操作:

  1. 请检查范围 - https://stackoverflow.com/a/76574604/9640177
  2. 如果您正在使用Google API,请确保在Firebase和GCP上设置了适当的授权域或URI - https://stackoverflow.com/a/76547658/9640177

您还可以参考跨域隔离指南 - https://web.dev/cross-origin-isolation-guide/MDN文档以获取有关Cross-Origin-Opener-Policy的更多信息。

如果您正在使用Google API,请确保还添加了带有端口的URI,例如 localhost:3000。您可以查看我的实时网站 - https://radheshyamdas.com/,我正在使用Firebase认证,构建于Next.js之上。

英文:

This seems to be an unsolved issue for a long time. However, please try following

  1. Please check the scope - https://stackoverflow.com/a/76574604/9640177
  2. Make sure you have set proper authorized Domains or URIs set up on firebase and GCP if you are using Google APIs - https://stackoverflow.com/a/76547658/9640177

You can also refer to cross-origin isolation guide - https://web.dev/cross-origin-isolation-guide/ and MDN docs for more info on Cross-Origin-Opener-Policy

If you are using google APIs, make sure you also add the URIs with port as well, e.g., localhost:3000. You can check out my live website - https://radheshyamdas.com/ I am using firebase auth, built with Next.js

答案3

得分: 1

I believe your problem has to do with your environment setup and the reason is because the error you showing is under a javascript file which should be located on your dev env without violating the CROS policy.

The questions that should be answer are:

I would suggest to answered those questions and if that's true (you follow doc correctly) then show more details regarding the error. (like network under dev tools) also a good idea is to debug your app at the point of the authentication, otherwise if false then follow the documents because currently issue is not clear.

英文:

I believe your problem has to do with your environment setup and the reason is because the error you showing is under a javascript file which should be located on your dev env without violating the CROS policy.

The questions that should be answer are:

I would suggest to answered those questions and if that's true (you follow doc correctly) then show more details regarding the error. (like network under dev tools) also a good idea is to debug your app at the point of the authentication, otherwise if false then follow the documents because currently issue is not clear

答案4

得分: 1

我认为你应该在nextjs配置文件中允许firebase的URL。这对我来说似乎类似于在使用带有URL的图片时的情况。

另一个提示是,如果你不使用Vercel进行托管,你必须在环境变量中提供页面的baseUrl,我记得是这样的。这也可能会成为一个问题。

英文:

I think you should allow the firebase url in the nextjs config file. It seems to me similar like when you use pictures with urls.

Other tip, if you not hosting to Vercel you have to provide the baseUrl of the page in the environment variables, as I remember. It also could be a problem.

huangapple
  • 本文由 发表于 2023年6月26日 18:21:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76555769.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定