@jetthai/cuora 1.1.480 → 1.1.481-api.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.
package/AGENTS.md CHANGED
@@ -191,7 +191,7 @@ Token 5min TTL → fetchUserInfo success → extendToken resets TTL → poll aga
191
191
  | `LOGIN_TELEGRAM` | Telegram | additionalUserInfo |
192
192
  | `LOGIN_3PARTY` | Third-party redirect | **Does not switch state**; uses `window.location.href` directly |
193
193
 
194
- **OTP flow**: after a successful login it enters `otpChecking`, and only proceeds to `loginSuccess` after an `OTP_VERIFIED` or `OTP_SKIP` event.
194
+ **OTP / bind-bank flow**: after a successful login it enters `otpChecking`; `OTP_VERIFIED` or `OTP_SKIP` moves it to `bindBankChecking`, and only `BIND_BANK_DONE` or `BIND_BANK_SKIP` proceeds to `loginSuccess` (either gate accepts `LOGOUT` to abort). Whether binding is required is decided by the downstream frontend; the machine only waits — **downstream must send one of these events in `bindBankChecking`, or the session stalls there**. The `tokenAuthenticated` path skips both gates.
195
195
 
196
196
  **Account persistence**: the most recently used login account is stored in localStorage (31-day TTL) for form auto-fill. **Token expiration does not affect the account record.** However, `pruneRememberedAccounts`, which runs after `fetchSiteConfig` succeeds, **compares against `siteConfig.loginMethods` and clears the leftover account for any disabled login method** (`phone` clears the country code along with it) — fetching configuration, an apparently read-only action, carries the side effect of deleting from localStorage. The purpose is to prevent downstream frontends from back-filling a stale value into an input whose type has since changed after the backend disables a login method. When `loginMethods` is missing entirely, nothing is cleared, to avoid wiping everything by mistake.
197
197
 
@@ -189,7 +189,7 @@ Token 5min TTL → fetchUserInfo 成功 → extendToken 重設 TTL → 再次輪
189
189
  | `LOGIN_TELEGRAM` | Telegram | additionalUserInfo |
190
190
  | `LOGIN_3PARTY` | 第三方跳轉 | **不切換狀態**,直接 `window.location.href` |
191
191
 
192
- **OTP 流程**:登入成功後進入 `otpChecking`,等待 `OTP_VERIFIED` 或 `OTP_SKIP` 事件才進入 `loginSuccess`。
192
+ **OTP/綁銀行流程**:登入成功後進入 `otpChecking`,等待 `OTP_VERIFIED` 或 `OTP_SKIP` 後進入 `bindBankChecking`,再等待 `BIND_BANK_DONE` 或 `BIND_BANK_SKIP` 才進入 `loginSuccess`(兩關都可送 `LOGOUT` 中止)。是否需要綁定由下游前端判斷,machine 只負責等待;**下游必須在 `bindBankChecking` 送出其中一個事件,否則會停在這一關**。`tokenAuthenticated` 路徑不經過這兩關。
193
193
 
194
194
  **帳號持久化**:最後使用的登入帳號存 localStorage(31 天 TTL)供表單自動填入。**Token 過期不影響帳號記錄。**但 `fetchSiteConfig` 成功後的 `pruneRememberedAccounts` 會**比對 `siteConfig.loginMethods` 清掉已停用登入方式的殘留帳號**(`phone` 連同國碼一起刪)——拉設定這個看似唯讀的動作帶有刪 localStorage 的副作用,目的是避免後台關掉某種登入方式後,下游把舊值回填進已改型別的輸入框。`loginMethods` 整包缺失時不清除,以免誤刪全部。
195
195
 
package/CHANGELOG.md CHANGED
@@ -2,6 +2,17 @@
2
2
 
3
3
  All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
4
4
 
