Issues with environment variable not beeing present at build time #2900

Closed
opened 2026-03-13 10:26:27 -05:00 by GiteaMirror · 2 comments
Owner

Originally created by @mWtbg on GitHub (Feb 20, 2026).

Hi, in my project i use better-auth in Nextjs in stateless mode. I also use the generic oAuth plugin with the preconfigured provider for keycloak.

I noticed that my build fails whenever the einvironment variable "KEYCLOAK_ISSUER" is not present.
The error is the following:

Collecting page data ...
[Error: Failed to collect configuration for /] {
  [cause]: TypeError: Cannot read properties of undefined (reading 'replace')
      at v (.next/server/chunks/2824.js:361:71627)
      at 93573 (.next/server/chunks/6050.js:37:56207)
      at c (.next/server/webpack-runtime.js:1:127)
      at 94431 (.next/server/chunks/6050.js:37:56550)
      at Function.c (.next/server/webpack-runtime.js:1:127)
}
> Build error occurred
[Error: Failed to collect page data for /] { type: 'Error' }

I suspect it has something to do with this line:

d79c0db73b/packages/better-auth/src/plugins/generic-oauth/providers/keycloak.ts (L50)

Is this intended behaviour?

Originally created by @mWtbg on GitHub (Feb 20, 2026). Hi, in my project i use better-auth in Nextjs in stateless mode. I also use the generic oAuth plugin with the preconfigured provider for keycloak. I noticed that my build fails whenever the einvironment variable "KEYCLOAK_ISSUER" is not present. The error is the following: ``` Collecting page data ... [Error: Failed to collect configuration for /] { [cause]: TypeError: Cannot read properties of undefined (reading 'replace') at v (.next/server/chunks/2824.js:361:71627) at 93573 (.next/server/chunks/6050.js:37:56207) at c (.next/server/webpack-runtime.js:1:127) at 94431 (.next/server/chunks/6050.js:37:56550) at Function.c (.next/server/webpack-runtime.js:1:127) } > Build error occurred [Error: Failed to collect page data for /] { type: 'Error' } ``` I suspect it has something to do with this line: https://github.com/better-auth/better-auth/blob/d79c0db73b7da4ebc0d198447f2e925211c0cf7f/packages/better-auth/src/plugins/generic-oauth/providers/keycloak.ts#L50 Is this intended behaviour?
GiteaMirror added the nextjsbug labels 2026-03-13 10:26:27 -05:00
Author
Owner

@dosubot[bot] commented on GitHub (Feb 20, 2026):

Hi @mWtbg! I'm Dosu and I’m helping the better-auth team.

You've correctly identified the issue! The error occurs at line 50 in keycloak.ts where options.issuer.replace(/\/$/, "") is called - when KEYCLOAK_ISSUER is undefined, .replace() fails.

This isn't explicitly intended behavior, but the OAuth providers currently don't validate for missing environment variables at configuration time. A similar issue exists for SvelteKit builds.

Workarounds:

  1. Provide a fallback value:
keycloak({
  clientId: process.env.KEYCLOAK_CLIENT_ID || "",
  clientSecret: process.env.KEYCLOAK_CLIENT_SECRET || "",
  issuer: process.env.KEYCLOAK_ISSUER || "https://placeholder.example.com",
})
  1. Conditionally configure the provider:
const providers = [];
if (process.env.KEYCLOAK_ISSUER) {
  providers.push(keycloak({
    clientId: process.env.KEYCLOAK_CLIENT_ID,
    clientSecret: process.env.KEYCLOAK_CLIENT_SECRET,
    issuer: process.env.KEYCLOAK_ISSUER,
  }));
}

This could be considered a bug - the providers should ideally handle undefined values more gracefully or provide a clearer error message.

To reply, just mention @dosu.


How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other

Chat with Dosu Open in Cursor Join Discord Share on X

