@windrun-huaiin/third-ui 7.3.6 → 7.3.8

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.
@@ -5,43 +5,35 @@
5
5
  */
6
6
  /**
7
7
  * 生成基于真实浏览器特征的fingerprint ID
8
- * 使用FingerprintJS收集浏览器特征并生成唯一标识
9
- * 只能在客户端使用
8
+ * 使用 FingerprintJS 收集浏览器特征并生成唯一标识
10
9
  */
11
10
  export declare function generateFingerprintId(): Promise<string>;
12
11
  /**
13
12
  * 获取当前的fingerprint ID
14
- * 只能在客户端使用
15
13
  */
16
14
  export declare function getFingerprintId(): string | null;
17
15
  /**
18
16
  * 设置fingerprint ID到存储
19
- * 只能在客户端使用
20
17
  */
21
18
  export declare function setFingerprintId(fingerprintId: string): void;
22
19
  /**
23
20
  * 清除fingerprint ID
24
- * 只能在客户端使用
25
21
  */
26
22
  export declare function clearFingerprintId(): void;
27
23
  /**
28
24
  * 获取或生成fingerprint ID
29
25
  * 如果不存在则自动生成新的
30
- * 只能在客户端使用
31
26
  */
32
27
  export declare function getOrGenerateFingerprintId(): Promise<string>;
33
28
  /**
34
29
  * 创建包含fingerprint ID的fetch headers
35
- * 只能在客户端使用
36
30
  */
37
31
  export declare function createFingerprintHeaders(): Promise<Record<string, string>>;
38
32
  /**
39
33
  * Hook for generating fingerprint headers
40
- * 只能在客户端使用
41
34
  */
42
35
  export declare function useFingerprintHeaders(): () => Promise<Record<string, string>>;
43
36
  /**
44
37
  * Create a fetch wrapper that automatically includes fingerprint headers
45
- * 只能在客户端使用
46
38
  */
47
39
  export declare function createFingerprintFetch(): (url: string | URL | Request, init?: RequestInit) => Promise<Response>;
@@ -1,92 +1,94 @@
1
1
  'use strict';
2
2
 
3
3
  var tslib_es6 = require('../../node_modules/.pnpm/@rollup_plugin-typescript@12.1.4_rollup@4.46.2_tslib@2.8.1_typescript@5.9.2/node_modules/tslib/tslib.es6.js');
4
- var fingerprintShared = require('./fingerprint-shared.js');
5
4
  var fp_esm = require('../../node_modules/.pnpm/@fingerprintjs_fingerprintjs@4.6.2/node_modules/@fingerprintjs/fingerprintjs/dist/fp.esm.js');
5
+ var fingerprintShared = require('./fingerprint-shared.js');
6
6
 
7
7
  /**
8
8
  * Fingerprint Client Utilities
9
9
  * 客户端专用的指纹生成和管理逻辑
10
10
  * 只能在浏览器环境中使用
11
11
  */
12
+ /**
13
+ * 检查浏览器存储(localStorage 和 cookie)中的指纹 ID
14
+ * 返回有效的 ID 或 null
15
+ */
16
+ function checkStoredFingerprintId() {
17
+ if (typeof window === 'undefined') {
18
+ return null;
19
+ }
20
+ // 优先检查 localStorage
21
+ const localStorageId = localStorage.getItem(fingerprintShared.FINGERPRINT_STORAGE_KEY);
22
+ if (localStorageId && fingerprintShared.isValidFingerprintId(localStorageId)) {
23
+ return localStorageId;
24
+ }
25
+ // 检查 cookie
26
+ const cookieId = getCookieValue(fingerprintShared.FINGERPRINT_COOKIE_NAME);
27
+ if (cookieId && fingerprintShared.isValidFingerprintId(cookieId)) {
28
+ // 同步到 localStorage
29
+ localStorage.setItem(fingerprintShared.FINGERPRINT_STORAGE_KEY, cookieId);
30
+ return cookieId;
31
+ }
32
+ return null;
33
+ }
12
34
  /**
13
35
  * 生成基于真实浏览器特征的fingerprint ID
14
- * 使用FingerprintJS收集浏览器特征并生成唯一标识
15
- * 只能在客户端使用
36
+ * 使用 FingerprintJS 收集浏览器特征并生成唯一标识
16
37
  */
