sa2kit 3.0.0 → 3.1.0

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.
Files changed (198) hide show
  1. package/dist/ai/llm/ui/miniapp/index.d.mts +2 -2
  2. package/dist/ai/llm/ui/miniapp/index.d.ts +2 -2
  3. package/dist/ai/llm/ui/rn/index.d.mts +2 -2
  4. package/dist/ai/llm/ui/rn/index.d.ts +2 -2
  5. package/dist/ai/llm/ui/web/index.d.mts +3 -3
  6. package/dist/ai/llm/ui/web/index.d.ts +3 -3
  7. package/dist/analytics/index.d.mts +1 -1
  8. package/dist/analytics/index.d.ts +1 -1
  9. package/dist/{chunk-QP5N3ER6.js → chunk-3CSCIRQY.js} +5 -6
  10. package/dist/chunk-3CSCIRQY.js.map +1 -0
  11. package/dist/{chunk-RJP2BRJD.mjs → chunk-4OOANEJV.mjs} +2 -2
  12. package/dist/{chunk-RJP2BRJD.mjs.map → chunk-4OOANEJV.mjs.map} +1 -1
  13. package/dist/chunk-5FUAQLGC.mjs +14 -0
  14. package/dist/chunk-5FUAQLGC.mjs.map +1 -0
  15. package/dist/{chunk-S732H246.js → chunk-6PMXAK7F.js} +2 -2
  16. package/dist/{chunk-S732H246.js.map → chunk-6PMXAK7F.js.map} +1 -1
  17. package/dist/chunk-6YLCRWKM.mjs +8 -0
  18. package/dist/chunk-6YLCRWKM.mjs.map +1 -0
  19. package/dist/{chunk-IBWDBBX5.mjs → chunk-7B5SI4OF.mjs} +3 -4
  20. package/dist/chunk-7B5SI4OF.mjs.map +1 -0
  21. package/dist/chunk-EQXPL7TN.mjs +78 -0
  22. package/dist/chunk-EQXPL7TN.mjs.map +1 -0
  23. package/dist/{chunk-GQZHGSRR.js → chunk-GSNGCENJ.js} +39 -2
  24. package/dist/chunk-GSNGCENJ.js.map +1 -0
  25. package/dist/chunk-HQLKOXG7.js +16 -0
  26. package/dist/chunk-HQLKOXG7.js.map +1 -0
  27. package/dist/{chunk-S3PUP7N4.js → chunk-PA4ELVGI.js} +2 -2
  28. package/dist/{chunk-S3PUP7N4.js.map → chunk-PA4ELVGI.js.map} +1 -1
  29. package/dist/{chunk-WZDTNDYR.js → chunk-QSKLQX6H.js} +11 -11
  30. package/dist/{chunk-WZDTNDYR.js.map → chunk-QSKLQX6H.js.map} +1 -1
  31. package/dist/{chunk-LHJKLUM7.mjs → chunk-TNJ6LYX7.mjs} +2 -2
  32. package/dist/{chunk-LHJKLUM7.mjs.map → chunk-TNJ6LYX7.mjs.map} +1 -1
  33. package/dist/{chunk-J77KR2EV.mjs → chunk-WJ7IMUEJ.mjs} +3 -3
  34. package/dist/{chunk-J77KR2EV.mjs.map → chunk-WJ7IMUEJ.mjs.map} +1 -1
  35. package/dist/chunk-YTGUIRRH.js +10 -0
  36. package/dist/chunk-YTGUIRRH.js.map +1 -0
  37. package/dist/common/auth/client/index.d.mts +1 -31
  38. package/dist/common/auth/client/index.d.ts +1 -31
  39. package/dist/common/auth/client/index.js +2 -15
  40. package/dist/common/auth/client/index.mjs +1 -2
  41. package/dist/common/auth/components/index.d.mts +14 -248
  42. package/dist/common/auth/components/index.d.ts +14 -248
  43. package/dist/common/auth/components/index.js +878 -49
  44. package/dist/common/auth/components/index.js.map +1 -1
  45. package/dist/common/auth/components/index.mjs +871 -5
  46. package/dist/common/auth/components/index.mjs.map +1 -1
  47. package/dist/common/auth/hooks/index.d.mts +2 -29
  48. package/dist/common/auth/hooks/index.d.ts +2 -29
  49. package/dist/common/auth/hooks/index.js +2 -11
  50. package/dist/common/auth/hooks/index.mjs +1 -2
  51. package/dist/common/auth/index.d.mts +5 -9
  52. package/dist/common/auth/index.d.ts +5 -9
  53. package/dist/common/auth/index.js +7 -28
  54. package/dist/common/auth/index.mjs +3 -4
  55. package/dist/common/auth/react/index.d.mts +3 -3
  56. package/dist/common/auth/react/index.d.ts +3 -3
  57. package/dist/common/auth/rn/index.d.mts +3 -19
  58. package/dist/common/auth/rn/index.d.ts +3 -19
  59. package/dist/common/auth/rn/index.js +0 -32
  60. package/dist/common/auth/rn/index.js.map +1 -1
  61. package/dist/common/auth/rn/index.mjs +1 -30
  62. package/dist/common/auth/rn/index.mjs.map +1 -1
  63. package/dist/common/auth/server/index.d.mts +81 -2
  64. package/dist/common/auth/server/index.d.ts +81 -2
  65. package/dist/common/auth/server/index.js +128 -37
  66. package/dist/common/auth/server/index.js.map +1 -1
  67. package/dist/common/auth/server/index.mjs +124 -1
  68. package/dist/common/auth/server/index.mjs.map +1 -1
  69. package/dist/common/auth/services/index.js +12 -9
  70. package/dist/common/auth/services/index.js.map +1 -1
  71. package/dist/common/auth/services/index.mjs +10 -1
  72. package/dist/common/auth/services/index.mjs.map +1 -1
  73. package/dist/common/file/server/index.js +32 -32
  74. package/dist/common/file/server/index.mjs +3 -3
  75. package/dist/common/index.d.mts +5 -8
  76. package/dist/common/index.d.ts +5 -8
  77. package/dist/common/index.js +12 -14
  78. package/dist/common/index.mjs +6 -8
  79. package/dist/common/request/index.js +4 -5
  80. package/dist/common/request/index.mjs +2 -3
  81. package/dist/i18n/index.d.mts +1 -1
  82. package/dist/i18n/index.d.ts +1 -1
  83. package/dist/imageCrop/index.d.mts +3 -3
  84. package/dist/imageCrop/index.d.ts +3 -3
  85. package/dist/index-Bd7cKF1j.d.mts +50 -0
  86. package/dist/{index-CLB80GCP.d.ts → index-D0cqaQEH.d.mts} +5 -5
  87. package/dist/{index-CLB80GCP.d.mts → index-D0cqaQEH.d.ts} +5 -5
  88. package/dist/index-DRQxtCDw.d.ts +50 -0
  89. package/dist/{index-BxZauNN_.d.mts → index-DoUo8IrU.d.mts} +8 -8
  90. package/dist/{index-BxZauNN_.d.ts → index-DoUo8IrU.d.ts} +8 -8
  91. package/dist/index.d.mts +152 -131
  92. package/dist/index.d.ts +152 -131
  93. package/dist/index.js +105 -554
  94. package/dist/index.js.map +1 -1
  95. package/dist/index.mjs +106 -555
  96. package/dist/index.mjs.map +1 -1
  97. package/dist/ossFile/server/index.js +32 -32
  98. package/dist/ossFile/server/index.mjs +3 -3
  99. package/dist/request/index.js +3 -4
  100. package/dist/request/index.mjs +1 -2
  101. package/dist/universalExport/index.d.mts +3 -3
  102. package/dist/universalExport/index.d.ts +3 -3
  103. package/dist/universalFile/index.d.mts +5 -5
  104. package/dist/universalFile/index.d.ts +5 -5
  105. package/dist/universalFile/server/index.js +62 -62
  106. package/dist/universalFile/server/index.mjs +2 -2
  107. package/package.json +1 -36
  108. package/dist/auth/client/index.d.mts +0 -13
  109. package/dist/auth/client/index.d.ts +0 -13
  110. package/dist/auth/client/index.js +0 -26
  111. package/dist/auth/client/index.js.map +0 -1
  112. package/dist/auth/client/index.mjs +0 -5
  113. package/dist/auth/client/index.mjs.map +0 -1
  114. package/dist/auth/components/index.d.mts +0 -10
  115. package/dist/auth/components/index.d.ts +0 -10
  116. package/dist/auth/components/index.js +0 -70
  117. package/dist/auth/components/index.js.map +0 -1
  118. package/dist/auth/components/index.mjs +0 -9
  119. package/dist/auth/components/index.mjs.map +0 -1
  120. package/dist/auth/hooks/index.d.mts +0 -13
  121. package/dist/auth/hooks/index.d.ts +0 -13
  122. package/dist/auth/hooks/index.js +0 -28
  123. package/dist/auth/hooks/index.js.map +0 -1
  124. package/dist/auth/hooks/index.mjs +0 -7
  125. package/dist/auth/hooks/index.mjs.map +0 -1
  126. package/dist/auth/index.d.mts +0 -21
  127. package/dist/auth/index.d.ts +0 -21
  128. package/dist/auth/index.js +0 -112
  129. package/dist/auth/index.js.map +0 -1
  130. package/dist/auth/index.mjs +0 -11
  131. package/dist/auth/index.mjs.map +0 -1
  132. package/dist/auth/rn/index.d.mts +0 -3133
  133. package/dist/auth/rn/index.d.ts +0 -3133
  134. package/dist/auth/rn/index.js +0 -684
  135. package/dist/auth/rn/index.js.map +0 -1
  136. package/dist/auth/rn/index.mjs +0 -663
  137. package/dist/auth/rn/index.mjs.map +0 -1
  138. package/dist/auth/schema/index.d.mts +0 -4
  139. package/dist/auth/schema/index.d.ts +0 -4
  140. package/dist/auth/schema/index.js +0 -49
  141. package/dist/auth/schema/index.js.map +0 -1
  142. package/dist/auth/schema/index.mjs +0 -4
  143. package/dist/auth/schema/index.mjs.map +0 -1
  144. package/dist/auth/services/index.d.mts +0 -1
  145. package/dist/auth/services/index.d.ts +0 -1
  146. package/dist/auth/services/index.js +0 -17
  147. package/dist/auth/services/index.js.map +0 -1
  148. package/dist/auth/services/index.mjs +0 -4
  149. package/dist/auth/services/index.mjs.map +0 -1
  150. package/dist/base-api-client-BpmcQt4Q.d.ts +0 -103
  151. package/dist/base-api-client-CFIhZK4C.d.mts +0 -277
  152. package/dist/base-api-client-CFIhZK4C.d.ts +0 -277
  153. package/dist/base-api-client-DXLsq2yz.d.mts +0 -103
  154. package/dist/chunk-7E3J7VAD.mjs +0 -3
  155. package/dist/chunk-7E3J7VAD.mjs.map +0 -1
  156. package/dist/chunk-B3CZ3HXJ.mjs +0 -126
  157. package/dist/chunk-B3CZ3HXJ.mjs.map +0 -1
  158. package/dist/chunk-FOQEQWX5.js +0 -4
  159. package/dist/chunk-FOQEQWX5.js.map +0 -1
  160. package/dist/chunk-G4NFB2QA.mjs +0 -212
  161. package/dist/chunk-G4NFB2QA.mjs.map +0 -1
  162. package/dist/chunk-G5HJGXGC.mjs +0 -39
  163. package/dist/chunk-G5HJGXGC.mjs.map +0 -1
  164. package/dist/chunk-GQZHGSRR.js.map +0 -1
  165. package/dist/chunk-GS7XLKET.js +0 -41
  166. package/dist/chunk-GS7XLKET.js.map +0 -1
  167. package/dist/chunk-IBWDBBX5.mjs.map +0 -1
  168. package/dist/chunk-MWSAH7ZG.mjs +0 -42
  169. package/dist/chunk-MWSAH7ZG.mjs.map +0 -1
  170. package/dist/chunk-MZOGYD4N.mjs +0 -186
  171. package/dist/chunk-MZOGYD4N.mjs.map +0 -1
  172. package/dist/chunk-QDUBO567.js +0 -1148
  173. package/dist/chunk-QDUBO567.js.map +0 -1
  174. package/dist/chunk-QP5N3ER6.js.map +0 -1
  175. package/dist/chunk-RWCD2CAD.mjs +0 -1132
  176. package/dist/chunk-RWCD2CAD.mjs.map +0 -1
  177. package/dist/chunk-S37OK2QG.js +0 -216
  178. package/dist/chunk-S37OK2QG.js.map +0 -1
  179. package/dist/chunk-SFDJNKWC.js +0 -22
  180. package/dist/chunk-SFDJNKWC.js.map +0 -1
  181. package/dist/chunk-TXMX6PZR.js +0 -190
  182. package/dist/chunk-TXMX6PZR.js.map +0 -1
  183. package/dist/chunk-VHN7PF5I.js +0 -20
  184. package/dist/chunk-VHN7PF5I.js.map +0 -1
  185. package/dist/chunk-XBZIS3MV.mjs +0 -13
  186. package/dist/chunk-XBZIS3MV.mjs.map +0 -1
  187. package/dist/chunk-XFOZ56FB.mjs +0 -20
  188. package/dist/chunk-XFOZ56FB.mjs.map +0 -1
  189. package/dist/chunk-Z23HAXHL.js +0 -136
  190. package/dist/chunk-Z23HAXHL.js.map +0 -1
  191. package/dist/index-BcjDRcKp.d.mts +0 -40
  192. package/dist/index-BlpXrrTy.d.ts +0 -40
  193. package/dist/session-BCXvGCnm.d.mts +0 -81
  194. package/dist/session-BCXvGCnm.d.ts +0 -81
  195. package/dist/types-C_W_CoUD.d.ts +0 -99
  196. package/dist/types-DCRvasyH.d.mts +0 -99
  197. package/dist/types.legacy-J-j-_ig_.d.mts +0 -25
  198. package/dist/types.legacy-J-j-_ig_.d.ts +0 -25
