如何配置托管在Firebase上的Angular应用,以正确处理Google认证处理程序的URL?

前端开发 2026-07-11

我有一个在Firebase上托管的Angular应用,目前使用Google作为登录提供商。当我使用默认的Firebase authDomain (https://default-firebase-app-url.firebaseapp.com/__/auth/handler) 时,实际的认证逻辑可以正常工作;但当我尝试将生产域名用于authDomain (https://auth.example.com/__/auth/handler) 时,就会出现问题,似乎问题出在Angular如何处理该路由。

当打开Google登录的弹出窗口时,窗口内我只是被重定向到我的Angular应用的首页:https://auth.example.com/。我在浏览器控制台看到一个错误,看起来像是Angular无法找到 __/auth/handler 路由的定义,因此它只是回退到基本URL:

ERROR B: NG04002: '__/auth/handler'

我的想法是,如果我能够配置Angular应用忽略这个路由,Firebase/Google就能够访问它并正常工作,但我一直只是把路由在Angular中设置为由某个组件处理,而不是相反,让应用程序“忽略”它。

解决方案

你需要确保Firebase Hosting的配置将 __/ 命名空间排除在重写为你的Angular index.html 之外。

在你的 firebase.json 中,重写规则应如下所示:

{
  "hosting": {
    "public": "dist/your-project-name",
    "ignore": [
      "firebase.json",
      "**/.*",
      "**/node_modules/**"
    ],
    "rewrites": [
      {
        "source": "!/__/**",
        "destination": "/index.html"
      }
    ]
  }
}
站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。

相关文章