17
38
  function generateFingerprintId() {
18
39
  return tslib_es6.__awaiter(this, void 0, void 0, function* () {
19
40
  if (typeof window === 'undefined') {
20
41
  throw new Error('generateFingerprintId can only be used in browser environment');
21
42
  }
22
- // 首先检查现有ID
23
- const existingId = getFingerprintId();
24
- if (existingId && fingerprintShared.isValidFingerprintId(existingId)) {
43
+ // 检查现有 ID
44
+ const existingId = checkStoredFingerprintId();
45
+ if (existingId) {
46
+ console.log('Using existing fingerprint ID:', existingId);
25
47
  return existingId;
26
48
  }
27
- // 检查cookie
28
- const cookieId = getCookieValue(fingerprintShared.FINGERPRINT_COOKIE_NAME);
29
- if (cookieId && fingerprintShared.isValidFingerprintId(cookieId)) {
30
- // 同步到localStorage
31
- localStorage.setItem(fingerprintShared.FINGERPRINT_STORAGE_KEY, cookieId);
32
- return cookieId;
33
- }
34
49
  try {
35
- // 使用FingerprintJS生成基于浏览器特征的指纹
50
+ // 使用 FingerprintJS 生成指纹
36
51
  const fp = yield fp_esm.default.load();
37
52
  const result = yield fp.get();
38
53
  const fingerprintId = `fp_${result.visitorId}`;
39
- // 存储到localStorage和cookie
54
+ // 存储到 localStorage cookie
40
55
  localStorage.setItem(fingerprintShared.FINGERPRINT_STORAGE_KEY, fingerprintId);
41
- setCookie(fingerprintShared.FINGERPRINT_COOKIE_NAME, fingerprintId, 365); // 365天过期
56
+ setCookie(fingerprintShared.FINGERPRINT_COOKIE_NAME, fingerprintId, 365);
57
+ console.log('Generated new fingerprint ID:', fingerprintId);
42
58
  return fingerprintId;
43
59
  }
44
60
  catch (error) {
45
61
  console.warn('Failed to generate fingerprint with FingerprintJS:', error);
46
- // 降级方案:生成时间戳+随机数
62
+ // 降级方案:生成基于时间戳和随机数的 ID
47
63
  const fallbackId = `fp_fallback_${Date.now()}_${Math.random().toString(36).slice(2, 11)}`;
48
64
  localStorage.setItem(fingerprintShared.FINGERPRINT_STORAGE_KEY, fallbackId);
49
65
  setCookie(fingerprintShared.FINGERPRINT_COOKIE_NAME, fallbackId, 365);
66
+ console.log('Generated fallback fingerprint ID:', fallbackId);
50
67
  return fallbackId;
51
68
  }
52
69
  });
53
70
  }
54
71
  /**
55
72
  * 获取当前的fingerprint ID
56
- * 只能在客户端使用
57
73
  */
58
74
  function getFingerprintId() {
59
- if (typeof window === 'undefined') {
60
- return null;
61
- }
62
- // 首先检查localStorage
63
- const localStorageId = localStorage.getItem(fingerprintShared.FINGERPRINT_STORAGE_KEY);
64
- if (localStorageId) {
65
- return localStorageId;
66
- }
67
- // 检查cookie
68
- const cookieId = getCookieValue(fingerprintShared.FINGERPRINT_COOKIE_NAME);
69
- if (cookieId) {
70
- // 同步到localStorage
71
- localStorage.setItem(fingerprintShared.FINGERPRINT_STORAGE_KEY, cookieId);
72
- return cookieId;
73
- }
74
- return null;
75
+ return checkStoredFingerprintId();
75
76
  }
76
77
  /**
77
78
  * 设置fingerprint ID到存储
78
- * 只能在客户端使用
79
79
  */
80
80
  function setFingerprintId(fingerprintId) {
81
81
  if (typeof window === 'undefined') {
82
82
  throw new Error('setFingerprintId can only be used in browser environment');
83
83
  }
84
+ if (!fingerprintShared.isValidFingerprintId(fingerprintId)) {
85
+ throw new Error('Invalid fingerprint ID');
86
+ }
84
87
  localStorage.setItem(fingerprintShared.FINGERPRINT_STORAGE_KEY, fingerprintId);
85
88
  setCookie(fingerprintShared.FINGERPRINT_COOKIE_NAME, fingerprintId, 365);
86
89
  }
87
90
  /**
88
91
  * 清除fingerprint ID
89
- * 只能在客户端使用
90
92
  */
91
93
  function clearFingerprintId() {
92
94
  if (typeof window === 'undefined') {
@@ -98,12 +100,12 @@ function clearFingerprintId() {
98
100
  /**
99
101
  * 获取或生成fingerprint ID
100
102
  * 如果不存在则自动生成新的
101
- * 只能在客户端使用
102
103
  */
103
104
  function getOrGenerateFingerprintId() {
104
105
  return tslib_es6.__awaiter(this, void 0, void 0, function* () {
105
- const existingId = getFingerprintId();
106
+ const existingId = checkStoredFingerprintId();
106
107
  if (existingId) {
108
+ console.log('Retrieved existing fingerprint ID:', existingId);
107
109
  return existingId;
108
110
  }
109
111
  return yield generateFingerprintId();
@@ -111,7 +113,6 @@ function getOrGenerateFingerprintId() {
111
113
  }
112
114
  /**
113
115
  * 创建包含fingerprint ID的fetch headers
114
- * 只能在客户端使用
115
116
  */
116
117
  function createFingerprintHeaders() {
117
118
  return tslib_es6.__awaiter(this, void 0, void 0, function* () {
@@ -123,14 +124,12 @@ function createFingerprintHeaders() {
123
124
  }
124
125
  /**
125
126
  * Hook for generating fingerprint headers
126
- * 只能在客户端使用
127
127
  */
128
128
  function useFingerprintHeaders() {
129
129
  return createFingerprintHeaders;
130
130
  }
131
131
  /**
132
132
  * Create a fetch wrapper that automatically includes fingerprint headers
133
- * 只能在客户端使用
134
133
  */
135
134
  function createFingerprintFetch() {
136
135
  return (url, init) => tslib_es6.__awaiter(this, void 0, void 0, function* () {
@@ -1,90 +1,92 @@
1
1
  import { __awaiter } from '../../node_modules/.pnpm/@rollup_plugin-typescript@12.1.4_rollup@4.46.2_tslib@2.8.1_typescript@5.9.2/node_modules/tslib/tslib.es6.mjs';
2
- import { FINGERPRINT_STORAGE_KEY, isValidFingerprintId, FINGERPRINT_COOKIE_NAME } from './fingerprint-shared.mjs';
3
2
  import index from '../../node_modules/.pnpm/@fingerprintjs_fingerprintjs@4.6.2/node_modules/@fingerprintjs/fingerprintjs/dist/fp.esm.mjs';
3
+ import { isValidFingerprintId, FINGERPRINT_STORAGE_KEY, FINGERPRINT_COOKIE_NAME } from './fingerprint-shared.mjs';
4
4
 
5
5
  /**
6
6
  * Fingerprint Client Utilities
7
7
  * 客户端专用的指纹生成和管理逻辑
8
8
  * 只能在浏览器环境中使用
9
9
  */
10
+ /**
11
+ * 检查浏览器存储(localStorage 和 cookie)中的指纹 ID
12
+ * 返回有效的 ID 或 null
13
+ */
14
+ function checkStoredFingerprintId() {
15
+ if (typeof window === 'undefined') {
16
+ return null;
17
+ }
18
+ // 优先检查 localStorage
19
+ const localStorageId = localStorage.getItem(FINGERPRINT_STORAGE_KEY);
20
+ if (localStorageId && isValidFingerprintId(localStorageId)) {
21
+ return localStorageId;
22
+ }
23
+ // 检查 cookie
24
+ const cookieId = getCookieValue(FINGERPRINT_COOKIE_NAME);
25
+ if (cookieId && isValidFingerprintId(cookieId)) {
26
+ // 同步到 localStorage
27
+ localStorage.setItem(FINGERPRINT_STORAGE_KEY, cookieId);
28
+ return cookieId;
29
+ }
30
+ return null;
31
+ }
10
32
  /**
11
33
  * 生成基于真实浏览器特征的fingerprint ID
12
- * 使用FingerprintJS收集浏览器特征并生成唯一标识
13
- * 只能在客户端使用
34
+ * 使用 FingerprintJS 收集浏览器特征并生成唯一标识
14
35
  */
15
36
  function generateFingerprintId() {
16
37
  return __awaiter(this, void 0, void 0, function* () {
17
38
  if (typeof window === 'undefined') {
18
39
  throw new Error('generateFingerprintId can only be used in browser environment');
19
40
  }
20
- // 首先检查现有ID
21
- const existingId = getFingerprintId();
22
- if (existingId && isValidFingerprintId(existingId)) {
41
+ // 检查现有 ID
42
+ const existingId = checkStoredFingerprintId();
43
+ if (existingId) {
44
+ console.log('Using existing fingerprint ID:', existingId);
23
45
  return existingId;
24
46
  }
25
- // 检查cookie
26
- const cookieId = getCookieValue(FINGERPRINT_COOKIE_NAME);
27
- if (cookieId && isValidFingerprintId(cookieId)) {
28
- // 同步到localStorage
29
- localStorage.setItem(FINGERPRINT_STORAGE_KEY, cookieId);
30
- return cookieId;
31
- }
32
47
  try {
33
- // 使用FingerprintJS生成基于浏览器特征的指纹
48
+ // 使用 FingerprintJS 生成指纹
34
49
  const fp = yield index.load();
35
50
  const result = yield fp.get();
36
51
  const fingerprintId = `fp_${result.visitorId}`;
37
- // 存储到localStorage和cookie
52
+ // 存储到 localStorage cookie
38
53
  localStorage.setItem(FINGERPRINT_STORAGE_KEY, fingerprintId);
39
- setCookie(FINGERPRINT_COOKIE_NAME, fingerprintId, 365); // 365天过期
54
+ setCookie(FINGERPRINT_COOKIE_NAME, fingerprintId, 365);
55
+ console.log('Generated new fingerprint ID:', fingerprintId);
40
56
  return fingerprintId;
41
57
  }
42
58
  catch (error) {
43
59
  console.warn('Failed to generate fingerprint with FingerprintJS:', error);
44
- // 降级方案:生成时间戳+随机数
60
+ // 降级方案:生成基于时间戳和随机数的 ID
45
61
  const fallbackId = `fp_fallback_${Date.now()}_${Math.random().toString(36).slice(2, 11)}`;
46
62
  localStorage.setItem(FINGERPRINT_STORAGE_KEY, fallbackId);
47
63
  setCookie(FINGERPRINT_COOKIE_NAME, fallbackId, 365);
64
+ console.log('Generated fallback fingerprint ID:', fallbackId);
48
65
  return fallbackId;
49
66
  }
50
67
  });
51
68
  }
52
69
  /**
53
70
  * 获取当前的fingerprint ID
54
- * 只能在客户端使用
55
71
  */
56
72
  function getFingerprintId() {
57
- if (typeof window === 'undefined') {
58
- return null;
59
- }
60
- // 首先检查localStorage
61
- const localStorageId = localStorage.getItem(FINGERPRINT_STORAGE_KEY);
62
- if (localStorageId) {
63
- return localStorageId;
64
- }
65
- // 检查cookie
66
- const cookieId = getCookieValue(FINGERPRINT_COOKIE_NAME);
67
- if (cookieId) {
68
- // 同步到localStorage
69
- localStorage.setItem(FINGERPRINT_STORAGE_KEY, cookieId);
70
- return cookieId;
71
- }
72
- return null;
73
+ return checkStoredFingerprintId();
73
74
  }
74
75
  /**
75
76
  * 设置fingerprint ID到存储
76
- * 只能在客户端使用
77
77
  */
78
78
  function setFingerprintId(fingerprintId) {
79
79
  if (typeof window === 'undefined') {
80
80
  throw new Error('setFingerprintId can only be used in browser environment');
81
81
  }
82
+ if (!isValidFingerprintId(fingerprintId)) {
83
+ throw new Error('Invalid fingerprint ID');
84
+ }
82
85
  localStorage.setItem(FINGERPRINT_STORAGE_KEY, fingerprintId);
83
86
  setCookie(FINGERPRINT_COOKIE_NAME, fingerprintId, 365);
84
87
  }
85
88
  /**
86
89
  * 清除fingerprint ID
87
- * 只能在客户端使用
88
90
  */
89
91
  function clearFingerprintId() {
90
92
  if (typeof window === 'undefined') {
@@ -96,12 +98,12 @@ function clearFingerprintId() {
96
98
  /**
97
99
  * 获取或生成fingerprint ID
98
100
  * 如果不存在则自动生成新的
99
- * 只能在客户端使用
100
101
  */
101
102
  function getOrGenerateFingerprintId() {
102
103
  return __awaiter(this, void 0, void 0, function* () {
103
- const existingId = getFingerprintId();
104
+ const existingId = checkStoredFingerprintId();
104
105
  if (existingId) {
106
+ console.log('Retrieved existing fingerprint ID:', existingId);
105
107
  return existingId;
106
108
  }
107
109
  return yield generateFingerprintId();
@@ -109,7 +111,6 @@ function getOrGenerateFingerprintId() {
109
111
  }
110
112
  /**
111
113
  * 创建包含fingerprint ID的fetch headers
112
- * 只能在客户端使用
113
114
  */
114
115
  function createFingerprintHeaders() {
115
116
  return __awaiter(this, void 0, void 0, function* () {
@@ -121,14 +122,12 @@ function createFingerprintHeaders() {
121
122
  }
122
123
  /**
123
124
  * Hook for generating fingerprint headers
124
- * 只能在客户端使用
125
125
  */
126
126
  function useFingerprintHeaders() {
127
127
  return createFingerprintHeaders;
128
128
  }
129
129
  /**
130
130
  * Create a fetch wrapper that automatically includes fingerprint headers
131
- * 只能在客户端使用
132
131
  */
133
132
  function createFingerprintFetch() {
134
133
  return (url, init) => __awaiter(this, void 0, void 0, function* () {
@@ -57,7 +57,6 @@ function FingerprintStatus() {
57
57
  setIsOpen(false);
58
58
  }
59
59
  };
60
- // ESC键关闭弹框
61
60
  React.useEffect(() => {
62
61
  const handleEscKey = (e) => {
63
62
  if (e.key === 'Escape' && isOpen) {
@@ -71,6 +70,12 @@ function FingerprintStatus() {
71
70
  document.removeEventListener('keydown', handleEscKey);
72
71
  };
73
72
  }, [isOpen]);
73
+ // 确保 xUser 更新后才渲染内容
74
+ React.useEffect(() => {
75
+ if (xUser && !xUser.fingerprintId) {
76
+ console.warn('xUser.fingerprintId is missing:', xUser);
77
+ }
78
+ }, [xUser]);
74
79
  return (jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [jsxRuntime.jsx("button", { onClick: handleToggle, style: {
75
80
  position: 'fixed',
76
81
  top: '10px',
@@ -111,7 +116,7 @@ function FingerprintStatus() {
111
116
  zIndex: 9999,
112
117
  border: '1px solid #ccc',
113
118
  boxShadow: '0 4px 12px rgba(0, 0, 0, 0.2)',
114
- }, children: [jsxRuntime.jsx("h4", { style: { margin: '0 0 5px 0' }, children: "Fingerprint Debug" }), jsxRuntime.jsxs("div", { children: [jsxRuntime.jsx("strong", { children: "FP_ID:" }), " ", fingerprintId || 'None'] }), jsxRuntime.jsxs("div", { children: [jsxRuntime.jsx("strong", { children: "Loading:" }), " ", isLoading ? 'Yes' : 'No'] }), jsxRuntime.jsxs("div", { children: [jsxRuntime.jsx("strong", { children: "Initialized:" }), " ", isInitialized ? 'Yes' : 'No'] }), error && jsxRuntime.jsxs("div", { style: { color: 'red' }, children: [jsxRuntime.jsx("strong", { children: "Error:" }), " ", error] }), xUser && (jsxRuntime.jsxs("div", { children: [jsxRuntime.jsx("strong", { children: "user_id:" }), " ", xUser.userId, " ", jsxRuntime.jsx("br", {}), jsxRuntime.jsx("strong", { children: "clerk_user_id:" }), " ", xUser.clerkUserId || 'None', " ", jsxRuntime.jsx("br", {}), jsxRuntime.jsx("strong", { children: "email:" }), " ", xUser.email || 'None', " ", jsxRuntime.jsx("br", {})] })), xCredit && (jsxRuntime.jsxs("div", { children: [jsxRuntime.jsx("strong", { children: "Credits:" }), " ", xCredit.balanceFree, " Free + ", xCredit.balancePaid, " Paid = ", xCredit.totalBalance, " Total"] })), jsxRuntime.jsx("div", { children: xSubscription ? (jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [jsxRuntime.jsx("strong", { children: "user_id:" }), " ", xSubscription.userId, " ", jsxRuntime.jsx("br", {}), jsxRuntime.jsx("strong", { children: "pay_subscription_id:" }), " ", xSubscription.paySubscriptionId, " ", jsxRuntime.jsx("br", {}), jsxRuntime.jsx("strong", { children: "price_id:" }), " ", xSubscription.priceId || 'None', " ", jsxRuntime.jsx("br", {}), jsxRuntime.jsx("strong", { children: "price_name:" }), " ", xSubscription.priceName || 'None', " ", jsxRuntime.jsx("br", {}), jsxRuntime.jsx("strong", { children: "status:" }), " ", xSubscription.status || 'Free', " ", jsxRuntime.jsx("br", {}), jsxRuntime.jsx("strong", { children: "credits_allocated:" }), " ", xSubscription.creditsAllocated || '', " ", jsxRuntime.jsx("br", {}), jsxRuntime.jsx("strong", { children: "sub_period_start:" }), " ", xSubscription.subPeriodStart || '', " ", jsxRuntime.jsx("br", {}), jsxRuntime.jsx("strong", { children: "sub_period_end:" }), " ", xSubscription.subPeriodEnd || '', " ", jsxRuntime.jsx("br", {})] })) : (jsxRuntime.jsx("strong", { children: "No Subscription, Default as Hobby Plan" })) })] })] }))] }));
119
+ }, children: [jsxRuntime.jsx("h4", { style: { margin: '0 0 5px 0' }, children: "Fingerprint Debug" }), jsxRuntime.jsxs("div", { children: [jsxRuntime.jsx("strong", { children: "FP_ID:" }), " ", fingerprintId || 'None'] }), jsxRuntime.jsxs("div", { children: [jsxRuntime.jsx("strong", { children: "Loading:" }), " ", isLoading ? 'Yes' : 'No'] }), jsxRuntime.jsxs("div", { children: [jsxRuntime.jsx("strong", { children: "Initialized:" }), " ", isInitialized ? 'Yes' : 'No'] }), error && jsxRuntime.jsxs("div", { style: { color: 'red' }, children: [jsxRuntime.jsx("strong", { children: "Error:" }), " ", error] }), xUser && (jsxRuntime.jsxs("div", { children: [jsxRuntime.jsx("strong", { children: "user_id:" }), " ", xUser.userId || 'None', " ", jsxRuntime.jsx("br", {}), jsxRuntime.jsx("strong", { children: "fingerprintId:" }), " ", xUser.fingerprintId || 'None', " ", jsxRuntime.jsx("br", {}), jsxRuntime.jsx("strong", { children: "clerk_user_id:" }), " ", xUser.clerkUserId || 'None', " ", jsxRuntime.jsx("br", {}), jsxRuntime.jsx("strong", { children: "email:" }), " ", xUser.email || 'None', " ", jsxRuntime.jsx("br", {})] })), xCredit && (jsxRuntime.jsxs("div", { children: [jsxRuntime.jsx("strong", { children: "Credits:" }), " ", xCredit.balanceFree, " Free + ", xCredit.balancePaid, " Paid = ", xCredit.totalBalance, " Total"] })), jsxRuntime.jsx("div", { children: xSubscription ? (jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [jsxRuntime.jsx("strong", { children: "user_id:" }), " ", xSubscription.userId, " ", jsxRuntime.jsx("br", {}), jsxRuntime.jsx("strong", { children: "pay_subscription_id:" }), " ", xSubscription.paySubscriptionId || 'None', " ", jsxRuntime.jsx("br", {}), jsxRuntime.jsx("strong", { children: "price_id:" }), " ", xSubscription.priceId || 'None', " ", jsxRuntime.jsx("br", {}), jsxRuntime.jsx("strong", { children: "price_name:" }), " ", xSubscription.priceName || 'None', " ", jsxRuntime.jsx("br", {}), jsxRuntime.jsx("strong", { children: "status:" }), " ", xSubscription.status || 'Free', " ", jsxRuntime.jsx("br", {}), jsxRuntime.jsx("strong", { children: "credits_allocated:" }), " ", xSubscription.creditsAllocated || '', " ", jsxRuntime.jsx("br", {}), jsxRuntime.jsx("strong", { children: "sub_period_start:" }), " ", xSubscription.subPeriodStart || '', " ", jsxRuntime.jsx("br", {}), jsxRuntime.jsx("strong", { children: "sub_period_end:" }), " ", xSubscription.subPeriodEnd || '', " ", jsxRuntime.jsx("br", {})] })) : (jsxRuntime.jsx("strong", { children: "No Subscription, Default as Hobby Plan" })) })] })] }))] }));
115
120
  }
116
121
 
117
122
  exports.FingerprintProvider = FingerprintProvider;
@@ -55,7 +55,6 @@ function FingerprintStatus() {
55
55
  setIsOpen(false);
56
56
  }
57
57
  };
58
- // ESC键关闭弹框
59
58
  useEffect(() => {
60
59
  const handleEscKey = (e) => {
61
60
  if (e.key === 'Escape' && isOpen) {
@@ -69,6 +68,12 @@ function FingerprintStatus() {
69
68
  document.removeEventListener('keydown', handleEscKey);
70
69
  };
71
70
  }, [isOpen]);
71
+ // 确保 xUser 更新后才渲染内容
72
+ useEffect(() => {
73
+ if (xUser && !xUser.fingerprintId) {
74
+ console.warn('xUser.fingerprintId is missing:', xUser);
75
+ }
76
+ }, [xUser]);
72
77
  return (jsxs(Fragment, { children: [jsx("button", { onClick: handleToggle, style: {
73
78
  position: 'fixed',
74
79
  top: '10px',
@@ -109,7 +114,7 @@ function FingerprintStatus() {
109
114
  zIndex: 9999,
110
115
  border: '1px solid #ccc',
111
116
  boxShadow: '0 4px 12px rgba(0, 0, 0, 0.2)',
112
- }, children: [jsx("h4", { style: { margin: '0 0 5px 0' }, children: "Fingerprint Debug" }), jsxs("div", { children: [jsx("strong", { children: "FP_ID:" }), " ", fingerprintId || 'None'] }), jsxs("div", { children: [jsx("strong", { children: "Loading:" }), " ", isLoading ? 'Yes' : 'No'] }), jsxs("div", { children: [jsx("strong", { children: "Initialized:" }), " ", isInitialized ? 'Yes' : 'No'] }), error && jsxs("div", { style: { color: 'red' }, children: [jsx("strong", { children: "Error:" }), " ", error] }), xUser && (jsxs("div", { children: [jsx("strong", { children: "user_id:" }), " ", xUser.userId, " ", jsx("br", {}), jsx("strong", { children: "clerk_user_id:" }), " ", xUser.clerkUserId || 'None', " ", jsx("br", {}), jsx("strong", { children: "email:" }), " ", xUser.email || 'None', " ", jsx("br", {})] })), xCredit && (jsxs("div", { children: [jsx("strong", { children: "Credits:" }), " ", xCredit.balanceFree, " Free + ", xCredit.balancePaid, " Paid = ", xCredit.totalBalance, " Total"] })), jsx("div", { children: xSubscription ? (jsxs(Fragment, { children: [jsx("strong", { children: "user_id:" }), " ", xSubscription.userId, " ", jsx("br", {}), jsx("strong", { children: "pay_subscription_id:" }), " ", xSubscription.paySubscriptionId, " ", jsx("br", {}), jsx("strong", { children: "price_id:" }), " ", xSubscription.priceId || 'None', " ", jsx("br", {}), jsx("strong", { children: "price_name:" }), " ", xSubscription.priceName || 'None', " ", jsx("br", {}), jsx("strong", { children: "status:" }), " ", xSubscription.status || 'Free', " ", jsx("br", {}), jsx("strong", { children: "credits_allocated:" }), " ", xSubscription.creditsAllocated || '', " ", jsx("br", {}), jsx("strong", { children: "sub_period_start:" }), " ", xSubscription.subPeriodStart || '', " ", jsx("br", {}), jsx("strong", { children: "sub_period_end:" }), " ", xSubscription.subPeriodEnd || '', " ", jsx("br", {})] })) : (jsx("strong", { children: "No Subscription, Default as Hobby Plan" })) })] })] }))] }));
117
+ }, children: [jsx("h4", { style: { margin: '0 0 5px 0' }, children: "Fingerprint Debug" }), jsxs("div", { children: [jsx("strong", { children: "FP_ID:" }), " ", fingerprintId || 'None'] }), jsxs("div", { children: [jsx("strong", { children: "Loading:" }), " ", isLoading ? 'Yes' : 'No'] }), jsxs("div", { children: [jsx("strong", { children: "Initialized:" }), " ", isInitialized ? 'Yes' : 'No'] }), error && jsxs("div", { style: { color: 'red' }, children: [jsx("strong", { children: "Error:" }), " ", error] }), xUser && (jsxs("div", { children: [jsx("strong", { children: "user_id:" }), " ", xUser.userId || 'None', " ", jsx("br", {}), jsx("strong", { children: "fingerprintId:" }), " ", xUser.fingerprintId || 'None', " ", jsx("br", {}), jsx("strong", { children: "clerk_user_id:" }), " ", xUser.clerkUserId || 'None', " ", jsx("br", {}), jsx("strong", { children: "email:" }), " ", xUser.email || 'None', " ", jsx("br", {})] })), xCredit && (jsxs("div", { children: [jsx("strong", { children: "Credits:" }), " ", xCredit.balanceFree, " Free + ", xCredit.balancePaid, " Paid = ", xCredit.totalBalance, " Total"] })), jsx("div", { children: xSubscription ? (jsxs(Fragment, { children: [jsx("strong", { children: "user_id:" }), " ", xSubscription.userId, " ", jsx("br", {}), jsx("strong", { children: "pay_subscription_id:" }), " ", xSubscription.paySubscriptionId || 'None', " ", jsx("br", {}), jsx("strong", { children: "price_id:" }), " ", xSubscription.priceId || 'None', " ", jsx("br", {}), jsx("strong", { children: "price_name:" }), " ", xSubscription.priceName || 'None', " ", jsx("br", {}), jsx("strong", { children: "status:" }), " ", xSubscription.status || 'Free', " ", jsx("br", {}), jsx("strong", { children: "credits_allocated:" }), " ", xSubscription.creditsAllocated || '', " ", jsx("br", {}), jsx("strong", { children: "sub_period_start:" }), " ", xSubscription.subPeriodStart || '', " ", jsx("br", {}), jsx("strong", { children: "sub_period_end:" }), " ", xSubscription.subPeriodEnd || '', " ", jsx("br", {})] })) : (jsx("strong", { children: "No Subscription, Default as Hobby Plan" })) })] })] }))] }));
113
118
  }
