Skip to content

Commit c0f9875

Browse files
authored
♻️ refactor(auth): remove NEXT_PUBLIC_AUTH_URL env variable (lobehub#11658)
1 parent a8b042f commit c0f9875

File tree

19 files changed

+135
-124
lines changed

19 files changed

+135
-124
lines changed

.env.example

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -307,9 +307,6 @@ OPENAI_API_KEY=sk-xxxxxxxxx
307307
# Shared between Better-Auth and Next-Auth
308308
# AUTH_SECRET=xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
309309

310-
# Auth URL (accessible from browser, optional if same domain)
311-
# NEXT_PUBLIC_AUTH_URL=http://localhost:3210
312-
313310
# Require email verification before allowing users to sign in (default: false)
314311
# Set to '1' to force users to verify their email before signing in
315312
# NEXT_PUBLIC_AUTH_EMAIL_VERIFICATION=0

.env.example.development

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,6 @@ NEXT_PUBLIC_ENABLE_BETTER_AUTH=1
4343
# Better Auth secret for JWT signing (generate with: openssl rand -base64 32)
4444
AUTH_SECRET=${UNSAFE_SECRET}
4545

46-
# Authentication URL
47-
NEXT_PUBLIC_AUTH_URL=${APP_URL}
48-
4946
# SSO providers configuration - using Casdoor for development
5047
AUTH_SSO_PROVIDERS=casdoor
5148

Dockerfile

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,7 @@ ENV KEY_VAULTS_SECRET="" \
189189

190190
# Better Auth
191191
ENV AUTH_SECRET="" \
192-
AUTH_SSO_PROVIDERS="" \
193-
NEXT_PUBLIC_AUTH_URL=""
192+
AUTH_SSO_PROVIDERS=""
194193

195194
# Clerk
196195
ENV CLERK_SECRET_KEY="" \

docs/self-hosting/advanced/auth.mdx

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,11 @@ By setting the environment variables `NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY` and `CL
3939

4040
To enable Better Auth in LobeChat, set the following environment variables:
4141

42-
| Environment Variable | Type | Description |
43-
| -------------------------------- | -------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
44-
| `NEXT_PUBLIC_ENABLE_BETTER_AUTH` | Required | Set to `1` to enable Better Auth service |
45-
| `AUTH_SECRET` | Required | Key used to encrypt session tokens. Generate using: `openssl rand -base64 32` |
46-
| `NEXT_PUBLIC_AUTH_URL` | Required | The browser-accessible base URL for Better Auth (e.g., `http://localhost:3010`, `https://lobechat.com`). Optional for Vercel deployments (auto-detected from `VERCEL_URL`) |
47-
| `AUTH_SSO_PROVIDERS` | Optional | Comma-separated list of enabled SSO providers, e.g., `google,github,microsoft` |
42+
| Environment Variable | Type | Description |
43+
| -------------------------------- | -------- | ----------------------------------------------------------------------------- |
44+
| `NEXT_PUBLIC_ENABLE_BETTER_AUTH` | Required | Set to `1` to enable Better Auth service |
45+
| `AUTH_SECRET` | Required | Key used to encrypt session tokens. Generate using: `openssl rand -base64 32` |
46+
| `AUTH_SSO_PROVIDERS` | Optional | Comma-separated list of enabled SSO providers, e.g., `google,github,microsoft`|
4847

4948
<Callout type={'error'}>
5049
**Important**: Better Auth is currently only suitable for **fresh deployments**. If you are already using NextAuth or Clerk and have existing user data in your database, **do not switch to Better Auth yet**, otherwise existing users will not be able to log in.

docs/self-hosting/advanced/auth.zh-CN.mdx

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,11 @@ LobeChat 与 Clerk 做了深度集成,能够为用户提供一个更加安全
3737

3838
要在 LobeChat 中启用 Better Auth,请设置以下环境变量:
3939

40-
| 环境变量 | 类型 | 描述 |
41-
| -------------------------------- | -- | ---------------------------------------------------------------------------------------------------------------- |
42-
| `NEXT_PUBLIC_ENABLE_BETTER_AUTH` | 必选 | 设置为 `1` 以启用 Better Auth 服务 |
43-
| `AUTH_SECRET` | 必选 | 用于加密会话令牌的密钥。使用以下命令生成:`openssl rand -base64 32` |
44-
| `NEXT_PUBLIC_AUTH_URL` | 必选 | 浏览器可访问的 Better Auth 基础 URL(例如 `http://localhost:3010``https://lobechat.com`)。Vercel 部署时可选(会自动从 `VERCEL_URL` 获取) |
45-
| `AUTH_SSO_PROVIDERS` | 可选 | 启用的 SSO 提供商列表,以逗号分隔,例如 `google,github,microsoft` |
40+
| 环境变量 | 类型 | 描述 |
41+
| -------------------------------- | -- | ----------------------------------------------------------- |
42+
| `NEXT_PUBLIC_ENABLE_BETTER_AUTH` | 必选 | 设置为 `1` 以启用 Better Auth 服务 |
43+
| `AUTH_SECRET` | 必选 | 用于加密会话令牌的密钥。使用以下命令生成:`openssl rand -base64 32` |
44+
| `AUTH_SSO_PROVIDERS` | 可选 | 启用的 SSO 提供商列表,以逗号分隔,例如 `google,github,microsoft` |
4645

4746
<Callout type={'error'}>
4847
**重要提示**:Better Auth 目前仅适用于**全新部署**的场景。如果你已经使用 NextAuth 或 Clerk 并且数据库中存在用户数据,**请暂时不要切换到 Better Auth**,否则现有用户将无法登录。

docs/self-hosting/environment-variables/auth.mdx

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,6 @@ LobeChat provides a complete authentication service capability when deployed. Th
3434
- Default: `-`
3535
- Example: `Tfhi2t2pelSMEA8eaV61KaqPNEndFFdMIxDaJnS1CUI=`
3636

37-
#### `NEXT_PUBLIC_AUTH_URL`
38-
39-
- Type: Optional
40-
- Description: The URL accessible from the browser for Better Auth callbacks. Only set this if the default generated URL is incorrect.
41-
- Default: `-`
42-
- Example: `https://example.com`
43-
4437
#### `NEXT_PUBLIC_AUTH_EMAIL_VERIFICATION`
4538

4639
- Type: Optional

docs/self-hosting/environment-variables/auth.zh-CN.mdx

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,6 @@ LobeChat 在部署时提供了完善的身份验证服务能力,以下是相
3232
- 默认值:`-`
3333
- 示例:`Tfhi2t2pelSMEA8eaV61KaqPNEndFFdMIxDaJnS1CUI=`
3434

35-
#### `NEXT_PUBLIC_AUTH_URL`
36-
37-
- 类型:可选
38-
- 描述:浏览器可访问的 Better Auth 回调 URL。仅在默认生成的 URL 不正确时设置。
39-
- 默认值:`-`
40-
- 示例:`https://example.com`
41-
4235
#### `NEXT_PUBLIC_AUTH_EMAIL_VERIFICATION`
4336

4437
- 类型:可选

scripts/prebuild.mts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,10 @@ const printEnvInfo = () => {
5151

5252
// Auth-related env vars
5353
console.log('\n Auth Environment Variables:');
54-
console.log(` NEXT_PUBLIC_AUTH_URL: ${process.env.NEXT_PUBLIC_AUTH_URL ?? '(not set)'}`);
55-
console.log(` NEXTAUTH_URL: ${process.env.NEXTAUTH_URL ?? '(not set)'}`);
5654
console.log(` APP_URL: ${process.env.APP_URL ?? '(not set)'}`);
5755
console.log(` VERCEL_URL: ${process.env.VERCEL_URL ?? '(not set)'}`);
56+
console.log(` VERCEL_BRANCH_URL: ${process.env.VERCEL_BRANCH_URL ?? '(not set)'}`);
57+
console.log(` VERCEL_PROJECT_PRODUCTION_URL: ${process.env.VERCEL_PROJECT_PRODUCTION_URL ?? '(not set)'}`);
5858
console.log(` AUTH_EMAIL_VERIFICATION: ${process.env.AUTH_EMAIL_VERIFICATION ?? '(not set)'}`);
5959
console.log(` ENABLE_MAGIC_LINK: ${process.env.ENABLE_MAGIC_LINK ?? '(not set)'}`);
6060
console.log(` AUTH_SECRET: ${process.env.AUTH_SECRET ? '✓ set' : '✗ not set'}`);

src/envs/__tests__/app.test.ts

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,84 @@ describe('getServerConfig', () => {
8282
});
8383
});
8484
});
85+
86+
describe('APP_URL fallback', () => {
87+
beforeEach(() => {
88+
vi.resetModules();
89+
// Clean up all related env vars
90+
delete process.env.APP_URL;
91+
delete process.env.VERCEL;
92+
delete process.env.VERCEL_ENV;
93+
delete process.env.VERCEL_URL;
94+
delete process.env.VERCEL_BRANCH_URL;
95+
delete process.env.VERCEL_PROJECT_PRODUCTION_URL;
96+
});
97+
98+
it('should use APP_URL when explicitly set', async () => {
99+
process.env.APP_URL = 'https://custom-app.com';
100+
process.env.VERCEL = '1';
101+
102+
const { getAppConfig } = await import('../app');
103+
const config = getAppConfig();
104+
expect(config.APP_URL).toBe('https://custom-app.com');
105+
});
106+
107+
describe('Vercel environment', () => {
108+
it('should use VERCEL_PROJECT_PRODUCTION_URL in production', async () => {
109+
process.env.VERCEL = '1';
110+
process.env.VERCEL_ENV = 'production';
111+
process.env.VERCEL_PROJECT_PRODUCTION_URL = 'lobechat.vercel.app';
112+
process.env.VERCEL_BRANCH_URL = 'lobechat-git-main-org.vercel.app';
113+
process.env.VERCEL_URL = 'lobechat-abc123.vercel.app';
114+
115+
const { getAppConfig } = await import('../app');
116+
const config = getAppConfig();
117+
expect(config.APP_URL).toBe('https://lobechat.vercel.app');
118+
});
119+
120+
it('should use VERCEL_BRANCH_URL in preview environment', async () => {
121+
process.env.VERCEL = '1';
122+
process.env.VERCEL_ENV = 'preview';
123+
process.env.VERCEL_BRANCH_URL = 'lobechat-git-feature-org.vercel.app';
124+
process.env.VERCEL_URL = 'lobechat-abc123.vercel.app';
125+
126+
const { getAppConfig } = await import('../app');
127+
const config = getAppConfig();
128+
expect(config.APP_URL).toBe('https://lobechat-git-feature-org.vercel.app');
129+
});
130+
131+
it('should fallback to VERCEL_URL when VERCEL_BRANCH_URL is not set', async () => {
132+
process.env.VERCEL = '1';
133+
process.env.VERCEL_ENV = 'preview';
134+
process.env.VERCEL_URL = 'lobechat-abc123.vercel.app';
135+
136+
const { getAppConfig } = await import('../app');
137+
const config = getAppConfig();
138+
expect(config.APP_URL).toBe('https://lobechat-abc123.vercel.app');
139+
});
140+
});
141+
142+
describe('local environment', () => {
143+
it('should use localhost:3010 in development', async () => {
144+
145+
vi.stubEnv('NODE_ENV', 'development');
146+
147+
const { getAppConfig } = await import('../app');
148+
const config = getAppConfig();
149+
expect(config.APP_URL).toBe('http://localhost:3010');
150+
151+
152+
});
153+
154+
it('should use localhost:3210 in non-development', async () => {
155+
156+
vi.stubEnv('NODE_ENV', 'test');
157+
158+
const { getAppConfig } = await import('../app');
159+
const config = getAppConfig();
160+
expect(config.APP_URL).toBe('http://localhost:3210');
161+
162+
163+
});
164+
});
165+
});

