@cloudbase/auth 2.24.8-beta.0 → 2.24.9

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cloudbase/auth",
3
- "version": "2.24.8-beta.0",
3
+ "version": "2.24.9",
4
4
  "description": "cloudbase javascript sdk auth componets",
5
5
  "main": "dist/cjs/index.js",
6
6
  "module": "dist/esm/index.js",
@@ -32,14 +32,14 @@
32
32
  "license": "Apache-2.0",
33
33
  "dependencies": {
34
34
  "@cloudbase/adapter-wx_mp": "^1.2.1",
35
- "@cloudbase/oauth": "2.24.8-beta.0",
36
- "@cloudbase/utilities": "2.24.8-beta.0"
35
+ "@cloudbase/oauth": "2.24.9",
36
+ "@cloudbase/utilities": "2.24.9"
37
37
  },
38
38
  "devDependencies": {
39
- "@cloudbase/types": "2.24.8-beta.0",
39
+ "@cloudbase/types": "2.24.9",
40
40
  "@types/node": "^22.1.0",
41
41
  "terser-webpack-plugin": "^3.0.2",
42
42
  "webpack-bundle-analyzer": "^4.9.1"
43
43
  },
44
- "gitHead": "5bb4e5302174e93d2cca6ecbf943297ee64666ee"
44
+ "gitHead": "81fc02cfc00a4444a6fbfb91a02e4dcbbd76ede4"
45
45
  }
package/src/index.ts CHANGED
@@ -78,9 +78,8 @@ interface UserInfo {
78
78
  created_from?: string
79
79
  }
80
80
 
81
- export const eventBus = new CloudbaseEventEmitter()
82
81
 