@@ -1,684 +0,0 @@
1
- 'use strict';
2
-
3
- var react = require('better-auth/react');
4
- var plugins = require('better-auth/client/plugins');
5
- var React = require('react');
6
-
7
- function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
8
-
9
- var React__default = /*#__PURE__*/_interopDefault(React);
10
-
11
- var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
12
- get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
13
- }) : x)(function(x) {
14
- if (typeof require !== "undefined") return require.apply(this, arguments);
15
- throw Error('Dynamic require of "' + x + '" is not supported');
16
- });
17
-
18
- // src/storage/adapters/react-native-adapter.ts
19
- var AsyncStorage = null;
20
- try {
21
- AsyncStorage = __require("@react-native-async-storage/async-storage").default;
22
- } catch (e) {
23
- }
24
- var ReactNativeStorageAdapter = class {
25
- constructor() {
26
- this.listeners = /* @__PURE__ */ new Map();
27
- }
28
- async getItem(key) {
29
- if (!AsyncStorage) return null;
30
- try {
31
- return await AsyncStorage.getItem(key);
32
- } catch (error) {
33
- console.error('[ReactNativeStorage] Error getting item "' + key + '":', error);
34
- return null;
35
- }
36
- }
37
- async setItem(key, value) {
38
- if (!AsyncStorage) {
39
- throw new Error("AsyncStorage is not available");
40
- }
41
- try {
42
- await AsyncStorage.setItem(key, value);
43
- this.notifyListeners(key, value);
44
- } catch (error) {
45
- console.error('[ReactNativeStorage] Error setting item "' + key + '":', error);
46
- throw error;
47
- }
48
- }
49
- async removeItem(key) {
50
- if (!AsyncStorage) {
51
- throw new Error("AsyncStorage is not available");
52
- }
53
- try {
54
- await AsyncStorage.removeItem(key);
55
- this.notifyListeners(key, null);
56
- } catch (error) {
57
- console.error('[ReactNativeStorage] Error removing item "' + key + '":', error);
58
- throw error;
59
- }
60
- }
61
- async clear() {
62
- if (!AsyncStorage) {
63
- throw new Error("AsyncStorage is not available");
64
- }
65
- try {
66
- await AsyncStorage.clear();
67
- } catch (error) {
68
- console.error("[ReactNativeStorage] Error clearing storage:", error);
69
- throw error;
70
- }
71
- }
72
- addChangeListener(callback) {
73
- if (!this.listeners.has("*")) {
74
- this.listeners.set("*", /* @__PURE__ */ new Set());
75
- }
76
- this.listeners.get("*").add(callback);
77
- return () => {
78
- this.listeners.get("*")?.delete(callback);
79
- };
80
- }
81
- notifyListeners(key, value) {
82
- const globalListeners = this.listeners.get("*");
83
- if (globalListeners) {
84
- globalListeners.forEach((callback) => callback(key, value));
85
- }
86
- }
87
- };
88
-
89
- // src/common/auth/rn/token-storage.ts
90
- var RN_BEARER_TOKEN_KEY = "sa2kit_auth_bearer_token";
91
- var storageOverride = null;
92
- var defaultStorage = new ReactNativeStorageAdapter();
93
- function storage() {
94
- return storageOverride ?? defaultStorage;
95
- }
96
- function setRnBearerTokenStorage(next) {
97
- storageOverride = next;
98
- }
99
- async function getRnBearerToken() {
100
- return storage().getItem(RN_BEARER_TOKEN_KEY);
101
- }
102
- async function setRnBearerToken(token) {
103
- await storage().setItem(RN_BEARER_TOKEN_KEY, token);
104
- }
105
- async function clearRnBearerToken() {
106
- await storage().removeItem(RN_BEARER_TOKEN_KEY);
107
- }
108
-
109
- // src/common/auth/rn/create-rn-auth-client.ts
110
- function normalizeRnAuthBaseUrl(authApiBase) {
111
- const trimmed = authApiBase.replace(/\/+$/, "");
112
- if (trimmed.endsWith("/api/auth")) {
113
- return {
114
- baseURL: trimmed.slice(0, -"/api/auth".length),
115
- basePath: "/api/auth"
116
- };
117
- }
118
- if (trimmed.endsWith("/api")) {
119
- return {
120
- baseURL: trimmed.slice(0, -"/api".length),
121
- basePath: "/api/auth"
122
- };
123
- }
124
- return {
125
- baseURL: trimmed,
126
- basePath: "/api/auth"
127
- };
128
- }
129
- function buildSa2kitRnAuthClient(options) {
130
- return react.createAuthClient({
131
- baseURL: options.baseURL.replace(/\/+$/, ""),
132
- basePath: options.basePath ?? "/api/auth",
133
- plugins: [plugins.emailOTPClient(), plugins.phoneNumberClient()],
134
- disableDefaultFetchPlugins: false,
135
- fetchOptions: {
136
- credentials: "omit",
137
- onRequest: async (ctx) => {
138
- const token = await getRnBearerToken();
139
- if (token) {
140
- ctx.headers.set("Authorization", `Bearer ${token}`);
141
- }
142
- return ctx;
143
- },
144
- onResponse: async (ctx) => {
145
- const token = ctx.response.headers.get("set-auth-token");
146
- if (token) {
147
- await setRnBearerToken(token);
148
- }
149
- return ctx;
150
- }
151
- }
152
- });
153
- }
154
- var cachedClient = null;
155
- var cachedKey = "";
156
- function cacheKey(options) {
157
- return `${options.baseURL}|${options.basePath ?? "/api/auth"}`;
158
- }
159
- function createSa2kitRnAuthClient(options) {
160
- const key = cacheKey(options);
161
- if (cachedClient && cachedKey === key) {
162
- return cachedClient;
163
- }
164
- cachedClient = buildSa2kitRnAuthClient(options);
165
- cachedKey = key;
166
- return cachedClient;
167
- }
168
- function createSa2kitRnAuthClientFromApiBase(authApiBase) {
169
- const { baseURL, basePath } = normalizeRnAuthBaseUrl(authApiBase);
170
- return createSa2kitRnAuthClient({ baseURL, basePath });
171
- }
172
- async function initSa2kitRnAuthClient(options) {
173
- const client = typeof options === "string" ? createSa2kitRnAuthClientFromApiBase(options) : createSa2kitRnAuthClient(options);
174
- const token = await getRnBearerToken();
175
- if (token) {
176
- try {
177
- await client.getSession();
178
- } catch {
179
- await clearRnBearerToken();
180
- }
181
- }
182
- return client;
183
- }
184
- async function signOutSa2kitRnAuthClient(client) {
185
- try {
186
- await client.signOut();
187
- } finally {
188
- await clearRnBearerToken();
189
- }
190
- }
191
- function resetSa2kitRnAuthClientCache() {
192
- cachedClient = null;
193
- cachedKey = "";
194
- }
195
-
196
- // src/common/auth/components/utils.ts
197
- function validatePhoneNumber(phone) {
198
- return /^1[3-9]\d{9}$/.test(phone.trim());
199
- }
200
- function validateEmail(email) {
201
- return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email.trim());
202
- }
203
- function getAuthClientError(result, fallback) {
204
- const r = result;
205
- if (!r?.error) return null;
206
- return r.error.message ?? r.error.statusText ?? fallback;
207
- }
208
- function errorMessage(error, fallback) {
209
- if (error instanceof Error && error.message) return error.message;
210
- if (typeof error === "string") return error;
211
- return fallback;
212
- }
213
-
214
- // src/common/auth/rn/sign-in.ts
215
- function clientApi(client) {
216
- return client;
217
- }
218
- async function signInWithRnAuthClient(authClient, account, password) {
219
- const trimmed = account.trim();
220
- try {
221
- if (validatePhoneNumber(trimmed)) {
222
- const result = await clientApi(authClient).signIn.phoneNumber({
223
- phoneNumber: trimmed,
224
- password
225
- });
226
- const err = getAuthClientError(result, "\u767B\u5F55\u5931\u8D25");
227
- return err ? { success: false, error: err } : { success: true };
228
- }
229
- if (validateEmail(trimmed)) {
230
- const result = await clientApi(authClient).signIn.email({
231
- email: trimmed,
232
- password
233
- });
234
- const err = getAuthClientError(result, "\u767B\u5F55\u5931\u8D25");
235
- return err ? { success: false, error: err } : { success: true };
236
- }
237
- return { success: false, error: "\u8BF7\u8F93\u5165\u6B63\u786E\u7684\u624B\u673A\u53F7\u6216\u90AE\u7BB1" };
238
- } catch (e) {
239
- return { success: false, error: errorMessage(e, "\u767B\u5F55\u5931\u8D25") };
240
- }
241
- }
242
- var ReactNative = __require("react-native");
243
- var { ActivityIndicator, Pressable, Text, TextInput, View, StyleSheet } = ReactNative;
244
- var defaultStyles = StyleSheet.create({
245
- label: { fontSize: 12, color: "#8b98a5", marginBottom: 6, marginTop: 12 },
246
- input: {
247
- borderWidth: 1,
248
- borderColor: "rgba(255,255,255,0.12)",
249
- borderRadius: 10,
250
- paddingHorizontal: 12,
251
- paddingVertical: 10,
252
- color: "#e8edf2",
253
- backgroundColor: "rgba(0,0,0,0.2)",
254
- fontSize: 15
255
- },
256
- err: { color: "#f87171", marginTop: 10, fontSize: 14 },
257
- btn: {
258
- marginTop: 14,
259
- paddingVertical: 12,
260
- borderRadius: 10,
261
- alignItems: "center",
262
- borderWidth: 1,
263
- borderColor: "rgba(255,255,255,0.12)",
264
- backgroundColor: "rgba(255,255,255,0.08)"
265
- },
266
- primary: { backgroundColor: "#3d9eff", borderColor: "transparent" },
267
- disabled: { opacity: 0.6 },
268
- btnTextPrimary: { color: "#fff", fontSize: 15, fontWeight: "600" },
269
- centerInline: { paddingVertical: 24, alignItems: "center" }
270
- });
271
- var DEFAULT_LABELS = {
272
- authApiBase: "\u8BA4\u8BC1 API \u6839\u5730\u5740\uFF08\u542B /api\uFF09",
273
- account: "\u624B\u673A\u53F7 / \u90AE\u7BB1",
274
- password: "\u5BC6\u7801",
275
- submit: "\u767B\u5F55"
276
- };
277
- function RnAccountLoginForm({
278
- authApiBase,
279
- defaultAuthApiBase = "",
280
- onAuthApiBaseChange,
281
- submitting = false,
282
- error = "",
283
- onError,
284
- onSuccess,
285
- theme,
286
- labels: labelsProp,
287
- placeholders
288
- }) {
289
- const labels = {
290
- ...DEFAULT_LABELS,
291
- account: labelsProp?.account ?? labelsProp?.phone ?? labelsProp?.email ?? DEFAULT_LABELS.account,
292
- ...labelsProp
293
- };
294
- const [authClient, setAuthClient] = React.useState(null);
295
- const [account, setAccount] = React.useState("");
296
- const [password, setPassword] = React.useState("");
297
- const [loading, setLoading] = React.useState(false);
298
- React.useEffect(() => {
299
- let cancelled = false;
300
- const base = authApiBase.trim() || defaultAuthApiBase;
301
- if (!base) {
302
- setAuthClient(null);
303
- return;
304
- }
305
- resetSa2kitRnAuthClientCache();
306
- initSa2kitRnAuthClient(base).then((client) => {
307
- if (!cancelled) setAuthClient(client);
308
- });
309
- return () => {
310
- cancelled = true;
311
- };
312
- }, [authApiBase, defaultAuthApiBase]);
313
- const handleSubmit = React.useCallback(async () => {
314
- if (!authClient) return;
315
- onError?.("");
316
- setLoading(true);
317
- try {
318
- const trimmedAccount = account.trim();
319
- if (!trimmedAccount || !password) {
320
- onError?.("\u8BF7\u586B\u5199\u8D26\u53F7\u548C\u5BC6\u7801");
321
- return;
322
- }
323
- const result = await signInWithRnAuthClient(authClient, trimmedAccount, password);
324
- if (!result.success) {
325
- onError?.(result.error);
326
- return;
327
- }
328
- const token = await getRnBearerToken();
329
- if (!token) {
330
- onError?.("\u767B\u5F55\u6210\u529F\u4F46\u672A\u83B7\u53D6\u5230 Bearer token");
331
- return;
332
- }
333
- await onSuccess(token);
334
- } finally {
335
- setLoading(false);
336
- }
337
- }, [account, authClient, onError, onSuccess, password]);
338
- const authApiField = onAuthApiBaseChange ? /* @__PURE__ */ React__default.default.createElement(React__default.default.Fragment, null, /* @__PURE__ */ React__default.default.createElement(Text, { style: [defaultStyles.label, theme?.label] }, labels.authApiBase), /* @__PURE__ */ React__default.default.createElement(
339
- TextInput,
340
- {
341
- style: [defaultStyles.input, theme?.input, theme?.inputContainer],
342
- value: authApiBase,
343
- onChangeText: onAuthApiBaseChange,
344
- autoCapitalize: "none",
345
- autoCorrect: false,
346
- placeholder: placeholders?.authApiBase ?? defaultAuthApiBase,
347
- placeholderTextColor: "#6b7a8a"
348
- }
349
- )) : null;
350
- if (!authClient) {
351
- return /* @__PURE__ */ React__default.default.createElement(View, { style: [defaultStyles.centerInline, theme?.loadingContainer] }, /* @__PURE__ */ React__default.default.createElement(ActivityIndicator, null));
352
- }
353
- const busy = loading || submitting;
354
- return /* @__PURE__ */ React__default.default.createElement(React__default.default.Fragment, null, authApiField, /* @__PURE__ */ React__default.default.createElement(Text, { style: [defaultStyles.label, theme?.label] }, labels.account), /* @__PURE__ */ React__default.default.createElement(
355
- TextInput,
356
- {
357
- style: [defaultStyles.input, theme?.input, theme?.inputContainer],
358
- value: account,
359
- onChangeText: setAccount,
360
- keyboardType: "email-address",
361
- autoCapitalize: "none",
362
- autoCorrect: false,
363
- placeholder: placeholders?.account ?? placeholders?.phone ?? "\u624B\u673A\u53F7\u6216\u90AE\u7BB1",
364
- placeholderTextColor: "#6b7a8a"
365
- }
366
- ), /* @__PURE__ */ React__default.default.createElement(Text, { style: [defaultStyles.label, theme?.label] }, labels.password), /* @__PURE__ */ React__default.default.createElement(
367
- TextInput,
368
- {
369
- style: [defaultStyles.input, theme?.input, theme?.inputContainer],
370
- value: password,
371
- onChangeText: setPassword,
372
- secureTextEntry: true,
373
- autoCapitalize: "none",
374
- autoCorrect: false,
375
- placeholder: placeholders?.password ?? "\u5BC6\u7801",
376
- placeholderTextColor: "#6b7a8a"
377
- }
378
- ), error ? /* @__PURE__ */ React__default.default.createElement(Text, { style: [defaultStyles.err, theme?.error] }, error) : null, /* @__PURE__ */ React__default.default.createElement(
379
- Pressable,
380
- {
381
- style: [
382
- defaultStyles.btn,
383
- defaultStyles.primary,
384
- theme?.button,
385
- theme?.buttonPrimary,
386
- busy && (theme?.buttonDisabled ?? defaultStyles.disabled)
387
- ],
388
- onPress: () => void handleSubmit(),
389
- disabled: busy
390
- },
391
- busy ? /* @__PURE__ */ React__default.default.createElement(ActivityIndicator, { color: "#fff" }) : /* @__PURE__ */ React__default.default.createElement(Text, { style: [defaultStyles.btnTextPrimary, theme?.buttonTextPrimary] }, labels.submit)
392
- ));
393
- }
394
-
395
- // src/common/auth/client/types.ts
396
- var STORAGE_KEYS = {
397
- AUTH_TOKEN: "auth_token",
398
- USER_DATA: "user_data"
399
- };
400
- var API_ROUTES = {
401
- AUTH: {
402
- LOGIN: "/auth/login",
403
- REGISTER: "/auth/register",
404
- LOGOUT: "/auth/logout",
405
- ME: "/auth/me"
406
- }
407
- };
408
-
409
- // src/common/auth/client/base-api-client.ts
410
- var BaseApiClient = class {
411
- constructor(storage2, request, baseUrl) {
412
- this.storage = storage2;
413
- this.request = request;
414
- this.baseUrl = baseUrl;
415
- this.token = null;
416
- this.user = null;
417
- }
418
- /**
419
- * 初始化 - 从存储中加载 token 和用户信息
420
- */
421
- async init() {
422
- try {
423
- this.token = await this.storage.getItem(STORAGE_KEYS.AUTH_TOKEN);
424
- const userData = await this.storage.getItem(STORAGE_KEYS.USER_DATA);
425
- if (userData) {
426
- this.user = JSON.parse(userData);
427
- }
428
- } catch (error) {
429
- console.error("Failed to load auth data:", error);
430
- }
431
- }
432
- /**
433
- * 设置认证 token
434
- */
435
- async setToken(token) {
436
- this.token = token;
437
- if (token) {
438
- await this.storage.setItem(STORAGE_KEYS.AUTH_TOKEN, token);
439
- } else {
440
- await this.storage.removeItem(STORAGE_KEYS.AUTH_TOKEN);
441
- }
442
- }
443
- /**
444
- * 设置用户信息
445
- */
446
- async setUser(user) {
447
- this.user = user;
448
- if (user) {
449
- await this.storage.setItem(STORAGE_KEYS.USER_DATA, JSON.stringify(user));
450
- } else {
451
- await this.storage.removeItem(STORAGE_KEYS.USER_DATA);
452
- }
453
- }
454
- /**
455
- * 获取当前 token
456
- */
457
- getToken() {
458
- return this.token;
459
- }
460
- /**
461
- * 获取当前用户
462
- */
463
- getUser() {
464
- return this.user;
465
- }
466
- /**
467
- * 检查是否已登录
468
- */
469
- async isAuthenticated() {
470
- return !!this.token;
471
- }
472
- /**
473
- * 清除用户数据
474
- */
475
- async clearUserData() {
476
- await this.storage.removeItem(STORAGE_KEYS.AUTH_TOKEN);
477
- await this.storage.removeItem(STORAGE_KEYS.USER_DATA);
478
- this.token = null;
479
- this.user = null;
480
- }
481
- /**
482
- * 发送请求的通用方法
483
- */
484
- async sendRequest(config) {
485
- try {
486
- const headers = {
487
- "Content-Type": "application/json",
488
- ...config.headers || {}
489
- };
490
- if (this.token) {
491
- headers["Authorization"] = "Bearer " + this.token;
492
- }
493
- const response = await this.request.request({
494
- ...config,
495
- url: this.baseUrl + config.url,
496
- headers
497
- });
498
- return response;
499
- } catch (error) {
500
- console.error("API request error:", error);
501
- return {
502
- success: false,
503
- error: error instanceof Error ? error.message : "\u7F51\u7EDC\u9519\u8BEF\uFF0C\u8BF7\u91CD\u8BD5"
504
- };
505
- }
506
- }
507
- // ==================== 认证相关 API ====================
508
- /**
509
- * 用户注册
510
- */
511
- async register(email, password, username) {
512
- const response = await this.sendRequest({
513
- url: API_ROUTES.AUTH.REGISTER,
514
- method: "POST",
515
- body: { email, password, username }
516
- });
517
- if (response.success && response.data) {
518
- await this.setToken(response.data.token);
519
- await this.setUser(response.data.user);
520
- }
521
- return response;
522
- }
523
- /**
524
- * 用户登录
525
- */
526
- async login(email, password) {
527
- const response = await this.sendRequest({
528
- url: API_ROUTES.AUTH.LOGIN,
529
- method: "POST",
530
- body: { email, password }
531
- });
532
- if (response.success && response.data) {
533
- await this.setToken(response.data.token);
534
- await this.setUser(response.data.user);
535
- if (typeof window !== "undefined") {
536
- window.dispatchEvent(
537
- new CustomEvent("user_login_success", {
538
- detail: {
539
- userId: response.data.user.id,
540
- email: response.data.user.email,
541
- role: response.data.user.role
542
- }
543
- })
544
- );
545
- }
546
- }
547
- return response;
548
- }
549
- /**
550
- * 用户退出登录
551
- */
552
- async logout() {
553
- await this.sendRequest({
554
- url: API_ROUTES.AUTH.LOGOUT,
555
- method: "POST"
556
- });
557
- await this.clearUserData();
558
- }
559
- /**
560
- * 获取当前用户信息
561
- */
562
- async getCurrentUser() {
563
- const response = await this.sendRequest({
564
- url: API_ROUTES.AUTH.ME,
565
- method: "GET"
566
- });
567
- if (response.success && response.data) {
568
- const userData = response.data.user || response.data;
569
- await this.setUser(userData);
570
- return {
571
- ...response,
572
- data: userData
573
- };
574
- }
575
- return response;
576
- }
577
- // ==================== 通用方法 ====================
578
- /**
579
- * 发送 GET 请求
580
- */
581
- async get(url, params) {
582
- return this.sendRequest({ url, method: "GET", params });
583
- }
584
- /**
585
- * 发送 POST 请求
586
- */
587
- async post(url, body) {
588
- return this.sendRequest({ url, method: "POST", body });
589
- }
590
- /**
591
- * 发送 PUT 请求
592
- */
593
- async put(url, body) {
594
- return this.sendRequest({ url, method: "PUT", body });
595
- }
596
- /**
597
- * 发送 DELETE 请求
598
- */
599
- async delete(url) {
600
- return this.sendRequest({ url, method: "DELETE" });
601
- }
602
- };
603
-
604
- // src/request/adapters/react-native-adapter.ts
605
- var ReactNativeRequestAdapter = class {
606
- async request(config) {
607
- const { url, method = "GET", headers = {}, body, params } = config;
608
- let fullUrl = url;
609
- if (params) {
610
- const searchParams = new URLSearchParams();
611
- Object.entries(params).forEach(([key, value]) => {
612
- if (value !== void 0 && value !== null) {
613
- searchParams.append(key, String(value));
614
- }
615
- });
616
- const qs = searchParams.toString();
617
- if (qs) {
618
- fullUrl += url.includes("?") ? `&${qs}` : `?${qs}`;
619
- }
620
- }
621
- const response = await fetch(fullUrl, {
622
- method,
623
- headers: {
624
- "Content-Type": "application/json",
625
- ...headers
626
- },
627
- body: body ? JSON.stringify(body) : void 0
628
- });
629
- const data = await response.json();
630
- if (!response.ok) {
631
- return {
632
- success: false,
633
- error: data.error ?? `\u8BF7\u6C42\u5931\u8D25 (${response.status})`
634
- };
635
- }
636
- return data;
637
- }
638
- };
639
-
640
- // src/common/auth/rn/legacy-client.ts
641
- var cached = null;
642
- var cachedBase = "";
643
- function createRnAuthClient(authApiBase) {
644
- const base = authApiBase.replace(/\/+$/, "");
645
- if (cached && cachedBase === base) {
646
- return cached;
647
- }
648
- const client = new BaseApiClient(
649
- new ReactNativeStorageAdapter(),
650
- new ReactNativeRequestAdapter(),
651
- base
652
- );
653
- cached = client;
654
- cachedBase = base;
655
- return client;
656
- }
657
- async function initRnAuthClient(authApiBase) {
658
- const client = createRnAuthClient(authApiBase);
659
- await client.init();
660
- return client;
661
- }
662
- function resetRnAuthClientCache() {
663
- cached = null;
664
- cachedBase = "";
665
- }
666
-
667
- exports.RN_BEARER_TOKEN_KEY = RN_BEARER_TOKEN_KEY;
668
- exports.RnAccountLoginForm = RnAccountLoginForm;
669
- exports.clearRnBearerToken = clearRnBearerToken;
670
- exports.createRnAuthClient = createRnAuthClient;
671
- exports.createSa2kitRnAuthClient = createSa2kitRnAuthClient;
672
- exports.createSa2kitRnAuthClientFromApiBase = createSa2kitRnAuthClientFromApiBase;
673
- exports.getRnBearerToken = getRnBearerToken;
674
- exports.initRnAuthClient = initRnAuthClient;
675
- exports.initSa2kitRnAuthClient = initSa2kitRnAuthClient;
676
- exports.normalizeRnAuthBaseUrl = normalizeRnAuthBaseUrl;
677
- exports.resetRnAuthClientCache = resetRnAuthClientCache;
678
- exports.resetSa2kitRnAuthClientCache = resetSa2kitRnAuthClientCache;
679
- exports.setRnBearerToken = setRnBearerToken;
680
- exports.setRnBearerTokenStorage = setRnBearerTokenStorage;
681
- exports.signInWithRnAuthClient = signInWithRnAuthClient;
682
- exports.signOutSa2kitRnAuthClient = signOutSa2kitRnAuthClient;
683
- //# sourceMappingURL=index.js.map
684
- //# sourceMappingURL=index.js.map