Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

🐛 fix: better-auth fallback next-auth providers env (#10459)

YuTengjinge1670758cebb
diff --git a/src/envs/auth.test.ts b/src/envs/auth.test.ts
new file mode 100644
index 00000000000..c3ae6e255c4
--- /dev/null
+++ b/src/envs/auth.test.ts
@@ -0,0 +1,60 @@
+import { afterEach, beforeEach, describe, expect, it } from 'vitest';
+
+import { getAuthConfig } from './auth';
+
+const ORIGINAL_ENV = { ...process.env };
+const ORIGINAL_WINDOW = globalThis.window;
+
+describe('getAuthConfig fallbacks', () => {
+ beforeEach(() => {
+ // reset env to a clean clone before each test
+ process.env = { ...ORIGINAL_ENV };
+ globalThis.window = ORIGINAL_WINDOW;
+ });
+
+ afterEach(() => {
+ process.env = { ...ORIGINAL_ENV };
+ globalThis.window = ORIGINAL_WINDOW;
+ });
+
+ it('should fall back to NEXT_AUTH_SSO_PROVIDERS when AUTH_SSO_PROVIDERS is empty string', () => {
+ process.env.AUTH_SSO_PROVIDERS = '';
+ process.env.NEXT_AUTH_SSO_PROVIDERS = 'logto,github';
+
+ // Simulate server runtime so @t3-oss/env treats this as server-side access
+ // (happy-dom sets window by default in Vitest)
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
+ // @ts-expect-error - allow overriding for test
+ globalThis.window = undefined;
+
+ const config = getAuthConfig();
+
+ expect(config.AUTH_SSO_PROVIDERS).toBe('logto,github');
+ });
+
+ it('should fall back to NEXT_AUTH_SECRET when AUTH_SECRET is empty string', () => {
+ process.env.AUTH_SECRET = '';
+ process.env.NEXT_AUTH_SECRET = 'nextauth-secret';
+
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
+ // @ts-expect-error - allow overriding for test
+ globalThis.window = undefined;
+
+ const config = getAuthConfig();
+
+ expect(config.AUTH_SECRET).toBe('nextauth-secret');
+ });
+
+ it('should fall back to NEXTAUTH_URL origin when NEXT_PUBLIC_AUTH_URL is empty string', () => {
+ process.env.NEXT_PUBLIC_AUTH_URL = '';
+ process.env.NEXTAUTH_URL = 'https://example.com/api/auth';
+
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
+ // @ts-expect-error - allow overriding for test
+ globalThis.window = undefined;
+
+ const config = getAuthConfig();
+
+ expect(config.NEXT_PUBLIC_AUTH_URL).toBe('https://example.com');
+ });
+});
diff --git a/src/envs/auth.ts b/src/envs/auth.ts
index 61e6d73c32b..410be92e4b2 100644
--- a/src/envs/auth.ts
+++ b/src/envs/auth.ts
@@ -237,14 +237,14 @@ export const getAuthConfig = () => {
NEXT_PUBLIC_ENABLE_BETTER_AUTH: process.env.NEXT_PUBLIC_ENABLE_BETTER_AUTH === '1',
// Fallback to NEXTAUTH_URL origin for seamless migration from next-auth
NEXT_PUBLIC_AUTH_URL:
- process.env.NEXT_PUBLIC_AUTH_URL ??
+ process.env.NEXT_PUBLIC_AUTH_URL ||
(process.env.NEXTAUTH_URL ? new URL(process.env.NEXTAUTH_URL).origin : undefined),
NEXT_PUBLIC_AUTH_EMAIL_VERIFICATION: process.env.NEXT_PUBLIC_AUTH_EMAIL_VERIFICATION === '1',
NEXT_PUBLIC_ENABLE_MAGIC_LINK: process.env.NEXT_PUBLIC_ENABLE_MAGIC_LINK === '1',
// Fallback to NEXT_AUTH_SECRET for seamless migration from next-auth
- AUTH_SECRET: process.env.AUTH_SECRET ?? process.env.NEXT_AUTH_SECRET,
+ AUTH_SECRET: process.env.AUTH_SECRET || process.env.NEXT_AUTH_SECRET,
// Fallback to NEXT_AUTH_SSO_PROVIDERS for seamless migration from next-auth
- AUTH_SSO_PROVIDERS: process.env.AUTH_SSO_PROVIDERS ?? process.env.NEXT_AUTH_SSO_PROVIDERS,
+ AUTH_SSO_PROVIDERS: process.env.AUTH_SSO_PROVIDERS || process.env.NEXT_AUTH_SSO_PROVIDERS,
// better-auth env for Cognito provider is different from next-auth's one
AUTH_COGNITO_DOMAIN: process.env.AUTH_COGNITO_DOMAIN,