83
- const onCredentialsError = (params) => {
82
+ const onCredentialsError = eventBus => (params) => {
84
83
  eventBus.fire(EVENTS.LOGIN_STATE_CHANGED, { ...params, eventType: LOGIN_STATE_CHANGED_TYPE.CREDENTIALS_ERROR })
85
84
  }
86
85
 
@@ -1218,7 +1217,7 @@ class Auth extends SbaseApi {
1218
1217
  }
1219
1218
 
1220
1219
  public async onLoginStateChanged(callback: Function) {
1221
- eventBus.on(EVENTS.LOGIN_STATE_CHANGED, async (params) => {
1220
+ this.config.eventBus?.on(EVENTS.LOGIN_STATE_CHANGED, async (params) => {
1222
1221
  // getLoginState会重复触发getCredentials,导致死循环,所以getCredentials出错不再出发getLoginState
1223
1222
  const loginState = params?.data?.eventType !== LOGIN_STATE_CHANGED_TYPE.CREDENTIALS_ERROR ? await this.getLoginState() : {}
1224
1223
  callback.call(this, { ...params, ...loginState })
@@ -1387,9 +1386,9 @@ class Auth extends SbaseApi {
1387
1386
  }
1388
1387
  }
1389
1388
 
1390
- eventBus.fire(EVENTS.LOGIN_STATE_CHANGED, { eventType: LOGIN_STATE_CHANGED_TYPE.SIGN_IN })
1389
+ this.config.eventBus?.fire(EVENTS.LOGIN_STATE_CHANGED, { eventType: LOGIN_STATE_CHANGED_TYPE.SIGN_IN })
1391
1390
 
1392
- eventBus.fire(EVENTS.AUTH_STATE_CHANGED, { event: AUTH_STATE_CHANGED_TYPE.SIGNED_IN })
1391
+ this.config.eventBus?.fire(EVENTS.AUTH_STATE_CHANGED, { event: AUTH_STATE_CHANGED_TYPE.SIGNED_IN })
1393
1392
  return loginState
1394
1393
  }
1395
1394
 
@@ -1444,7 +1443,7 @@ export function generateAuthInstance(
1444
1443
  i18n,
1445
1444
  accessKey,
1446
1445
  useWxCloud,
1447
- eventBus,
1446
+ eventBus: new CloudbaseEventEmitter(),
1448
1447
  }
1449
1448
 
1450
1449
  const oauthInstance = new CloudbaseOAuth(useAuthAdapter({
@@ -1459,7 +1458,7 @@ export function generateAuthInstance(
1459
1458
  captchaOptions: config?.captchaOptions,
1460
1459
  wxCloud: config?.wxCloud,
1461
1460
  adapter,
1462
- onCredentialsError,
1461
+ onCredentialsError: onCredentialsError(commonOpts.eventBus),
1463
1462
  headers: { 'X-SDK-Version': `@cloudbase/js-sdk/${config.sdkVersion}`, ...(config.headers || {}) },
1464
1463
  detectSessionInUrl: config.detectSessionInUrl,
1465
1464
  debug,
@@ -1489,7 +1488,7 @@ export function generateAuthInstance(
1489
1488
 
1490
1489
  if (data.type === OAUTH_TYPE.SIGN_IN) {
1491
1490
  if (error) {
1492
- eventBus.fire(EVENTS.AUTH_STATE_CHANGED, {
1491
+ commonOpts.eventBus.fire(EVENTS.AUTH_STATE_CHANGED, {
1493
1492
  event: AUTH_STATE_CHANGED_TYPE.SIGNED_IN,
1494
1493
  info: { ...data, error },
1495
1494
  })
@@ -1499,7 +1498,7 @@ export function generateAuthInstance(
1499
1498
  authInstance.createLoginState({}, { userInfo: data.user })
1500
1499
  }
1501
1500
  } else if (data.type === OAUTH_TYPE.BIND_IDENTITY) {
1502
- eventBus.fire(EVENTS.AUTH_STATE_CHANGED, {
1501
+ commonOpts.eventBus.fire(EVENTS.AUTH_STATE_CHANGED, {
1503
1502
  event: AUTH_STATE_CHANGED_TYPE.BIND_IDENTITY,
1504
1503
  info: { ...data, error },
1505
1504
  })
@@ -1509,55 +1508,65 @@ export function generateAuthInstance(
1509
1508
  return { authInstance, oauthInstance }
1510
1509
  }
1511
1510
 
1511
+ const NAMESPACE = 'auth'
1512
+
1512
1513
  const component: ICloudbaseComponent = {
1513
1514
  name: COMPONENT_NAME,
1514
- namespace: 'auth',
1515
+ namespace: NAMESPACE,
1515
1516
  entity(config?: TInitAuthOptions) {
1516
- if (this.authInstance && !config) {
1517
- // printWarn(ERRORS.INVALID_OPERATION, 'every cloudbase instance should has only one auth object')
1518
- return this.authInstance
1519
- }
1517
+ const auth = function (config?: TInitAuthOptions) {
1518
+ if (this.authInstance && !config) {
1519
+ // printWarn(ERRORS.INVALID_OPERATION, 'every cloudbase instance should has only one auth object')
1520
+ return this.authInstance
1521
+ }
1520
1522
 
1521
- config = config || {
1522
- region: '',
1523
- persistence: 'local',
1524
- apiPath: AUTH_API_PREFIX,
1525
- }
1526
- const { adapter } = this.platform
1527
- // 如不明确指定persistence则优先取各平台adapter首选,其次localStorage
1528
- const newPersistence = config.persistence || adapter.primaryStorage
1529
- if (newPersistence && newPersistence !== this.config.persistence) {
1530
- this.updateConfig({ persistence: newPersistence })
1531
- }
1523
+ config = config || {
1524
+ region: '',
1525
+ persistence: 'local',
1526
+ apiPath: AUTH_API_PREFIX,
1527
+ }
1528
+ const { adapter } = this.platform
1529
+ // 如不明确指定persistence则优先取各平台adapter首选,其次localStorage
1530
+ const newPersistence = config.persistence || adapter.primaryStorage
1531
+ if (newPersistence && newPersistence !== this.config.persistence) {
1532
+ this.updateConfig({ persistence: newPersistence })
1533
+ }
1532
1534
 
1533
- const { authInstance, oauthInstance } = generateAuthInstance(
1534
- {
1535
- wxCloud: this.config.wxCloud,
1536
- storage: this.config.storage,
1537
- ...config,
1538
- persistence: this.config.persistence,
1539
- i18n: this.config.i18n,
1540
- accessKey: this.config.accessKey,
1541
- useWxCloud: this.config.useWxCloud,
1542
- sdkVersion: this.version,
1543
- detectSessionInUrl: this.config.auth?.detectSessionInUrl,
1544
- },
1545
- {
1546
- env: this.config.env,
1547
- clientId: this.config.clientId,
1548
- apiOrigin: this.request.getBaseEndPoint(this.config.endPointMode || 'CLOUD_API'),
1549
- platform: this.platform,
1550
- cache: this.cache,
1551
- app: this,
1552
- debug: this.config.debug,
1553
- },
1554
- )
1535
+ const { authInstance, oauthInstance } = generateAuthInstance(
1536
+ {
1537
+ wxCloud: this.config.wxCloud,
1538
+ storage: this.config.storage,
1539
+ ...config,
1540
+ persistence: this.config.persistence,
1541
+ i18n: this.config.i18n,
1542
+ accessKey: this.config.accessKey,
1543
+ useWxCloud: this.config.useWxCloud,
1544
+ sdkVersion: this.version,
1545
+ detectSessionInUrl: this.config.auth?.detectSessionInUrl,
1546
+ },
1547
+ {
1548
+ env: this.config.env,
1549
+ clientId: this.config.clientId,
1550
+ apiOrigin: this.request.getBaseEndPoint(this.config.endPointMode || 'CLOUD_API'),
1551
+ platform: this.platform,
1552
+ cache: this.cache,
1553
+ app: this,
1554
+ debug: this.config.debug,
1555
+ },
1556
+ )
1555
1557
 
1556
- this.oauthInstance = oauthInstance
1558
+ this.oauthInstance = oauthInstance
1559
+
1560
+ this.authInstance = authInstance
1557
1561
 
1558
- this.authInstance = authInstance
1562
+ return this.authInstance
1563
+ }
1559
1564
 
1560
- return this.authInstance
1565
+ const authProto = auth.call(this, config)
1566
+ Object.assign(auth, authProto)
1567
+ Object.setPrototypeOf(auth, Object.getPrototypeOf(authProto))
1568
+ this[NAMESPACE] = auth
1569
+ return auth
1561
1570
  },
1562
1571
  }
1563
1572
 
package/src/sbaseApi.ts CHANGED
@@ -40,7 +40,7 @@ import {
40
40
  VerifyOAuthReq,
41
41
  VerifyOtpReq,
42
42
  } from './type'
43
- import { eventBus, LoginState, User } from '.'
43
+ import { LoginState, User } from '.'
44
44
  import { saveToBrowserSession, getBrowserSession, removeBrowserSession, addUrlSearch } from './utils'
45
45
  import { utils } from '@cloudbase/utilities'
46
46
  import { adapterForWxMp } from './utilities'
@@ -265,9 +265,9 @@ export class SbaseApi {
265
265
  await this.cache.removeStoreAsync(userInfoKey)
266
266
  this.setAccessKey()
267
267
 
268
- eventBus.fire(EVENTS.LOGIN_STATE_CHANGED, { eventType: LOGIN_STATE_CHANGED_TYPE.SIGN_OUT })
268
+ this.config.eventBus?.fire(EVENTS.LOGIN_STATE_CHANGED, { eventType: LOGIN_STATE_CHANGED_TYPE.SIGN_OUT })
269
269
 
270
- eventBus.fire(EVENTS.AUTH_STATE_CHANGED, { event: AUTH_STATE_CHANGED_TYPE.SIGNED_OUT })
270
+ this.config.eventBus?.fire(EVENTS.AUTH_STATE_CHANGED, { event: AUTH_STATE_CHANGED_TYPE.SIGNED_OUT })
271
271
 
272
272
  // res返回是为了兼容v2版本
273
273
  return { ...res, data: {}, error: null }
@@ -661,7 +661,7 @@ export class SbaseApi {
661
661
  verification_token: verificationTokenRes.verification_token,
662
662
  })
663
663
 
664
- eventBus.fire(EVENTS.AUTH_STATE_CHANGED, { event: AUTH_STATE_CHANGED_TYPE.PASSWORD_RECOVERY })
664
+ this.config.eventBus?.fire(EVENTS.AUTH_STATE_CHANGED, { event: AUTH_STATE_CHANGED_TYPE.PASSWORD_RECOVERY })
665
665
 
666
666
  const res = await this.signInWithPassword({
667
667
  email: isEmail ? emailOrPhone : undefined,
@@ -925,7 +925,7 @@ export class SbaseApi {
925
925
  const {
926
926
  data: { user },
927
927
  } = await this.getUser()
928
- eventBus.fire(EVENTS.AUTH_STATE_CHANGED, { event: AUTH_STATE_CHANGED_TYPE.USER_UPDATED })
928
+ this.config.eventBus?.fire(EVENTS.AUTH_STATE_CHANGED, { event: AUTH_STATE_CHANGED_TYPE.USER_UPDATED })
929
929
 
930
930
  return { data: { user }, error: null }
931
931
  } catch (error) {
@@ -940,7 +940,7 @@ export class SbaseApi {
940
940
  const {
941
941
  data: { user },
942
942
  } = await this.getUser()
943
- eventBus.fire(EVENTS.AUTH_STATE_CHANGED, { event: AUTH_STATE_CHANGED_TYPE.USER_UPDATED })
943
+ this.config.eventBus?.fire(EVENTS.AUTH_STATE_CHANGED, { event: AUTH_STATE_CHANGED_TYPE.USER_UPDATED })
944
944
 
945
945
  return { data: { user, ...extraRes }, error: null }
946
946
  } catch (error) {
@@ -1158,7 +1158,7 @@ export class SbaseApi {
1158
1158
 
1159
1159
  const { data: { session } = {} } = await this.getSession()
1160
1160
 
1161
- eventBus.fire(EVENTS.AUTH_STATE_CHANGED, { event: AUTH_STATE_CHANGED_TYPE.SIGNED_IN })
1161
+ this.config.eventBus?.fire(EVENTS.AUTH_STATE_CHANGED, { event: AUTH_STATE_CHANGED_TYPE.SIGNED_IN })
1162
1162
 
1163
1163
  return { data: { user: session.user, session }, error: null }
1164
1164
  } catch (error) {
@@ -1398,7 +1398,7 @@ export class SbaseApi {
1398
1398
  try {
1399
1399
  const credentials: Credentials = await this.oauthInstance.oauth2client.localCredentials.getCredentials()
1400
1400
  if (credentials) {
1401
- eventBus.fire(EVENTS.AUTH_STATE_CHANGED, { event: AUTH_STATE_CHANGED_TYPE.INITIAL_SESSION })
1401
+ this.config.eventBus?.fire(EVENTS.AUTH_STATE_CHANGED, { event: AUTH_STATE_CHANGED_TYPE.INITIAL_SESSION })
1402
1402
  }
1403
1403
  } catch (error) {
1404
1404
  // Ignore errors when checking for existing credentials
@@ -1408,7 +1408,7 @@ export class SbaseApi {
1408
1408
  }
1409
1409
 
1410
1410
  private setupListeners() {
1411
- eventBus.on(EVENTS.AUTH_STATE_CHANGED, async (params) => {
1411
+ this.config.eventBus?.on(EVENTS.AUTH_STATE_CHANGED, async (params) => {
1412
1412
  const event = params?.data?.event
1413
1413
  const info = params?.data?.info
1414
1414
  const {
package/src/type.ts CHANGED
@@ -1,4 +1,27 @@
1
- import type { authModels, AuthError, OAUTH_TYPE } from '@cloudbase/oauth'
1
+ import type { authModels, OAUTH_TYPE } from '@cloudbase/oauth'
2
+
3
+ class AuthError extends Error {
4
+ /**
5
+ * Error code associated with the error. Most errors coming from
6
+ * HTTP responses will have a code, though some errors that occur
7
+ * before a response is received will not have one present. In that
8
+ * case {@link #status} will also be undefined.
9
+ */
10
+ code: (string & {}) | undefined
11
+
12
+ /** HTTP status code that caused the error. */
13
+ status: number | undefined
14
+
15
+ protected __isAuthError = true
16
+
17
+ constructor(error) {
18
+ super(error.error_description || error.message)
19
+ this.name = 'AuthError'
20
+ this.status = error.error
21
+ this.code = error.error_code
22
+ }
23
+ }
24
+
2
25
 
3
26
  export interface SignInAnonymouslyReq {
4
27
  provider_token?: string // 提供令牌