You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

部署Firebase Hosting后访问跳转至Google登录页问题排查

Hey there, let's dig into why your SSR app on Firebase Hosting is redirecting to Google Accounts, and walk through how to fix it.

Possible Causes

  • Forced authentication in your SSR function (index.js): It’s likely your index.js includes Firebase Auth middleware that requires users to be logged in for all routes, with no exceptions for public pages. When an unauthenticated user visits, Firebase Auth automatically redirects them to Google’s login page (its default behavior for unauthenticated requests).
  • Misconfigured Firebase Hosting rewrites: Your firebase.json might have incorrect rewrite rules that send requests to a protected endpoint, or your SSR Cloud Function has IAM permissions set to only allow authenticated users. This triggers an automatic redirect when unauthenticated users try to access the function.
  • Overly restrictive Hosting authentication settings: If you added authentication rules in firebase.json that enforce login for all paths (without exceptions), every unauthenticated visit will trigger the Google Accounts redirect.

Fixes to Try

Adjust SSR Function Authentication Logic

Open your index.js and check your auth middleware setup:

  1. Exclude public pages from auth checks: If only certain routes need login, apply the auth middleware selectively instead of globally:
    // ❌ Bad: Global auth enforcement
    app.use(requireAuth);
    
    // ✅ Good: Apply auth only to protected routes
    app.get('/profile', requireAuth, (req, res) => { /* SSR protected profile page */ });
    app.get('/', (req, res) => { /* SSR public homepage, no auth needed */ });
    
  2. Customize unauthenticated behavior: If you do need full-site login, tweak the middleware to return a custom 401 page instead of redirecting, or adjust the redirect target to your own login page (instead of Google’s default).

Fix Firebase Hosting Rewrite Rules

Check your firebase.json for incorrect rewrite configurations:

  1. Ensure rewrites point to your correct SSR function:
    {
      "hosting": {
        "public": "public",
        "rewrites": [
          {
            "source": "**",
            "function": "ssrRenderer" // Replace with your actual SSR function name
          }
        ]
      }
    }
    
  2. Update Cloud Function IAM permissions: If your SSR function is set to only allow authenticated users, head to the Firebase Console → Cloud Functions, find your SSR function, and adjust its permissions to allow allUsers to invoke it (if public access is intended).

Tune Hosting Authentication Settings

If you have authentication rules in firebase.json:

  1. Restrict auth requirements to specific paths only:
    {
      "hosting": {
        "public": "public",
        "authentication": {
          "required": false, // Default to no auth
          "paths": {
            "/dashboard/**": {
              "required": true // Only enforce auth on dashboard routes
            }
          }
        }
      }
    }
    
  2. Remove the authentication block entirely if you don’t need any site-wide auth enforcement.

After making these changes, redeploy your app with firebase deploy and test using an incognito window to verify the redirect no longer happens.

内容的提问来源于stack exchange,提问作者Patrickkx

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 09:09:10