5
+ ### [1.1.481](https://github.com/jetthai/cuora-module/compare/v1.1.480...v1.1.481) (2026-10-20)
6
+
7
+ ### ⚠ BREAKING CHANGES
8
+
9
+ - 下游在 OTP 關卡送出 OTP_VERIFIED / OTP_SKIP 後,必須在 bindBankChecking 送出
10
+ BIND_BANK_DONE 或 BIND_BANK_SKIP,否則會停在此關卡。目前僅 client-pwa-cash 使用本套件。
11
+
12
+ Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
13
+
14
+ - [JT-1784011020] feat(auth): 新增 bindBankChecking 關卡,綁銀行完成前不進入 loginSuccess ([c9b5210](https://github.com/jetthai/cuora-module/commit/c9b5210b8f1abb9adca7c79207e89d66191daf8f))
15
+
5
16
  ### [1.1.480](https://github.com/jetthai/cuora-module/compare/v1.1.479...v1.1.480) (2026-10-19)
6
17
 
7
18
  ### ⚠ BREAKING CHANGES
package/CLAUDE.md CHANGED
@@ -191,7 +191,7 @@ Token 5min TTL → fetchUserInfo success → extendToken resets TTL → poll aga
191
191
  | `LOGIN_TELEGRAM` | Telegram | additionalUserInfo |
192
192
  | `LOGIN_3PARTY` | Third-party redirect | **Does not switch state**; uses `window.location.href` directly |
193
193
 
194
- **OTP flow**: after a successful login it enters `otpChecking`, and only proceeds to `loginSuccess` after an `OTP_VERIFIED` or `OTP_SKIP` event.
194
+ **OTP / bind-bank flow**: after a successful login it enters `otpChecking`; `OTP_VERIFIED` or `OTP_SKIP` moves it to `bindBankChecking`, and only `BIND_BANK_DONE` or `BIND_BANK_SKIP` proceeds to `loginSuccess` (either gate accepts `LOGOUT` to abort). Whether binding is required is decided by the downstream frontend; the machine only waits — **downstream must send one of these events in `bindBankChecking`, or the session stalls there**. The `tokenAuthenticated` path skips both gates.
195
195
 
196
196
  **Account persistence**: the most recently used login account is stored in localStorage (31-day TTL) for form auto-fill. **Token expiration does not affect the account record.** However, `pruneRememberedAccounts`, which runs after `fetchSiteConfig` succeeds, **compares against `siteConfig.loginMethods` and clears the leftover account for any disabled login method** (`phone` clears the country code along with it) — fetching configuration, an apparently read-only action, carries the side effect of deleting from localStorage. The purpose is to prevent downstream frontends from back-filling a stale value into an input whose type has since changed after the backend disables a login method. When `loginMethods` is missing entirely, nothing is cleared, to avoid wiping everything by mistake.
197
197
 
package/GEMINI.md CHANGED
@@ -191,7 +191,7 @@ Token 5min TTL → fetchUserInfo success → extendToken resets TTL → poll aga
191
191
  | `LOGIN_TELEGRAM` | Telegram | additionalUserInfo |
192
192
  | `LOGIN_3PARTY` | Third-party redirect | **Does not switch state**; uses `window.location.href` directly |
193
193
 
194
- **OTP flow**: after a successful login it enters `otpChecking`, and only proceeds to `loginSuccess` after an `OTP_VERIFIED` or `OTP_SKIP` event.
194
+ **OTP / bind-bank flow**: after a successful login it enters `otpChecking`; `OTP_VERIFIED` or `OTP_SKIP` moves it to `bindBankChecking`, and only `BIND_BANK_DONE` or `BIND_BANK_SKIP` proceeds to `loginSuccess` (either gate accepts `LOGOUT` to abort). Whether binding is required is decided by the downstream frontend; the machine only waits — **downstream must send one of these events in `bindBankChecking`, or the session stalls there**. The `tokenAuthenticated` path skips both gates.
195
195
 
196
196
  **Account persistence**: the most recently used login account is stored in localStorage (31-day TTL) for form auto-fill. **Token expiration does not affect the account record.** However, `pruneRememberedAccounts`, which runs after `fetchSiteConfig` succeeds, **compares against `siteConfig.loginMethods` and clears the leftover account for any disabled login method** (`phone` clears the country code along with it) — fetching configuration, an apparently read-only action, carries the side effect of deleting from localStorage. The purpose is to prevent downstream frontends from back-filling a stale value into an input whose type has since changed after the backend disables a login method. When `loginMethods` is missing entirely, nothing is cleared, to avoid wiping everything by mistake.
197
197
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jetthai/cuora",
3
- "version": "1.1.480",
3
+ "version": "1.1.481-api.0",
4
4
  "description": "",
5
5
  "main": "./src/index.js",
6
6
  "type": "module",
@@ -115,9 +115,46 @@ export default function (request) {
115
115
  },
116
116
  on: {
117
117
  OTP_VERIFIED: {
118
- target: 'loginSuccess',
118
+ target: 'bindBankChecking',
119
119
  },
120
120
  OTP_SKIP: {
121
+ target: 'bindBankChecking',
122
+ },
123
+ LOGOUT: {
124
+ target: 'leaving',
125
+ },
126
+ },
127
+ },
128
+ // 綁銀行與 OTP 同為登入完成前的關卡:放在 loginSuccess 之前,登入後的副作用(公告、活動彈窗等)
129
+ // 才不會在綁定窗開著時觸發。是否需要綁定由前端依 siteConfig 判斷,machine 只負責等待結果。
130
+ bindBankChecking: {
131
+ initial: 'pending',
132
+ states: {
133
+ pending: {
134
+ after: {
135
+ TIMEOUT: {
136
+ target: 'fetching',
137
+ },
138
+ },
139
+ },
140
+ fetching: {
141
+ invoke: {
142
+ src: 'fetchUserInfo',
143
+ onDone: {
144
+ target: 'pending',
145
+ actions: ['setUserInfo', 'extendToken'],
146
+ },
147
+ onError: {
148
+ target: 'pending',
149
+ },
150
+ },
151
+ },
152
+ },
153
+ on: {
154
+ BIND_BANK_DONE: {
155
+ target: 'loginSuccess',
156
+ },
157
+ BIND_BANK_SKIP: {
121
158
  target: 'loginSuccess',
122
159
  },
123
160
  LOGOUT: {
@@ -5,6 +5,8 @@ import { getErr998SentryExtra } from '../util/err998SentryExtra.js'
5
5
  // 暫停 ERR_998 上報:在組裝 Sentry 資料之前就中止,不建立事件;仍照常 reject ERR_998 給呼叫端。恢復時改回 false 即可
6
6
  const IS_ERR_998_REPORT_PAUSED = true
7
7
 
8
+ const CANCELLED_MESSAGE = 'Requests Cancelled'
9
+
8
10
  // const pickBy = require('lodash/pickBy')
9
11
  const supportVersion2List = [
10
12
  // 04-09 Update
@@ -160,6 +162,9 @@ export default class Request {
160
162
  this.errorHandler = null
161
163
  this.hosts = []
162
164
  this.isBlocked = false
165
+ this.activeRequests = new Set() // { source, token }:僅追蹤非 public 且帶 token 的在途請求,供 revokeToken 取消
166
+ this.revokedToken = null
167
+ this.tokenGate = null
163
168
  this.clientIp = '' // 由外部 plugin(如 clientIp.client.js)非同步寫入,用於 ERR_998 Sentry 回報
164
169
  this.hostTester = null // 由外部 plugin(如 host.client.js)寫入,需提供 lastResults 陣列供 ERR_998 Sentry 回報使用
165
170
  this.initialize()
@@ -237,15 +242,70 @@ export default class Request {
237
242
  this.errorHandler = handler
238
243
  }
239
244
 
245
+ // 作廢指定 token:取消同 token 的在途請求,並讓之後仍帶此 token 的 send() 直接 reject。
246
+ // 只在 token 仍是目前身分時作用,舊身分遲到的錯誤不得影響新身分;重新登入後 token 改變即自然恢復
247
+ revokeToken(token) {
248
+ if (!token || token !== this.getToken()) return
249
+ this.revokedToken = token
250
+ this.activeRequests.forEach((item) => {
251
+ if (item.token === token) item.source.cancel(CANCELLED_MESSAGE)
252
+ })
253
+ }
254
+
255
+ // 回前景時先確認 token 仍有效:promise 結算(成功或失敗)或逾時即放行,之後的 send() 才會送出
256
+ beginTokenGate(promise, timeoutMs = 3000) {
257
+ let timer
258
+ const timeout = new Promise((resolve) => {
259
+ timer = setTimeout(resolve, timeoutMs)
260
+ })
261
+ const gate = Promise.race([Promise.resolve(promise).catch(() => {}), timeout]).then(() => {
262
+ clearTimeout(timer)
263
+ if (this.tokenGate === gate) this.tokenGate = null
264
+ })
265
+ this.tokenGate = gate
266
+ return gate
267
+ }
268
+
269
+ _throwIfRevoked() {
270
+ if (this.revokedToken && this.getToken() === this.revokedToken) {
271
+ throw new Error(CANCELLED_MESSAGE)
272
+ }
273
+ }
274
+
275
+ _isCancelled(error) {
276
+ return !!this.axios.isCancel?.(error) || error?.message === CANCELLED_MESSAGE
277
+ }
278
+
240
279
  async send(req, customToken) {
280
+ let tracked = null
281
+ let requestToken = null
241
282
  try {
242
283
  // 非 public 請求缺 token 直接 reject:reject → onError → leaving → 乾淨登出,且不送網路 → 洪水照止
243
284
  if (!req.isPublic && !this.getToken()) return Promise.reject({ code: 'ERR_A103' })
244
285
 
286
+ // 公開請求不帶 token,作廢期間 token 仍留在 storage 也不可誤拒(如 autoLogin 前後的 getConfig)
287
+ if (!req.isPublic) this._throwIfRevoked()
288
+
289
+ // /self 是 gate 自己的探測請求,不可等待 gate,否則死鎖
290
+ if (this.tokenGate && !req.isPublic && req.url.split('?')[0] !== 'self') {
291
+ await this.tokenGate
292
+ this._throwIfRevoked()
293
+ if (!this.getToken()) return Promise.reject({ code: 'ERR_A103' })
294
+ }
295
+
296
+ requestToken = req.isPublic ? null : this.getToken()
297
+
245
298
  // axios <0.22 對 cancelToken.promise 註冊的取消 handler 永不退訂(xhr.js 無 unsubscribe),
246
299
  // 預設掛長壽命共用 token 會使 handler 隨請求數線性累積(掛機過夜 OOM,TI-15555)。
247
- // 故預設不掛 cancelToken,僅呼叫端明確傳入 customToken(自行管理其生命週期)時才附加。
248
- const cancelToken = customToken
300
+ // 故不掛共用 token:呼叫端傳入 customToken 時自行管理其生命週期;否則帶 token 的請求用一次性 source,
301
+ // 結束即移出 activeRequests 隨請求回收
302
+ let cancelToken = customToken
303
+ if (!customToken && requestToken && this.axios.CancelToken) {
304
+ const source = this.axios.CancelToken.source()
305
+ tracked = { source, token: requestToken }
306
+ this.activeRequests.add(tracked)
307
+ cancelToken = source.token
308
+ }
249
309
 
250
310
  const requestConfig = this._createConfig(req, cancelToken)
251
311
  const configUrls = ['getConfig', 'get-config']
@@ -256,6 +316,10 @@ export default class Request {
256
316
  return data
257
317
  }
258
318
  } catch (error) {
319
+ if (this._isCancelled(error)) throw error
320
+ if (requestToken && error && typeof error === 'object') {
321
+ error.requestToken = requestToken
322
+ }
259
323
  if (error.code === 'ERR_F108') {
260
324
  this.isBlocked = true
261
325
  this.errorHandler && this.errorHandler(error)
@@ -270,6 +334,8 @@ export default class Request {
270
334
  }
271
335
  throw error
272
336
  }
337
+ } finally {
338
+ if (tracked) this.activeRequests.delete(tracked)
273
339
  }
274
340
  }
275
341