src/envs/app.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,24 @@ declare global {
1212
}
1313
const isInVercel = process.env.VERCEL === '1';
1414

15-
const vercelUrl = `https://${process.env.VERCEL_URL}`;
15+
// Vercel URL fallback order (by stability):
16+
// 1. VERCEL_PROJECT_PRODUCTION_URL - project level, most stable
17+
// 2. VERCEL_BRANCH_URL - branch level, stable across deployments on same branch
18+
// 3. VERCEL_URL - deployment level, changes every deployment
19+
const getVercelUrl = () => {
20+
if (process.env.VERCEL_ENV === 'production' && process.env.VERCEL_PROJECT_PRODUCTION_URL) {
21+
return `https://${process.env.VERCEL_PROJECT_PRODUCTION_URL}`;
22+
}
23+
if (process.env.VERCEL_BRANCH_URL) {
24+
return `https://${process.env.VERCEL_BRANCH_URL}`;
25+
}
26+
return `https://${process.env.VERCEL_URL}`;
27+
};
1628

1729
const APP_URL = process.env.APP_URL
1830
? process.env.APP_URL
1931
: isInVercel
20-
? vercelUrl
32+
? getVercelUrl()
2133
: process.env.NODE_ENV === 'development'
2234
? 'http://localhost:3010'
2335
: 'http://localhost:3210';

0 commit comments

Comments
 (0)