@cloudbase/auth 2.5.49-beta.0 → 2.5.49-beta.2

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/src/index.ts CHANGED
@@ -1,9 +1,9 @@
1
- import { ICloudbase } from '@cloudbase/types'
2
- import { utils, constants, helpers, events } from '@cloudbase/utilities'
3
- import { ICloudbaseCache } from '@cloudbase/types/cache'
4
- import { ICloudbaseRequest } from '@cloudbase/types/request'
5
- import { ICloudbaseAuthConfig, IUser, IUserInfo, ILoginState } from '@cloudbase/types/auth'
6
- import { ICloudbaseComponent } from '@cloudbase/types/component'
1
+ import type { ICloudbase, ICloudbaseConfig } from '@cloudbase/types'
2
+ import { utils, constants, helpers, events, cache } from '@cloudbase/utilities'
3
+ import type { ICloudbaseCache } from '@cloudbase/types/cache'
4
+ import type { ICloudbaseRequest } from '@cloudbase/types/request'
5
+ import type { ICloudbaseAuthConfig, IUser, IUserInfo, ILoginState } from '@cloudbase/types/auth'
6
+ import type { ICloudbaseComponent } from '@cloudbase/types/component'
7
7
 
8
8
  import { authModels, CloudbaseOAuth, AuthOptions, Credentials } from '@cloudbase/oauth'
9
9
  import { GrantProviderTokenResponse, ProviderSubType } from '@cloudbase/oauth/dist/cjs/auth/models'
@@ -17,6 +17,7 @@ const { printWarn, throwError } = utils
17
17
  const { ERRORS, COMMUNITY_SITE_URL } = constants
18
18
  const { catchErrorsDecorator } = helpers
19
19
  const { CloudbaseEventEmitter } = events
20
+ const { CloudbaseCache } = cache
20
21
 
21
22
  const COMPONENT_NAME = 'auth'
22
23
 
@@ -306,7 +307,7 @@ class Auth {
306
307
  ],
307
308
  })
308
309
  public async bindPhoneNumber(params: authModels.BindPhoneRequest) {
309
- return this.oauthInstance.authApi.bindPhone(params)
310
+ return this.oauthInstance.authApi.editContact(params)
310
311
  }
311
312
 
