We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
2 parents fb87c27 + e2ae9df commit ec7866eCopy full SHA for ec7866e
1 file changed
frontend/src/lib/auth.ts
@@ -1,4 +1,20 @@
1
-export function getAuthHeader() {
2
- const key = localStorage.getItem('qwen2api_key') || 'admin';
3
- return { Authorization: `Bearer ${key}` };
+/**
+ * 读取当前会话凭证。
+ * - 优先使用 localStorage 中用户显式保存的 key
4
+ * - 仅在没有任何配置时回退到 "admin",避免页面切换/刷新后静默丢失凭证
5
+ */
6
+export function getStoredApiKey(): string {
7
+ try {
8
+ const stored = localStorage.getItem('qwen2api_key')
9
+ if (stored && stored.trim()) return stored.trim()
10
+ } catch {
11
+ // localStorage 不可用时继续走默认值
12
+ }
13
+ return 'admin'
14
+}
15
+
16
+export function getAuthHeader(): Record<string, string> {
17
+ const key = getStoredApiKey()
18
+ if (!key) return {}
19
+ return { Authorization: `Bearer ${key}` }
20
}
0 commit comments