@cloudbase/auth 2.5.48-beta.0 → 2.5.49-beta.1

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