Tanstack Start environment variables not working in auth.ts #1518

Closed
opened 2026-03-13 08:44:53 -05:00 by GiteaMirror · 2 comments
Owner

Originally created by @janwolfram on GitHub (Jul 18, 2025).

I just configured better-auth in tanstack start and running into this issue. In my auth.ts file the env variables don't work inside the betterAuth setup.

import { betterAuth } from "better-auth";
import { reactStartCookies } from "better-auth/react-start";
import { drizzleAdapter } from "better-auth/adapters/drizzle";
import { database } from "~/db";

export const auth = betterAuth({
  database: drizzleAdapter(database, {
    provider: "pg",
  }),
  plugins: [reactStartCookies()],
  socialProviders: {
    google: {
      clientId: process.env.GOOGLE_CLIENT_ID as string,
      clientSecret: process.env.GOOGLE_CLIENT_SECRET as string,
    },
  },
  emailAndPassword: {
    enabled: true,
  },
});

It is throwing this error into my server console.

ERROR [Better Auth]: Client Id and Client Secret is required for Google. Make sure to provide them in the options.
# SERVER_ERROR:  [BetterAuthError: CLIENT_ID_AND_SECRET_REQUIRED] { cause: undefined }

process.env works fine for setting up my database. Also when I copy the values for GOOGLE_CLIENT_ID and GOOGLE_CLIENT_SECRET into the auth.ts file better-auth just works fine and as expected. But yeah I can not expose my secret ^^f.
Thanks for any help :)

Originally created by @janwolfram on GitHub (Jul 18, 2025). I just configured better-auth in tanstack start and running into this issue. In my auth.ts file the env variables don't work inside the betterAuth setup. ``` import { betterAuth } from "better-auth"; import { reactStartCookies } from "better-auth/react-start"; import { drizzleAdapter } from "better-auth/adapters/drizzle"; import { database } from "~/db"; export const auth = betterAuth({ database: drizzleAdapter(database, { provider: "pg", }), plugins: [reactStartCookies()], socialProviders: { google: { clientId: process.env.GOOGLE_CLIENT_ID as string, clientSecret: process.env.GOOGLE_CLIENT_SECRET as string, }, }, emailAndPassword: { enabled: true, }, }); ``` It is throwing this error into my server console. ``` ERROR [Better Auth]: Client Id and Client Secret is required for Google. Make sure to provide them in the options. # SERVER_ERROR: [BetterAuthError: CLIENT_ID_AND_SECRET_REQUIRED] { cause: undefined } ``` process.env works fine for setting up my database. Also when I copy the values for GOOGLE_CLIENT_ID and GOOGLE_CLIENT_SECRET into the auth.ts file better-auth just works fine and as expected. But yeah I can not expose my secret ^^f. Thanks for any help :)
GiteaMirror added the tanstack-start label 2026-03-13 08:44:53 -05:00
Author
Owner

@dosubot[bot] commented on GitHub (Jul 18, 2025):

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

This usually happens if the environment variables aren't available in the context where betterAuth runs. In Tanstack Start, make sure your auth.ts file is only ever imported/executed on the server—if it's imported in any client-side code, process.env will be undefined. The official Tanstack Start example for better-auth uses process.env for socialProviders, so it should work if the environment is set up correctly and the code runs server-side only (example).

Double-check that:

  • Your .env file is loaded (restart your dev server after changes).
  • The GOOGLE_CLIENT_ID and GOOGLE_CLIENT_SECRET are present in your environment at runtime.
  • auth.ts is not imported or referenced in any client-side code or files.
  • You are not using any build tools or plugins that might strip or replace process.env in server files.

If you still see the error, try logging process.env.GOOGLE_CLIENT_ID inside auth.ts right before calling betterAuth to confirm it's defined at runtime. If it's undefined, the issue is with how Tanstack Start loads env vars in your project setup. If it's defined, but better-auth still errors, there may be a typo or whitespace issue in the env file.

Let me know what you find!

To reply, just mention @dosu.


How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other  Join Discord Share on X

@dosubot[bot] commented on GitHub (Jul 18, 2025): <!-- Greeting --> Hi @janwolfram! I'm [Dosu](https://go.dosu.dev/dosubot) and I’m helping the better-auth team. <!-- Answer --> This usually happens if the environment variables aren't available in the context where betterAuth runs. In Tanstack Start, make sure your auth.ts file is only ever imported/executed on the server—if it's imported in any client-side code, process.env will be undefined. The official Tanstack Start example for better-auth uses process.env for socialProviders, so it should work if the environment is set up correctly and the code runs server-side only ([example](https://github.com/better-auth/better-auth/blob/4c9479ee2bab2f6515f713a145418a9b1141cbe9/examples/tanstack-example/app/lib/auth.ts)). Double-check that: - Your .env file is loaded (restart your dev server after changes). - The GOOGLE_CLIENT_ID and GOOGLE_CLIENT_SECRET are present in your environment at runtime. - auth.ts is not imported or referenced in any client-side code or files. - You are not using any build tools or plugins that might strip or replace process.env in server files. If you still see the error, try logging process.env.GOOGLE_CLIENT_ID inside auth.ts right before calling betterAuth to confirm it's defined at runtime. If it's undefined, the issue is with how Tanstack Start loads env vars in your project setup. If it's defined, but better-auth still errors, there may be a typo or whitespace issue in the env file. Let me know what you find! <!-- 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/9c1ac38a-4b4f-4e65-8d83-5f108a08254b?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/9c1ac38a-4b4f-4e65-8d83-5f108a08254b?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/9c1ac38a-4b4f-4e65-8d83-5f108a08254b?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/9c1ac38a-4b4f-4e65-8d83-5f108a08254b?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/9c1ac38a-4b4f-4e65-8d83-5f108a08254b?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/9c1ac38a-4b4f-4e65-8d83-5f108a08254b?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/9c1ac38a-4b4f-4e65-8d83-5f108a08254b?feedback_type=other)</sup>&nbsp;&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/3444)
Author
Owner

@janwolfram commented on GitHub (Jul 18, 2025):

I am very sorry I opened this issue. I had a typo :(

@janwolfram commented on GitHub (Jul 18, 2025): I am very sorry I opened this issue. I had a typo :(
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#1518