312
313
  /**
@@ -342,7 +343,7 @@ class Auth {
342
343
  ],
343
344
  })
344
345
  public bindEmail(params: authModels.BindEmailRequest) {
345
- return this.oauthInstance.authApi.bindEmail(params)
346
+ return this.oauthInstance.authApi.editContact(params)
346
347
  }
347
348
 
348
349
  /**
@@ -1095,7 +1096,7 @@ class Auth {
1095
1096
  }
1096
1097
 
1097
1098
  public async getMiniProgramQrCodeStatus(params: authModels.GetMiniProgramQrCodeStatusRequest,): Promise<authModels.GetMiniProgramQrCodeStatusResponse> {
1098
- return this.oauthInstance.authApi.checkMiniProgramCode(params)
1099
+ return this.oauthInstance.authApi.getMiniProgramQrCodeStatus(params)
1099
1100
  }
1100
1101
 
1101
1102
  private async createLoginState(params?: { version?: string; query?: any }): Promise<LoginState> {
@@ -1112,10 +1113,64 @@ class Auth {
1112
1113
  }
1113
1114
  }
1114
1115
 
1116
+ type TInitAuthOptions = Pick<ICloudbaseAuthConfig, 'region' | 'persistence'> & Partial<AuthOptions>
1117
+
1118
+ export function generateAuthInstance(
1119
+ config: TInitAuthOptions,
1120
+ options?: {
1121
+ clientId: ICloudbaseConfig['clientId']
1122
+ env: ICloudbaseConfig['env']
1123
+ apiOrigin: string
1124
+ cache?: ICloudbaseCache
1125
+ platform?: ICloudbase['platform']
1126
+ app?: ICloudbase
1127
+ debug?: ICloudbaseAuthConfig['debug']
1128
+ },
1129
+ ) {
1130
+ const { runtime } = options?.platform || {}
1131
+
1132
+ const { env, clientId, debug, cache, app: cloudbase } = options || {}
1133
+ let { apiOrigin } = options || {}
1134
+ if (!apiOrigin) {
1135
+ apiOrigin = `https://${env}.${config.region}.tcb-api.tencentcloudapi.com`
1136
+ }
1137
+ const oauthInstance = new CloudbaseOAuth(useAuthAdapter({
1138
+ env,
1139
+ clientId,
1140
+ apiOrigin,
1141
+ // @todo 以下最好走adaptor处理,目前oauth模块没按adaptor接口设计
1142
+ storage: config?.storage,
1143
+ baseRequest: config?.baseRequest,
1144
+ request: config?.request,
1145
+ anonymousSignInFunc: config?.anonymousSignInFunc,
1146
+ captchaOptions: config?.captchaOptions,
1147
+ wxCloud: config?.wxCloud,
1148
+ }),)
1149
+
1150
+ const authInstance = new Auth({
1151
+ env,
1152
+ region: config.region,
1153
+ persistence: config.persistence,
1154
+ debug,
1155
+ cache:
1156
+ cache
1157
+ || new CloudbaseCache({
1158
+ persistence: config.persistence,
1159
+ keys: { userInfoKey: `user_info_${env}` },
1160
+ platformInfo: options?.platform,
1161
+ }),
1162
+ runtime: runtime || 'web',
1163
+ _fromApp: cloudbase,
1164
+ oauthInstance,
1165
+ })
1166
+
1167
+ return { authInstance, oauthInstance }
1168
+ }
1169
+
1115
1170
  const component: ICloudbaseComponent = {
1116
1171
  name: COMPONENT_NAME,
1117
1172
  namespace: 'auth',
1118
- entity(config: Pick<ICloudbaseAuthConfig, 'region' | 'persistence'> & Partial<AuthOptions> = {
1173
+ entity(config: TInitAuthOptions = {
1119
1174
  region: '',
1120
1175
  persistence: 'local',
1121
1176
  },) {
@@ -1123,41 +1178,34 @@ const component: ICloudbaseComponent = {
1123
1178
  printWarn(ERRORS.INVALID_OPERATION, 'every cloudbase instance should has only one auth object')
1124
1179
  return this.authInstance
1125
1180
  }
1126
- const { adapter, runtime } = this.platform
1181
+ const { adapter } = this.platform
1127
1182
  // 如不明确指定persistence则优先取各平台adapter首选,其次localStorage
1128
1183
  const newPersistence = config.persistence || adapter.primaryStorage
1129
1184
  if (newPersistence && newPersistence !== this.config.persistence) {
1130
1185
  this.updateConfig({ persistence: newPersistence })
1131
1186
  }
1132
1187
 
1133
- const { env, persistence, debug, clientId, storage, wxCloud } = this.config
1134
- const oauthInstance = new CloudbaseOAuth(useAuthAdapter({
1135
- env,
1136
- clientId,
1137
- apiOrigin: this.request.getBaseEndPoint(),
1138
- // @todo 以下最好走adaptor处理,目前oauth模块没按adaptor接口设计
1139
- storage: config?.storage || storage,
1140
- baseRequest: config?.baseRequest,
1141
- request: config?.request,
1142
- anonymousSignInFunc: config?.anonymousSignInFunc,
1143
- captchaOptions: config?.captchaOptions,
1144
- wxCloud: config?.wxCloud || wxCloud,
1145
- }),)
1188
+ const { authInstance, oauthInstance } = generateAuthInstance(
1189
+ {
1190
+ wxCloud: this.config.wxCloud,
1191
+ storage: this.config.storage,
1192
+ ...config,
1193
+ persistence: this.config.persistence,
1194
+ },
1195
+ {
1196
+ env: this.config.env,
1197
+ clientId: this.config.clientId,
1198
+ apiOrigin: this.request.getBaseEndPoint(),
1199
+ platform: this.platform,
1200
+ cache: this.cache,
1201
+ app: this,
1202
+ debug: this.config.debug,
1203
+ },
1204
+ )
1146
1205
 
1147
1206
  this.oauthInstance = oauthInstance
1148
1207
 
1149
- this.authInstance = new Auth({
1150
- env,
1151
- region: config.region,
1152
- persistence,
1153
- debug,
1154
- cache: this.cache,
1155
- // request: this.request,
1156
- runtime,
1157
- _fromApp: this,
1158
- // oauthInstance: this.oauthInstance || (this as any).oauth()
1159
- oauthInstance,
1160
- })
1208
+ this.authInstance = authInstance
1161
1209
 
1162
1210
  return this.authInstance
1163
1211
  },