betterAuth callback for configuration #1688

Closed
opened 2026-03-13 08:56:31 -05:00 by GiteaMirror · 4 comments
Owner

Originally created by @bestickley on GitHub (Aug 12, 2025).

Is this suited for github?

  • Yes, this is suited for github

I want to be able to configure betterAuth options without running at module level scope. This causes issues when I run next build for example, and env vars are only available at request time.

Describe the solution you'd like

I want to be able to configure betterAuth with a callback instead of an object.

betterAuth(() => {
  const plugins = [];
  if (process.env.IDP_TYPE === "cognito") {
    plugins.push(genericOAuth(...));
  }  else {
    ..
  }
  return { plugins, ... };
})

Describe alternatives you've considered

Running this logic at module level scope.

Additional context

No response

Originally created by @bestickley on GitHub (Aug 12, 2025). ### Is this suited for github? - [x] Yes, this is suited for github ### Is your feature request related to a problem? Please describe. I want to be able to configure `betterAuth` options without running at module level scope. This causes issues when I run `next build` for example, and env vars are only available at request time. ### Describe the solution you'd like I want to be able to configure `betterAuth` with a callback instead of an object. ```ts betterAuth(() => { const plugins = []; if (process.env.IDP_TYPE === "cognito") { plugins.push(genericOAuth(...)); } else { .. } return { plugins, ... }; }) ``` ### Describe alternatives you've considered Running this logic at module level scope. ### Additional context _No response_
GiteaMirror added the nextjs label 2026-03-13 08:56:31 -05:00
Author
Owner

@ping-maxwell commented on GitHub (Aug 12, 2025):

Would loading dynamically once you have ENVs work for you?

@ping-maxwell commented on GitHub (Aug 12, 2025): Would loading dynamically once you have ENVs work for you?
Author
Owner

@bestickley commented on GitHub (Aug 13, 2025):

@ping-maxwell, not sure what you mean. Could you elaborate with code?

@bestickley commented on GitHub (Aug 13, 2025): @ping-maxwell, not sure what you mean. Could you elaborate with code?
Author
Owner

@Kinfe123 commented on GitHub (Aug 13, 2025):

Right now, Better Auth is initialized at module scope with a static options object passed to betterAuth(...) during setup. This keeps plugin wiring and other configurations consistent, ensuring type safety, security, and predictable runtime behavior. but there might be a hack to this. Moving this to a request-level setup would add overhead and could also break important capabilities like accurate type inference and certain APIs that depend on a stable, statically exposed auth instance. but if can be able to mitigate all those trade-off while maintaining the runtime safety that would be a good thing to have.

@Kinfe123 commented on GitHub (Aug 13, 2025): Right now, Better Auth is initialized at module scope with a static options object passed to betterAuth(...) during setup. This keeps plugin wiring and other configurations consistent, ensuring type safety, security, and predictable runtime behavior. but there might be a hack to this. Moving this to a request-level setup would add overhead and could also break important capabilities like accurate type inference and certain APIs that depend on a stable, statically exposed auth instance. but if can be able to mitigate all those trade-off while maintaining the runtime safety that would be a good thing to have.
Author
Owner

@JJosephttg commented on GitHub (Dec 5, 2025):

I think it is valuable to have for NestJS specifically. There are services we need to talk to that only show up at bootstrap time. For plugins like stripe, there is no way for us to talk to module based dependencies, which could be crucial for our flow

@JJosephttg commented on GitHub (Dec 5, 2025): I think it is valuable to have for NestJS specifically. There are services we need to talk to that only show up at bootstrap time. For plugins like stripe, there is no way for us to talk to module based dependencies, which could be crucial for our flow
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#1688