@lobehub/lobehub 2.0.0-next.344 → 2.0.0-next.346
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.cursor/rules/i18n.mdc +1 -1
- package/.cursor/rules/modal-imperative.mdc +162 -0
- package/.cursor/rules/rules-index.mdc +1 -0
- package/.env.example +0 -14
- package/.eslintrc.js +8 -1
- package/CHANGELOG.md +66 -0
- package/CLAUDE.md +4 -2
- package/Dockerfile +3 -13
- package/README.md +3 -5
- package/README.zh-CN.md +3 -5
- package/changelog/v1.json +20 -0
- package/docs/self-hosting/advanced/auth/clerk-to-betterauth.mdx +11 -42
- package/docs/self-hosting/advanced/auth/clerk-to-betterauth.zh-CN.mdx +10 -41
- package/e2e/src/support/webServer.ts +2 -0
- package/locales/ar/error.json +0 -4
- package/locales/bg-BG/error.json +0 -4
- package/locales/de-DE/error.json +0 -4
- package/locales/en-US/error.json +0 -4
- package/locales/es-ES/error.json +0 -4
- package/locales/fa-IR/error.json +0 -4
- package/locales/fr-FR/error.json +0 -4
- package/locales/it-IT/error.json +0 -4
- package/locales/ja-JP/error.json +0 -4
- package/locales/ko-KR/error.json +0 -4
- package/locales/nl-NL/error.json +0 -4
- package/locales/pl-PL/error.json +0 -4
- package/locales/pt-BR/error.json +0 -4
- package/locales/ru-RU/error.json +0 -4
- package/locales/tr-TR/error.json +0 -4
- package/locales/vi-VN/error.json +0 -4
- package/locales/zh-CN/error.json +0 -4
- package/locales/zh-TW/error.json +0 -4
- package/package.json +12 -12
- package/packages/builtin-agents/package.json +2 -0
- package/packages/builtin-agents/src/agents/agent-builder/index.ts +4 -2
- package/packages/builtin-agents/src/agents/group-agent-builder/index.ts +4 -2
- package/packages/builtin-agents/src/agents/page-agent/index.ts +5 -2
- package/packages/context-engine/src/engine/messages/MessagesEngine.ts +9 -9
- package/packages/context-engine/src/providers/GroupContextInjector.ts +19 -33
- package/packages/context-engine/src/providers/__tests__/GroupContextInjector.test.ts +79 -43
- package/packages/context-engine/src/providers/__tests__/__snapshots__/GroupContextInjector.test.ts.snap +5 -15
- package/packages/database/src/repositories/userMemory/__tests__/UserMemoryTopicRepository.test.ts +24 -3
- package/packages/file-loaders/package.json +1 -1
- package/packages/file-loaders/src/loadFile.ts +10 -15
- package/packages/file-loaders/src/loaders/index.ts +68 -19
- package/packages/file-loaders/src/loaders/pdf/__snapshots__/index.test.ts.snap +1 -1
- package/packages/file-loaders/test/__snapshots__/loaders.test.ts.snap +1 -1
- package/packages/model-bank/src/modelProviders/comfyui.ts +0 -1
- package/packages/model-bank/src/modelProviders/fal.ts +0 -1
- package/packages/types/src/fetch.ts +1 -2
- package/packages/utils/src/server/__tests__/auth.test.ts +0 -47
- package/packages/utils/src/server/auth.ts +1 -9
- package/pnpm-workspace.yaml +1 -0
- package/scripts/_shared/checkDeprecatedClerkEnv.js +42 -0
- package/scripts/changelogWorkflow/buildStaticChangelog.ts +2 -1
- package/scripts/clerk-to-betterauth/_internal/types.ts +53 -20
- package/scripts/clerk-to-betterauth/export-clerk-users-with-api.ts +43 -36
- package/scripts/countEnWord.ts +1 -1
- package/scripts/electronWorkflow/modifiers/appCode.mts +2 -131
- package/scripts/i18nWorkflow/protectedPatterns.ts +1 -2
- package/scripts/prebuild.mts +10 -8
- package/scripts/serverLauncher/startServer.js +23 -5
- package/src/app/(backend)/middleware/auth/index.test.ts +8 -4
- package/src/app/(backend)/middleware/auth/index.ts +0 -15
- package/src/app/(backend)/middleware/auth/utils.test.ts +0 -28
- package/src/app/(backend)/middleware/auth/utils.ts +2 -17
- package/src/app/(backend)/webapi/chat/[provider]/route.test.ts +3 -51
- package/src/app/(backend)/webapi/models/[provider]/route.test.ts +8 -4
- package/src/app/[variants]/(auth)/next-auth/signin/AuthSignInBox.tsx +7 -6
- package/src/app/[variants]/(auth)/signup/[[...signup]]/page.tsx +1 -16
- package/src/app/[variants]/(main)/home/_layout/Body/Agent/List/index.tsx +1 -1
- package/src/app/[variants]/(main)/home/features/InputArea/SkillInstallBanner.tsx +13 -13
- package/src/app/[variants]/(main)/home/features/RecentPage/Item.tsx +2 -2
- package/src/app/[variants]/(main)/resource/features/store/action.ts +2 -2
- package/src/app/[variants]/(main)/resource/features/store/initialState.ts +2 -2
- package/src/app/[variants]/(main)/resource/store/action.ts +2 -2
- package/src/app/[variants]/(main)/resource/store/initialState.ts +2 -2
- package/src/app/[variants]/(main)/settings/hooks/useCategory.tsx +3 -21
- package/src/app/[variants]/(main)/settings/profile/features/AvatarRow.tsx +1 -2
- package/src/app/[variants]/(main)/settings/security/index.tsx +1 -22
- package/src/app/[variants]/(main)/settings/skill/features/KlavisSkillItem.tsx +12 -14
- package/src/app/[variants]/(main)/settings/skill/features/LobehubSkillItem.tsx +8 -14
- package/src/app/[variants]/(main)/settings/skill/index.tsx +7 -5
- package/src/app/[variants]/(mobile)/me/(home)/__tests__/UserBanner.test.tsx +2 -35
- package/src/app/[variants]/(mobile)/me/(home)/__tests__/useCategory.test.tsx +0 -20
- package/src/app/[variants]/(mobile)/me/(home)/features/UserBanner.tsx +1 -2
- package/src/app/[variants]/(mobile)/me/profile/features/Category.tsx +3 -13
- package/src/app/[variants]/(mobile)/settings/_layout/Header.tsx +2 -3
- package/src/app/[variants]/share/t/[id]/_layout/index.tsx +1 -1
- package/src/app/[variants]/share/t/[id]/index.tsx +1 -1
- package/src/app/robots.tsx +1 -1
- package/src/envs/auth.ts +2 -27
- package/src/envs/llm.ts +2 -2
- package/src/features/AgentSetting/AgentPlugin/index.tsx +9 -12
- package/src/features/ChatInput/ActionBar/Tools/index.tsx +7 -5
- package/src/features/ChatMiniMap/utils.ts +1 -1
- package/src/features/CommandMenu/SearchResults.tsx +1 -1
- package/src/features/Conversation/ChatList/components/AutoScroll/DebugInspector.tsx +166 -0
- package/src/features/Conversation/ChatList/components/AutoScroll/index.tsx +86 -0
- package/src/features/Conversation/ChatList/components/VirtualizedList.tsx +11 -17
- package/src/features/Conversation/Messages/AgentCouncil/components/AutoScrollShadow.tsx +25 -14
- package/src/features/Conversation/Messages/AgentCouncil/components/CouncilMember.tsx +1 -1
- package/src/features/FileViewer/Renderer/PDF/index.tsx +5 -8
- package/src/features/IntegrationDetailModal/IntegrationDetailContent.tsx +305 -0
- package/src/features/IntegrationDetailModal/index.tsx +21 -283
- package/src/features/MCPPluginDetail/Deployment/index.tsx +1 -1
- package/src/features/MCPPluginDetail/Schema/Prompts.tsx +1 -1
- package/src/features/MCPPluginDetail/Schema/Tools.tsx +1 -1
- package/src/features/ProfileEditor/AgentTool.tsx +14 -20
- package/src/features/ResourceManager/components/Explorer/ListView/ListItem/index.tsx +0 -8
- package/src/features/ResourceManager/components/Explorer/MasonryView/MasonryFileItem/NoteFileItem.tsx +1 -1
- package/src/features/ResourceManager/index.tsx +1 -1
- package/src/features/ShareModal/SharePdf/PdfPreview.tsx +4 -4
- package/src/features/SkillStore/LobeHubList/index.tsx +50 -87
- package/src/features/SkillStore/Search/index.tsx +1 -1
- package/src/features/SkillStore/{Content.tsx → SkillStoreContent.tsx} +3 -8
- package/src/features/SkillStore/index.tsx +15 -33
- package/src/features/User/UserPanel/PanelContent.tsx +0 -8
- package/src/features/User/__tests__/PanelContent.test.tsx +1 -35
- package/src/features/User/__tests__/UserAvatar.test.tsx +30 -57
- package/src/features/User/__tests__/useMenu.test.tsx +2 -43
- package/src/layout/AuthProvider/index.tsx +0 -5
- package/src/libs/next/config/define-config.ts +20 -15
- package/src/libs/next/proxy/createRouteMatcher.test.ts +121 -0
- package/src/libs/next/proxy/createRouteMatcher.ts +18 -0
- package/src/libs/next/proxy/define-config.ts +4 -53
- package/src/libs/next-auth/adapter/index.ts +1 -2
- package/src/libs/oidc-provider/provider.test.ts +5 -316
- package/src/libs/pdfjs/pdf.worker.ts +1 -0
- package/src/libs/pdfjs/worker.ts +12 -0
- package/src/libs/trpc/lambda/context.test.ts +0 -13
- package/src/libs/trpc/lambda/context.ts +3 -22
- package/src/libs/trpc/middleware/userAuth.ts +2 -4
- package/src/libs/trusted-client/getSessionUser.ts +2 -17
- package/src/locales/default/error.ts +0 -6
- package/src/locales/default/index.ts +0 -2
- package/src/proxy.ts +0 -1
- package/src/server/routers/lambda/__tests__/user.test.ts +0 -71
- package/src/server/routers/lambda/user.ts +6 -63
- package/src/server/services/changelog/index.test.ts +3 -2
- package/src/server/services/changelog/index.ts +1 -1
- package/src/server/services/user/index.ts +0 -83
- package/src/services/chat/index.ts +1 -2
- package/src/services/chat/mecha/agentConfigResolver.test.ts +43 -0
- package/src/services/chat/mecha/agentConfigResolver.ts +3 -1
- package/src/store/chat/slices/aiChat/actions/__tests__/streamingExecutor.test.ts +58 -14
- package/src/store/chat/slices/aiChat/actions/streamingExecutor.ts +10 -2
- package/src/store/user/slices/auth/action.test.ts +1 -81
- package/src/store/user/slices/auth/action.ts +3 -28
- package/src/store/user/slices/auth/initialState.ts +1 -18
- package/src/store/user/slices/auth/selectors.test.ts +2 -127
- package/src/store/user/slices/auth/selectors.ts +1 -21
- package/src/utils/errorResponse.ts +1 -4
- package/src/utils/markdownToTxt.ts +20 -0
- package/locales/ar/clerk.json +0 -545
- package/locales/bg-BG/clerk.json +0 -545
- package/locales/de-DE/clerk.json +0 -545
- package/locales/en-US/clerk.json +0 -545
- package/locales/es-ES/clerk.json +0 -545
- package/locales/fa-IR/clerk.json +0 -545
- package/locales/fr-FR/clerk.json +0 -545
- package/locales/it-IT/clerk.json +0 -545
- package/locales/ja-JP/clerk.json +0 -545
- package/locales/ko-KR/clerk.json +0 -545
- package/locales/nl-NL/clerk.json +0 -545
- package/locales/pl-PL/clerk.json +0 -545
- package/locales/pt-BR/clerk.json +0 -545
- package/locales/ru-RU/clerk.json +0 -545
- package/locales/tr-TR/clerk.json +0 -545
- package/locales/vi-VN/clerk.json +0 -545
- package/locales/zh-CN/clerk.json +0 -545
- package/locales/zh-TW/clerk.json +0 -545
- package/src/app/(backend)/api/webhooks/clerk/__tests__/fixtures/createUser.json +0 -73
- package/src/app/(backend)/api/webhooks/clerk/route.ts +0 -95
- package/src/app/(backend)/api/webhooks/clerk/validateRequest.ts +0 -22
- package/src/app/[variants]/(auth)/login/[[...login]]/page.tsx +0 -27
- package/src/app/[variants]/(main)/settings/security/features/ClerkProfile.tsx +0 -67
- package/src/features/Conversation/ChatList/components/AutoScroll.tsx +0 -25
- package/src/layout/AuthProvider/Clerk/UserUpdater.tsx +0 -40
- package/src/layout/AuthProvider/Clerk/index.tsx +0 -54
- package/src/layout/AuthProvider/Clerk/useAppearance.ts +0 -133
- package/src/libs/clerk-auth/index.test.ts +0 -216
- package/src/libs/clerk-auth/index.ts +0 -80
- package/src/locales/default/clerk.ts +0 -677
- package/src/server/services/user/index.test.ts +0 -220
|
@@ -1,216 +0,0 @@
|
|
|
1
|
-
import { auth, getAuth } from '@clerk/nextjs/server';
|
|
2
|
-
import { NextRequest } from 'next/server';
|
|
3
|
-
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
4
|
-
|
|
5
|
-
import { ClerkAuth } from './index';
|
|
6
|
-
|
|
7
|
-
// 模拟 @clerk/nextjs/server 模块
|
|
8
|
-
vi.mock('@clerk/nextjs/server', () => ({
|
|
9
|
-
auth: vi.fn(),
|
|
10
|
-
getAuth: vi.fn(),
|
|
11
|
-
}));
|
|
12
|
-
|
|
13
|
-
// 模拟 process.env
|
|
14
|
-
const originalEnv = { ...process.env };
|
|
15
|
-
|
|
16
|
-
beforeEach(() => {
|
|
17
|
-
// 重置所有模拟
|
|
18
|
-
vi.resetAllMocks();
|
|
19
|
-
|
|
20
|
-
// 重置环境变量
|
|
21
|
-
process.env = { ...originalEnv };
|
|
22
|
-
Object.assign(process.env, { NODE_ENV: 'development' });
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
afterEach(() => {
|
|
26
|
-
// 恢复环境变量
|
|
27
|
-
process.env = originalEnv;
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
describe('ClerkAuth', () => {
|
|
31
|
-
describe('constructor', () => {
|
|
32
|
-
it('should parse user ID mapping from environment variable', () => {
|
|
33
|
-
process.env.CLERK_DEV_IMPERSONATE_USER = 'dev_user=prod_user';
|
|
34
|
-
const clerkAuth = new ClerkAuth();
|
|
35
|
-
|
|
36
|
-
// 使用私有属性测试,需要使用类型断言
|
|
37
|
-
expect(clerkAuth['devUserId']).toBe('dev_user');
|
|
38
|
-
expect(clerkAuth['prodUserId']).toBe('prod_user');
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
it('should handle empty mapping string', () => {
|
|
42
|
-
process.env.CLERK_DEV_IMPERSONATE_USER = '';
|
|
43
|
-
const clerkAuth = new ClerkAuth();
|
|
44
|
-
|
|
45
|
-
expect((clerkAuth as any).devUserId).toBeNull();
|
|
46
|
-
expect((clerkAuth as any).prodUserId).toBeNull();
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
it('should handle invalid mapping format', () => {
|
|
50
|
-
process.env.CLERK_DEV_IMPERSONATE_USER = 'invalid_format';
|
|
51
|
-
const clerkAuth = new ClerkAuth();
|
|
52
|
-
|
|
53
|
-
expect((clerkAuth as any).devUserId).toBeNull();
|
|
54
|
-
expect((clerkAuth as any).prodUserId).toBeNull();
|
|
55
|
-
});
|
|
56
|
-
|
|
57
|
-
it('should handle undefined mapping', () => {
|
|
58
|
-
delete process.env.CLERK_DEV_IMPERSONATE_USER;
|
|
59
|
-
const clerkAuth = new ClerkAuth();
|
|
60
|
-
|
|
61
|
-
expect((clerkAuth as any).devUserId).toBeNull();
|
|
62
|
-
expect((clerkAuth as any).prodUserId).toBeNull();
|
|
63
|
-
});
|
|
64
|
-
});
|
|
65
|
-
|
|
66
|
-
describe('getAuthFromRequest', () => {
|
|
67
|
-
it('should get auth from request and return original user ID when no mapping', () => {
|
|
68
|
-
// 设置模拟返回值
|
|
69
|
-
vi.mocked(getAuth).mockReturnValue({ userId: 'original_user_id' } as any);
|
|
70
|
-
|
|
71
|
-
const clerkAuth = new ClerkAuth();
|
|
72
|
-
const mockRequest = {} as NextRequest;
|
|
73
|
-
const result = clerkAuth.getAuthFromRequest(mockRequest);
|
|
74
|
-
|
|
75
|
-
expect(getAuth).toHaveBeenCalledWith(mockRequest);
|
|
76
|
-
expect(result).toEqual({
|
|
77
|
-
clerkAuth: { userId: 'original_user_id' },
|
|
78
|
-
userId: 'original_user_id',
|
|
79
|
-
});
|
|
80
|
-
});
|
|
81
|
-
|
|
82
|
-
it('should map user ID in development environment', () => {
|
|
83
|
-
// 设置环境和模拟
|
|
84
|
-
process.env.CLERK_DEV_IMPERSONATE_USER = 'dev_user=prod_user';
|
|
85
|
-
Object.assign(process.env, { NODE_ENV: 'development' });
|
|
86
|
-
vi.mocked(getAuth).mockReturnValue({ userId: 'dev_user' } as any);
|
|
87
|
-
|
|
88
|
-
const clerkAuth = new ClerkAuth();
|
|
89
|
-
const result = clerkAuth.getAuthFromRequest({} as NextRequest);
|
|
90
|
-
|
|
91
|
-
expect(result).toEqual({
|
|
92
|
-
clerkAuth: { userId: 'dev_user' },
|
|
93
|
-
userId: 'prod_user',
|
|
94
|
-
});
|
|
95
|
-
});
|
|
96
|
-
|
|
97
|
-
it('should not map user ID in production environment', () => {
|
|
98
|
-
// 设置环境和模拟
|
|
99
|
-
process.env.CLERK_DEV_IMPERSONATE_USER = 'dev_user=prod_user';
|
|
100
|
-
Object.assign(process.env, { NODE_ENV: 'production' });
|
|
101
|
-
|
|
102
|
-
vi.mocked(getAuth).mockReturnValue({ userId: 'dev_user' } as any);
|
|
103
|
-
|
|
104
|
-
const clerkAuth = new ClerkAuth();
|
|
105
|
-
const result = clerkAuth.getAuthFromRequest({} as NextRequest);
|
|
106
|
-
|
|
107
|
-
expect(result).toEqual({
|
|
108
|
-
clerkAuth: { userId: 'dev_user' },
|
|
109
|
-
userId: 'dev_user',
|
|
110
|
-
});
|
|
111
|
-
});
|
|
112
|
-
|
|
113
|
-
it('should handle null user ID', () => {
|
|
114
|
-
vi.mocked(getAuth).mockReturnValue({ userId: null } as any);
|
|
115
|
-
|
|
116
|
-
const clerkAuth = new ClerkAuth();
|
|
117
|
-
const result = clerkAuth.getAuthFromRequest({} as NextRequest);
|
|
118
|
-
|
|
119
|
-
expect(result).toEqual({
|
|
120
|
-
clerkAuth: { userId: null },
|
|
121
|
-
userId: null,
|
|
122
|
-
});
|
|
123
|
-
});
|
|
124
|
-
});
|
|
125
|
-
|
|
126
|
-
describe('getAuth', () => {
|
|
127
|
-
it('should get auth and return original user ID when no mapping', async () => {
|
|
128
|
-
vi.mocked(auth).mockResolvedValue({ userId: 'original_user_id' } as any);
|
|
129
|
-
|
|
130
|
-
const clerkAuth = new ClerkAuth();
|
|
131
|
-
const result = await clerkAuth.getAuth();
|
|
132
|
-
|
|
133
|
-
expect(auth).toHaveBeenCalled();
|
|
134
|
-
expect(result).toEqual({
|
|
135
|
-
clerkAuth: { userId: 'original_user_id' },
|
|
136
|
-
userId: 'original_user_id',
|
|
137
|
-
});
|
|
138
|
-
});
|
|
139
|
-
|
|
140
|
-
it('should map user ID in development environment', async () => {
|
|
141
|
-
process.env.CLERK_DEV_IMPERSONATE_USER = 'dev_user=prod_user';
|
|
142
|
-
Object.assign(process.env, { NODE_ENV: 'development' });
|
|
143
|
-
vi.mocked(auth).mockResolvedValue({ userId: 'dev_user' } as any);
|
|
144
|
-
|
|
145
|
-
const clerkAuth = new ClerkAuth();
|
|
146
|
-
const result = await clerkAuth.getAuth();
|
|
147
|
-
|
|
148
|
-
expect(result).toEqual({
|
|
149
|
-
clerkAuth: { userId: 'dev_user' },
|
|
150
|
-
userId: 'prod_user',
|
|
151
|
-
});
|
|
152
|
-
});
|
|
153
|
-
|
|
154
|
-
it('should not map user ID in production environment', async () => {
|
|
155
|
-
process.env.CLERK_DEV_IMPERSONATE_USER = 'dev_user=prod_user';
|
|
156
|
-
Object.assign(process.env, { NODE_ENV: 'production' });
|
|
157
|
-
vi.mocked(auth).mockResolvedValue({ userId: 'dev_user' } as any);
|
|
158
|
-
|
|
159
|
-
const clerkAuth = new ClerkAuth();
|
|
160
|
-
const result = await clerkAuth.getAuth();
|
|
161
|
-
|
|
162
|
-
expect(result).toEqual({
|
|
163
|
-
clerkAuth: { userId: 'dev_user' },
|
|
164
|
-
userId: 'dev_user',
|
|
165
|
-
});
|
|
166
|
-
});
|
|
167
|
-
|
|
168
|
-
it('should handle null user ID', async () => {
|
|
169
|
-
vi.mocked(auth).mockResolvedValue({ userId: null } as any);
|
|
170
|
-
|
|
171
|
-
const clerkAuth = new ClerkAuth();
|
|
172
|
-
const result = await clerkAuth.getAuth();
|
|
173
|
-
|
|
174
|
-
expect(result).toEqual({
|
|
175
|
-
clerkAuth: { userId: null },
|
|
176
|
-
userId: null,
|
|
177
|
-
});
|
|
178
|
-
});
|
|
179
|
-
});
|
|
180
|
-
|
|
181
|
-
describe('getMappedUserId', () => {
|
|
182
|
-
it('should return null for null input', () => {
|
|
183
|
-
const clerkAuth = new ClerkAuth();
|
|
184
|
-
const result = (clerkAuth as any).getMappedUserId(null);
|
|
185
|
-
|
|
186
|
-
expect(result).toBeNull();
|
|
187
|
-
});
|
|
188
|
-
|
|
189
|
-
it('should return original ID when no mapping exists', () => {
|
|
190
|
-
const clerkAuth = new ClerkAuth();
|
|
191
|
-
const result = (clerkAuth as any).getMappedUserId('some_user_id');
|
|
192
|
-
|
|
193
|
-
expect(result).toBe('some_user_id');
|
|
194
|
-
});
|
|
195
|
-
|
|
196
|
-
it('should return mapped ID when matching dev ID in development', () => {
|
|
197
|
-
process.env.CLERK_DEV_IMPERSONATE_USER = 'dev_user=prod_user';
|
|
198
|
-
Object.assign(process.env, { NODE_ENV: 'development' });
|
|
199
|
-
|
|
200
|
-
const clerkAuth = new ClerkAuth();
|
|
201
|
-
const result = (clerkAuth as any).getMappedUserId('dev_user');
|
|
202
|
-
|
|
203
|
-
expect(result).toBe('prod_user');
|
|
204
|
-
});
|
|
205
|
-
|
|
206
|
-
it('should return original ID when not matching dev ID', () => {
|
|
207
|
-
process.env.CLERK_DEV_IMPERSONATE_USER = 'dev_user=prod_user';
|
|
208
|
-
Object.assign(process.env, { NODE_ENV: 'development' });
|
|
209
|
-
|
|
210
|
-
const clerkAuth = new ClerkAuth();
|
|
211
|
-
const result = (clerkAuth as any).getMappedUserId('other_user');
|
|
212
|
-
|
|
213
|
-
expect(result).toBe('other_user');
|
|
214
|
-
});
|
|
215
|
-
});
|
|
216
|
-
});
|
|
@@ -1,80 +0,0 @@
|
|
|
1
|
-
import { auth, currentUser, getAuth } from '@clerk/nextjs/server';
|
|
2
|
-
import type { NextRequest } from 'next/server';
|
|
3
|
-
|
|
4
|
-
export class ClerkAuth {
|
|
5
|
-
private devUserId: string | null = null;
|
|
6
|
-
private prodUserId: string | null = null;
|
|
7
|
-
|
|
8
|
-
constructor() {
|
|
9
|
-
this.parseUserIdMapping();
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* 从请求中获取认证信息和用户ID
|
|
14
|
-
*/
|
|
15
|
-
getAuthFromRequest(request: NextRequest) {
|
|
16
|
-
const clerkAuth = getAuth(request);
|
|
17
|
-
const userId = this.getMappedUserId(clerkAuth.userId);
|
|
18
|
-
|
|
19
|
-
return { clerkAuth, userId };
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* 获取当前认证信息和用户ID
|
|
24
|
-
*/
|
|
25
|
-
async getAuth() {
|
|
26
|
-
const clerkAuth = await auth();
|
|
27
|
-
const userId = this.getMappedUserId(clerkAuth.userId);
|
|
28
|
-
|
|
29
|
-
return { clerkAuth, userId };
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
async getCurrentUser() {
|
|
33
|
-
const user = await currentUser();
|
|
34
|
-
|
|
35
|
-
if (!user) return null;
|
|
36
|
-
|
|
37
|
-
const userId = this.getMappedUserId(user.id) as string;
|
|
38
|
-
|
|
39
|
-
return { ...user, id: userId };
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* 根据环境变量映射用户ID
|
|
44
|
-
*/
|
|
45
|
-
private getMappedUserId(originalUserId: string | null): string | null {
|
|
46
|
-
if (!originalUserId) return null;
|
|
47
|
-
|
|
48
|
-
// 只在开发环境下执行映射
|
|
49
|
-
if (
|
|
50
|
-
process.env.NODE_ENV === 'development' &&
|
|
51
|
-
this.devUserId &&
|
|
52
|
-
this.prodUserId &&
|
|
53
|
-
originalUserId === this.devUserId
|
|
54
|
-
) {
|
|
55
|
-
return this.prodUserId;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
return originalUserId;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* 解析环境变量中的用户ID映射配置
|
|
63
|
-
* 格式: "dev=prod"
|
|
64
|
-
*/
|
|
65
|
-
private parseUserIdMapping(): void {
|
|
66
|
-
const mappingStr = process.env.CLERK_DEV_IMPERSONATE_USER || '';
|
|
67
|
-
|
|
68
|
-
if (!mappingStr) return;
|
|
69
|
-
|
|
70
|
-
const [dev, prod] = mappingStr.split('=');
|
|
71
|
-
if (dev && prod) {
|
|
72
|
-
this.devUserId = dev.trim();
|
|
73
|
-
this.prodUserId = prod.trim();
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
export type IClerkAuth = ReturnType<typeof getAuth>;
|
|
79
|
-
|
|
80
|
-
export const clerkAuth = new ClerkAuth();
|