@dosubot[bot] commented on GitHub (Feb 20, 2026): <!-- Greeting --> Hi @mWtbg! I'm [Dosu](https://go.dosu.dev/dosubot) and I’m helping the better-auth team. <!-- Answer --> You've correctly identified the issue! The error occurs at [line 50 in keycloak.ts](https://github.com/better-auth/better-auth/blob/9c78e5cb18945077e03697c42a7c35037ab81a86/packages/better-auth/src/plugins/generic-oauth/providers/keycloak.ts#L50) where `options.issuer.replace(/\/$/, "")` is called - when `KEYCLOAK_ISSUER` is undefined, `.replace()` fails. This isn't explicitly intended behavior, but the OAuth providers currently don't validate for missing environment variables at configuration time. A [similar issue exists for SvelteKit builds](https://github.com/better-auth/better-auth/issues/8061). **Workarounds:** 1. **Provide a fallback value:** ```typescript keycloak({ clientId: process.env.KEYCLOAK_CLIENT_ID || "", clientSecret: process.env.KEYCLOAK_CLIENT_SECRET || "", issuer: process.env.KEYCLOAK_ISSUER || "https://placeholder.example.com", }) ``` 2. **Conditionally configure the provider:** ```typescript const providers = []; if (process.env.KEYCLOAK_ISSUER) { providers.push(keycloak({ clientId: process.env.KEYCLOAK_CLIENT_ID, clientSecret: process.env.KEYCLOAK_CLIENT_SECRET, issuer: process.env.KEYCLOAK_ISSUER, })); } ``` This could be considered a bug - the providers should ideally handle undefined values more gracefully or provide a clearer error message. <!-- Dosu Comment Footer --> *To reply, just mention [@dosu](https://go.dosu.dev/dosubot).* --- <sup>How did I do? [Good](https://app.dosu.dev/response-feedback/7217734a-dae8-412b-a203-c499b69ece22?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/7217734a-dae8-412b-a203-c499b69ece22?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/7217734a-dae8-412b-a203-c499b69ece22?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/7217734a-dae8-412b-a203-c499b69ece22?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/7217734a-dae8-412b-a203-c499b69ece22?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/7217734a-dae8-412b-a203-c499b69ece22?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/7217734a-dae8-412b-a203-c499b69ece22?feedback_type=other)</sup> [![Chat with Dosu](https://dosu.dev/dosu-chat-badge.svg)](https://app.dosu.dev/cdda13d9-dd27-4d31-b09a-5d8bec92de21/ask?utm_source=github)&nbsp;[![Open in Cursor](https://dosu.dev/dosu-in-cursor.svg)](https://cursor.com/link/prompt?text=You%27ve%20correctly%20identified%20the%20issue%21%20The%20error%20occurs%20at%20%5Bline%2050%20in%20keycloak.ts%5D%28https%3A//github.com/better-auth/better-auth/blob/9c78e5cb18945077e03697c42a7c35037ab81a86/packages/better-auth/src/plugins/generic-oauth/providers/keycloak.ts%23L50%29%20where%20%60options.issuer.replace%28/%5C/%24/%2C%20%22%22%29%60%20is%20called%20-%20when%20%60KEYCLOAK_ISSUER%60%20is%20undefined%2C%20%60.replace%28%29%60%20fails.%0A%0AThis%20isn%27t%20explicitly%20intended%20behavior%2C%20but%20the%20OAuth%20providers%20currently%20don%27t%20validate%20for%20missing%20environment%20variables%20at%20configuration%20time.%20A%20%5Bsimilar%20issue%20exists%20for%20SvelteKit%20builds%5D%28https%3A//github.com/better-auth/better-auth/issues/8061%29.%0A%0A%2A%2AWorkarounds%3A%2A%2A%0A%0A1.%20%2A%2AProvide%20a%20fallback%20value%3A%2A%2A%0A%60%60%60typescript%0Akeycloak%28%7B%0A%20%20clientId%3A%20process.env.KEYCLOAK_CLIENT_ID%20%7C%7C%20%22%22%2C%0A%20%20clientSecret%3A%20process.env.KEYCLOAK_CLIENT_SECRET%20%7C%7C%20%22%22%2C%0A%20%20issuer%3A%20process.env.KEYCLOAK_ISSUER%20%7C%7C%20%22https%3A//placeholder.example.com%22%2C%0A%7D%29%0A%60%60%60%0A%0A2.%20%2A%2AConditionally%20configure%20the%20provider%3A%2A%2A%0A%60%60%60typescript%0Aconst%20providers%20%3D%20%5B%5D%3B%0Aif%20%28process.env.KEYCLOAK_ISSUER%29%20%7B%0A%20%20providers.push%28keycloak%28%7B%0A%20%20%20%20clientId%3A%20process.env.KEYCLOAK_CLIENT_ID%2C%0A%20%20%20%20clientSecret%3A%20process.env.KEYCLOAK_CLIENT_SECRET%2C%0A%20%20%20%20issuer%3A%20process.env.KEYCLOAK_ISSUER%2C%0A%20%20%7D%29%29%3B%0A%7D%0A%60%60%60%0A%0AThis%20could%20be%20considered%20a%20bug%20-%20the%20providers%20should%20ideally%20handle%20undefined%20values%20more%20gracefully%20or%20provide%20a%20clearer%20error%20message.)&nbsp;[![Join Discord](https://img.shields.io/badge/join-5865F2?logo=discord&logoColor=white&label=)](https://go.dosu.dev/discord-bot)&nbsp;[![Share on X](https://img.shields.io/badge/X-share-black)](https://twitter.com/intent/tweet?text=%40dosu_ai%20helped%20me%20solve%20this%20issue!&url=https%3A//github.com/better-auth/better-auth/issues/8076)
Author
Owner

@himself65 commented on GitHub (Feb 20, 2026):

As AI said, you should provide the issuer during the build time. I believe you didn't provide it in vercel or your local env

@himself65 commented on GitHub (Feb 20, 2026): As AI said, you should provide the issuer during the build time. I believe you didn't provide it in vercel or your local env
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#2900