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

在Heroku上使用Google OAuth Passport策略绝对路径时出现内部服务器错误

Troubleshooting Passport Google Strategy Error on Heroku

Hey Andrew, let's break down what's going on here. At first glance, it seems like switching to an absolute callbackURL is causing the crash—but looking closely at your Heroku error logs, the real root issue is this line:

Error: Unknown authentication strategy "google"

This tells us Passport has no idea what the "google" strategy is when you hit /auth/google, which means the strategy was never registered via passport.use() in your code when using the absolute URL.

Why does the relative path work but absolute path fail?

Here are the most likely scenarios:

  • Conditional strategy registration: You might have wrapped the GoogleStrategy setup in a conditional check (like if (NODE_ENV === 'development')) that doesn't trigger in production when using the absolute URL. For example, if you only register the strategy when proxy: true is enabled, but disable that for absolute URLs, the strategy never gets added to Passport.
  • Broken environment variable: If your absolute callbackURL comes from an environment variable that's misspelled, missing, or incorrectly set on Heroku, initializing the GoogleStrategy could fail silently (if you don't have error handling), and the code never reaches passport.use().
  • Silent initialization errors: If there's a problem creating the GoogleStrategy (like a typo in client ID/secret, though you said those are correct) and you don't catch the error, the registration code stops executing, leaving Passport without the "google" strategy.

Steps to fix this:

  1. Ensure strategy registration runs in production: Check your code to make sure passport.use(new GoogleStrategy(...)) always executes when your app starts on Heroku. Remove any unnecessary conditionals that might block this code in production.
  2. Validate Heroku environment variables: Run heroku config to confirm the variable holding your absolute callbackURL is set correctly, and matches exactly what you've configured in Google's OAuth console.
  3. Add debug logs and error handling: Insert logs to confirm the strategy is registering, and catch any initialization errors. For example:
    console.log("Initializing Google Strategy with callbackURL:", process.env.YOUR_CALLBACK_URL_VAR);
    try {
      const googleStrategy = new GoogleStrategy({
        clientID: process.env.GOOGLE_OAUTH_CLIENT_ID,
        clientSecret: process.env.GOOGLE_OAUTH_CLIENT_SECRET,
        callbackURL: process.env.YOUR_CALLBACK_URL_VAR,
        proxy: true // You can keep this even with absolute URLs—Heroku uses proxies!
      }, async () => awaitHandlerFn);
      passport.use('google', googleStrategy);
      console.log("Google Strategy registered successfully");
    } catch (err) {
      console.error("Failed to initialize Google Strategy:", err);
    }
    
    Use heroku logs --tail to check if these messages appear, and if any errors are caught.
  4. Don't disable proxy: true unnecessarily: Even with an absolute URL, proxy: true helps Passport trust Heroku's proxy servers, so there's no need to turn it off.

Key takeaway:

The "Internal Server Error" is a red herring—this isn't a limitation of OAuth or Passport on Heroku. The real problem is your Google Strategy isn't being registered when using the absolute callbackURL, so Passport can't handle the /auth/google route.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 03:35:24