114
119
 
115
120
  export { FingerprintProvider, FingerprintStatus, useFingerprintContext, useFingerprintContextSafe, withFingerprint };
@@ -1,4 +1,4 @@
1
- import type { UseFingerprintResult, FingerprintConfig } from './types';
1
+ import type { FingerprintConfig, UseFingerprintResult } from './types';
2
2
  /**
3
3
  * Hook for managing fingerprint ID and anonymous user data
4
4
  * Accepts configuration to customize API endpoint and behavior
@@ -10,6 +10,20 @@ var fingerprintClient = require('./fingerprint-client.js');
10
10
  * Accepts configuration to customize API endpoint and behavior
11
11
  */
12
12
  function useFingerprint(config) {
13
+ // 服务端渲染检查
14
+ if (typeof window === 'undefined') {
15
+ return {
16
+ fingerprintId: null,
17
+ xUser: null,
18
+ xCredit: null,
19
+ xSubscription: null,
20
+ isLoading: false,
21
+ isInitialized: false,
22
+ error: 'Server-side rendering is not supported',
23
+ initializeAnonymousUser: () => tslib_es6.__awaiter(this, void 0, void 0, function* () { }),
24
+ refreshUserData: () => tslib_es6.__awaiter(this, void 0, void 0, function* () { }),
25
+ };
26
+ }
13
27
  const [fingerprintId, setFingerprintIdState] = React.useState(null);
14
28
  const [xUser, setXUser] = React.useState(null);
15
29
  const [xCredit, setXCredit] = React.useState(null);
@@ -21,11 +35,9 @@ function useFingerprint(config) {
21
35
  * 第一阶段:初始化fingerprint ID
22
36
  */
23
37
  const initializeFingerprintId = React.useCallback(() => tslib_es6.__awaiter(this, void 0, void 0, function* () {
24
- if (typeof window === 'undefined')
25
- return null;
26
38
  try {
27
- // 优先检查现有ID, 没有就生成新的fingerprint ID
28
39
  const currentFingerprintId = yield fingerprintClient.getOrGenerateFingerprintId();
40
+ console.log('Initialized fingerprintId:', currentFingerprintId);
29
41
  setFingerprintIdState(currentFingerprintId);
30
42
  return currentFingerprintId;
31
43
  }
@@ -39,35 +51,37 @@ function useFingerprint(config) {
39
51
  * 第二阶段:初始化匿名用户
40
52
  */
41
53
  const initializeAnonymousUser = React.useCallback(() => tslib_es6.__awaiter(this, void 0, void 0, function* () {
54
+ var _a;
42
55
  if (!fingerprintId) {
43
- console.warn('Cannot initialize user without fingerprint ID');
56
+ console.warn('Cannot initialize user: Fingerprint ID is missing', { fingerprintId, isLoading, isInitialized });
57
+ setError('Cannot initialize user: Missing fingerprint ID');
44
58
  return;
45
59
  }
46
60
  try {
47
61
  setIsLoading(true);
48
62
  setError(null);
63
+ console.log('Initializing anonymous user with fingerprintId:', fingerprintId);
49
64
  const fingerprintHeaders = yield fingerprintClient.createFingerprintHeaders();
50
65
  const response = yield fetch(config.apiEndpoint, {
51
66
  method: 'POST',
52
67
  headers: Object.assign({ 'Content-Type': 'application/json' }, fingerprintHeaders),
53
- body: JSON.stringify({
54
- fingerprintId: fingerprintId,
55
- }),
68
+ body: JSON.stringify({ fingerprintId }),
56
69
  });
57
70
  if (!response.ok) {
58
71
  const errorData = yield response.json().catch(() => ({}));
59
72
  throw new Error(errorData.error || 'Failed to initialize anonymous user');
60
73
  }
61
74
  const data = yield response.json();
75
+ console.log('API response in initializeAnonymousUser:', data);
62
76
  if (data.success) {
63
- setXUser(data.xUser);
64
- setXCredit(data.xCredit);
65
- setXSubscription(data.xSubscription);
77
+ const updatedXUser = data.xUser || { userId: '', fingerprintId, clerkUserId: '', email: '', status: '', createdAt: '' };
78
+ console.log('Setting xUser:', updatedXUser);
79
+ setXUser(updatedXUser);
80
+ setXCredit(data.xCredit || null);
81
+ setXSubscription(data.xSubscription || null);
66
82
  setIsInitialized(true);
67
- // 确保fingerprint ID同步
68
- if (data.user.fingerprintId !== fingerprintId) {
69
- fingerprintClient.setFingerprintId(data.user.fingerprintId);
70
- setFingerprintIdState(data.user.fingerprintId);
83
+ if (((_a = data.xUser) === null || _a === void 0 ? void 0 : _a.fingerprintId) && data.xUser.fingerprintId !== fingerprintId) {
84
+ setFingerprintIdState(data.xUser.fingerprintId);
71
85
  }
72
86
  }
73
87
  else {
@@ -86,8 +100,11 @@ function useFingerprint(config) {
86
100
  * 刷新用户数据
87
101
  */
88
102
  const refreshUserData = React.useCallback(() => tslib_es6.__awaiter(this, void 0, void 0, function* () {
89
- if (!fingerprintId)
103
+ if (!fingerprintId) {
104
+ console.warn('Cannot refresh user data: Fingerprint ID is missing', { fingerprintId, isLoading, isInitialized });
105
+ setError('Cannot refresh user data: Missing fingerprint ID');
90
106
  return;
107
+ }
91
108
  try {
92
109
  setError(null);
93
110
  const fingerprintHeaders = yield fingerprintClient.createFingerprintHeaders();
@@ -105,9 +122,10 @@ function useFingerprint(config) {
105
122
  }
106
123
  const data = yield response.json();
107
124
  if (data.success) {
108
- setXUser(data.xUser);
109
- setXCredit(data.xCredit);
110
- setXSubscription(data.xSubscription);
125
+ const updatedXUser = data.xUser || { userId: '', fingerprintId, clerkUserId: '', email: '', status: '', createdAt: '' };
126
+ setXUser(updatedXUser);
127
+ setXCredit(data.xCredit || null);
128
+ setXSubscription(data.xSubscription || null);
111
129
  }
112
130
  }
113
131
  catch (err) {
@@ -119,8 +137,10 @@ function useFingerprint(config) {
119
137
  * 检查现有用户数据(仅在有fingerprint ID时执行)
120
138
  */
121
139
  const checkExistingUser = React.useCallback(() => tslib_es6.__awaiter(this, void 0, void 0, function* () {
122
- if (!fingerprintId)
140
+ if (!fingerprintId) {
141
+ console.warn('Cannot check existing user: Fingerprint ID is missing', { fingerprintId, isLoading, isInitialized });
123
142
  return;
143
+ }
124
144
  try {
125
145
  const fingerprintHeaders = yield fingerprintClient.createFingerprintHeaders();
126
146
  const response = yield fetch(`${config.apiEndpoint}?fingerprintId=${fingerprintId}`, {
@@ -130,9 +150,10 @@ function useFingerprint(config) {
130
150
  if (response.ok) {
131
151
  const data = yield response.json();
132
152
  if (data.success) {
133
- setXUser(data.xUser);
134
- setXCredit(data.xCredit);
135
- setXSubscription(data.xSubscription);
153
+ const updatedXUser = data.xUser || { userId: '', fingerprintId, clerkUserId: '', email: '', status: '', createdAt: '' };
154
+ setXUser(updatedXUser);
155
+ setXCredit(data.xCredit || null);
156
+ setXSubscription(data.xSubscription || null);
136
157
  setIsInitialized(true);
137
158
  }
138
159
  }
@@ -143,25 +164,27 @@ function useFingerprint(config) {
143
164
  }), [fingerprintId, config.apiEndpoint]);
144
165
  // 第一阶段:页面加载完成后生成指纹ID
145
166
  React.useEffect(() => {
146
- if (typeof window === 'undefined')
147
- return;
148
167
  const initFingerprint = () => tslib_es6.__awaiter(this, void 0, void 0, function* () {
149
- yield initializeFingerprintId();
150
- setIsLoading(false); // 第一阶段完成,结束加载状态
168
+ setIsLoading(true);
169
+ const currentFingerprintId = yield initializeFingerprintId();
170
+ if (currentFingerprintId) {
171
+ setIsLoading(false);
172
+ }
173
+ else {
174
+ setIsLoading(false);
175
+ }
151
176
  });
152
177
  initFingerprint();
153
178
  }, [initializeFingerprintId]);
154
179
  // 第二阶段:有指纹ID后检查现有用户
155
180
  React.useEffect(() => {
156
- if (!fingerprintId || isInitialized)
181
+ if (!fingerprintId || isInitialized || isLoading)
157
182
  return;
158
183
  checkExistingUser();
159
- }, [fingerprintId, isInitialized, checkExistingUser]);
184
+ }, [fingerprintId, isInitialized, isLoading, checkExistingUser]);
160
185
  // 第三阶段:如果没有现有用户且自动初始化开启,则创建新用户
161
186
  React.useEffect(() => {
162
- if (!fingerprintId || isInitialized || isLoading || error)
163
- return;
164
- if (config.autoInitialize === false)
187
+ if (!fingerprintId || isInitialized || isLoading || error || config.autoInitialize === false)
165
188
  return;
166
189
  initializeAnonymousUser();
167
190
  }, [fingerprintId, isInitialized, isLoading, error, initializeAnonymousUser, config.autoInitialize]);