@cloudbase/auth 2.4.4-beta.0 → 2.5.0-beta.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/src/index.ts CHANGED
@@ -1,47 +1,47 @@
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 { 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'
7
7
 
8
8
  import { authModels, CloudbaseOAuth } from '@cloudbase/oauth'
9
9
 
10
- declare const cloudbase: ICloudbase;
10
+ declare const cloudbase: ICloudbase
11
11
 
12
- const { printWarn, throwError } = utils;
13
- const { ERRORS, COMMUNITY_SITE_URL } = constants;
14
- const { catchErrorsDecorator } = helpers;
15
- const { CloudbaseEventEmitter } = events;
12
+ const { printWarn, throwError } = utils
13
+ const { ERRORS, COMMUNITY_SITE_URL } = constants
14
+ const { catchErrorsDecorator } = helpers
15
+ const { CloudbaseEventEmitter } = events
16
16
 
17
- const COMPONENT_NAME = 'auth';
17
+ const COMPONENT_NAME = 'auth'
18
18
 
19
19
  const EVENTS = {
20
20
  // 登录态改变后触发
21
21
  LOGIN_STATE_CHANGED: 'loginStateChanged',
22
- };
22
+ }
23
23
 
24
24
  interface UserInfo {
25
- uid?: string;
26
- gender?: string;
27
- picture?: string;
28
- email?: string;
29
- email_verified?: boolean;
30
- phone_number?: string;
31
- username?: string;
32
- name?: string;
33
- birthdate?: string;
34
- zoneinfo?: string;
35
- locale?: string;
36
- sub?: string;
37
- created_from?: string;
25
+ uid?: string
26
+ gender?: string
27
+ picture?: string
28
+ email?: string
29
+ email_verified?: boolean
30
+ phone_number?: string
31
+ username?: string
32
+ name?: string
33
+ birthdate?: string
34
+ zoneinfo?: string
35
+ locale?: string
36
+ sub?: string
37
+ created_from?: string
38
38
  }
39
39
 
40
- const eventBus = new CloudbaseEventEmitter();
40
+ const eventBus = new CloudbaseEventEmitter()
41
41
 
42
42
  interface IUserOptions {
43
- cache: ICloudbaseCache;
44
- // request: ICloudbaseRequest;
43
+ cache: ICloudbaseCache
44
+ // request: ICloudbaseRequest
45
45
  oauthInstance: CloudbaseOAuth
46
46
  }
47
47
 
@@ -50,68 +50,68 @@ class User implements IUser {
50
50
  public gender?: string;
51
51
  public picture?: string;
52
52
  public email?: string;
53
- public email_verified?: boolean;
54
- public phone_number?: string;
53
+ public emailVerified?: boolean;
54
+ public phoneNumber?: string;
55
55
  public username?: string;
56
56
  public name?: string;
57
57
  public providers?: {
58
- id?: string;
59
- provider_user_id?: string;
60
- name?: string;
61
- }[]
58
+ id?: string
59
+ providerUserId?: string
60
+ name?: string
61
+ }[];
62
62
  public birthdate?: string;
63
63
  public zoneinfo?: string;
64
64
  public locale?: string;
65
65
  public sub?: string;
66
- public created_from?: string;
66
+ public createdFrom?: string;
67
67
 
68
- private _cache: ICloudbaseCache;
69
- private _oauthInstance: CloudbaseOAuth // CloudbaseOAuth 类型
68
+ private cache: ICloudbaseCache;
69
+ private oauthInstance: CloudbaseOAuth; // CloudbaseOAuth 类型
70
70
 
71
71
  constructor(options: IUserOptions) {
72
- const { cache, oauthInstance } = options;
73
- this._cache = cache;
74
- this._oauthInstance = oauthInstance
72
+ const { cache, oauthInstance } = options
73
+ this.cache = cache
74
+ this.oauthInstance = oauthInstance
75
75
 
76
- this._setUserInfo();
76
+ this.setUserInfo()
77
77
  }
78
78
  /**
79
79
  * 获取本地用户信息-同步
80
80
  */
81
81
  public async checkLocalInfo() {
82
- this.uid = this._getLocalUserInfo('uid') as string;
83
- this.gender = this._getLocalUserInfo('gender') as string;
84
- this.picture = this._getLocalUserInfo('picture') as string;
85
- this.email = this._getLocalUserInfo('email') as string;
86
- this.email_verified = this._getLocalUserInfo('email_verified') as boolean;
87
- this.phone_number = this._getLocalUserInfo('phone_number') as string
88
- this.username = this._getLocalUserInfo('username') as string
89
- this.name = this._getLocalUserInfo('name') as string
90
- this.birthdate = this._getLocalUserInfo('birthdate') as string
91
- this.zoneinfo = this._getLocalUserInfo('zoneinfo') as string
92
- this.locale = this._getLocalUserInfo('locale') as string
93
- this.sub = this._getLocalUserInfo('sub') as string
94
- this.created_from = this._getLocalUserInfo('created_from') as string
95
- this.providers = this._getLocalUserInfo('providers') as any
82
+ this.uid = this.getLocalUserInfo('uid') as string
83
+ this.gender = this.getLocalUserInfo('gender') as string
84
+ this.picture = this.getLocalUserInfo('picture') as string
85
+ this.email = this.getLocalUserInfo('email') as string
86
+ this.emailVerified = this.getLocalUserInfo('email_verified') as boolean
87
+ this.phoneNumber = this.getLocalUserInfo('phone_number') as string
88
+ this.username = this.getLocalUserInfo('username') as string
89
+ this.name = this.getLocalUserInfo('name') as string
90
+ this.birthdate = this.getLocalUserInfo('birthdate') as string
91
+ this.zoneinfo = this.getLocalUserInfo('zoneinfo') as string
92
+ this.locale = this.getLocalUserInfo('locale') as string
93
+ this.sub = this.getLocalUserInfo('sub') as string
94
+ this.createdFrom = this.getLocalUserInfo('created_from') as string
95
+ this.providers = this.getLocalUserInfo('providers') as any
96
96
  }
97
97
  /**
98
98
  * 获取本地用户信息-异步
99
99
  */
100
100
  public async checkLocalInfoAsync() {
101
- this.uid = await this._getLocalUserInfoAsync('uid');
102
- this.gender = await this._getLocalUserInfoAsync('gender');
103
- this.picture = this._getLocalUserInfo('picture') as string;
104
- this.email = await this._getLocalUserInfoAsync('email');
105
- this.email_verified = this._getLocalUserInfo('email_verified') as boolean;
106
- this.phone_number = this._getLocalUserInfo('phone_number') as string
107
- this.username = await this._getLocalUserInfoAsync('username')
108
- this.name = this._getLocalUserInfo('name') as string
109
- this.birthdate = this._getLocalUserInfo('birthdate') as string
110
- this.zoneinfo = this._getLocalUserInfo('zoneinfo') as string
111
- this.locale = this._getLocalUserInfo('locale') as string
112
- this.sub = this._getLocalUserInfo('sub') as string
113
- this.created_from = this._getLocalUserInfo('created_from') as string
114
- this.providers = this._getLocalUserInfo('providers') as any
101
+ this.uid = await this.getLocalUserInfoAsync('uid')
102
+ this.gender = await this.getLocalUserInfoAsync('gender')
103
+ this.picture = this.getLocalUserInfo('picture') as string
104
+ this.email = await this.getLocalUserInfoAsync('email')
105
+ this.emailVerified = this.getLocalUserInfo('email_verified') as boolean
106
+ this.phoneNumber = this.getLocalUserInfo('phone_number') as string
107
+ this.username = await this.getLocalUserInfoAsync('username')
108
+ this.name = this.getLocalUserInfo('name') as string
109
+ this.birthdate = this.getLocalUserInfo('birthdate') as string
110
+ this.zoneinfo = this.getLocalUserInfo('zoneinfo') as string
111
+ this.locale = this.getLocalUserInfo('locale') as string
112
+ this.sub = this.getLocalUserInfo('sub') as string
113
+ this.createdFrom = this.getLocalUserInfo('created_from') as string
114
+ this.providers = this.getLocalUserInfo('providers') as any
115
115
  }
116
116
 
117
117
 
@@ -125,14 +125,14 @@ class User implements IUser {
125
125
  '请确认以下各项:',
126
126
  ' 1 - 调用 User.update() 的语法或参数是否正确',
127
127
  ' 2 - 用户信息中是否包含非法值',
128
- `如果问题依然存在,建议到官方问答社区提问或寻找帮助:${COMMUNITY_SITE_URL}`
129
- ]
128
+ `如果问题依然存在,建议到官方问答社区提问或寻找帮助:${COMMUNITY_SITE_URL}`,
129
+ ],
130
130
  })
131
131
  public async update(userInfo: authModels.UserProfile): Promise<void> {
132
- // const { name, gender, avatarUrl, province, country, city } = userInfo;
133
- const newUserInfo = await this._oauthInstance.authApi.setUserProfile({ ...userInfo })
132
+ // const { name, gender, avatarUrl, province, country, city } = userInfo
133
+ const newUserInfo = await this.oauthInstance.authApi.setUserProfile({ ...userInfo })
134
134
 
135
- this._setLocalUserInfo(newUserInfo);
135
+ this.setLocalUserInfo(newUserInfo)
136
136
  }
137
137
  /**
138
138
  * 更新密码
@@ -145,13 +145,13 @@ class User implements IUser {
145
145
  '请确认以下各项:',
146
146
  ' 1 - 调用 User.updatePassword() 的语法或参数是否正确',
147
147
  ' 3 - 新密码中是否包含非法字符',
148
- `如果问题依然存在,建议到官方问答社区提问或寻找帮助:${COMMUNITY_SITE_URL}`
149
- ]
148
+ `如果问题依然存在,建议到官方问答社区提问或寻找帮助:${COMMUNITY_SITE_URL}`,
149
+ ],
150
150
  })
151
151
  public updatePassword(newPassword: string, oldPassword: string) {
152
- return this._oauthInstance.authApi.updatePasswordByOld({
152
+ return this.oauthInstance.authApi.updatePasswordByOld({
153
153
  old_password: oldPassword,
154
- new_password: newPassword
154
+ new_password: newPassword,
155
155
  })
156
156
  }
157
157
 
@@ -165,16 +165,16 @@ class User implements IUser {
165
165
  '请确认以下各项:',
166
166
  ' 1 - 调用 User.updateUsername() 的语法或参数是否正确',
167
167
  ' 2 - 当前环境是否开通了用户名密码登录',
168
- `如果问题依然存在,建议到官方问答社区提问或寻找帮助:${COMMUNITY_SITE_URL}`
169
- ]
168
+ `如果问题依然存在,建议到官方问答社区提问或寻找帮助:${COMMUNITY_SITE_URL}`,
169
+ ],
170
170
  })
171
171
  public updateUsername(username: string) {
172
172
  if (typeof username !== 'string') {
173
- throwError(ERRORS.INVALID_PARAMS, 'username must be a string');
173
+ throwError(ERRORS.INVALID_PARAMS, 'username must be a string')
174
174
  }
175
175
 
176
176
  return this.update({
177
- username
177
+ username,
178
178
  })
179
179
  }
180
180
 
@@ -186,31 +186,31 @@ class User implements IUser {
186
186
  messages: [
187
187
  '请确认以下各项:',
188
188
  ' 1 - 调用 User.refresh() 的语法或参数是否正确',
189
- `如果问题依然存在,建议到官方问答社区提问或寻找帮助:${COMMUNITY_SITE_URL}`
190
- ]
189
+ `如果问题依然存在,建议到官方问答社区提问或寻找帮助:${COMMUNITY_SITE_URL}`,
190
+ ],
191
191
  })
192
192
  public async refresh(): Promise<IUserInfo> {
193
- const newUserInfo = await this._oauthInstance.authApi.getUserInfo()
194
- this._setLocalUserInfo(newUserInfo);
195
- return newUserInfo;
193
+ const newUserInfo = await this.oauthInstance.authApi.getUserInfo()
194
+ this.setLocalUserInfo(newUserInfo)
195
+ return newUserInfo
196
196
  }
197
197
 
198
- private _getLocalUserInfo(key: string): string | boolean {
199
- const { userInfoKey } = this._cache.keys;
200
- const userInfo = this._cache.getStore(userInfoKey);
201
- return userInfo[key];
198
+ private getLocalUserInfo(key: string): string | boolean {
199
+ const { userInfoKey } = this.cache.keys
200
+ const userInfo = this.cache.getStore(userInfoKey)
201
+ return userInfo[key]
202
202
  }
203
203
 
204
- private async _getLocalUserInfoAsync(key: string): Promise<string> {
205
- const { userInfoKey } = this._cache.keys;
206
- const userInfo = await this._cache.getStoreAsync(userInfoKey);
207
- return userInfo[key];
204
+ private async getLocalUserInfoAsync(key: string): Promise<string> {
205
+ const { userInfoKey } = this.cache.keys
206
+ const userInfo = await this.cache.getStoreAsync(userInfoKey)
207
+ return userInfo[key]
208
208
  }
209
209
 
210
- private _setUserInfo() {
211
- const { userInfoKey } = this._cache.keys;
212
- const userInfo = this._cache.getStore(userInfoKey);
213
- [
210
+ private setUserInfo() {
211
+ const { userInfoKey } = this.cache.keys
212
+ const userInfo = this.cache.getStore(userInfoKey)
213
+ ;[
214
214
  'uid',
215
215
  'email',
216
216
  'name',
@@ -224,65 +224,64 @@ class User implements IUser {
224
224
  'sub',
225
225
  'created_from',
226
226
  'providers',
227
- 'username'
228
- ].forEach(infoKey => {
229
- this[infoKey] = userInfo[infoKey];
230
- });
227
+ 'username',
228
+ ].forEach((infoKey) => {
229
+ this[infoKey] = userInfo[infoKey]
230
+ })
231
231
  }
232
232
 
233
- private _setLocalUserInfo(userInfo: any) {
234
- const { userInfoKey } = this._cache.keys;
235
- this._cache.setStore(userInfoKey, userInfo);
236
- this._setUserInfo();
233
+ private setLocalUserInfo(userInfo: any) {
234
+ const { userInfoKey } = this.cache.keys
235
+ this.cache.setStore(userInfoKey, userInfo)
236
+ this.setUserInfo()
237
237
  }
238
238
  }
239
239
  interface ILoginStateOptions extends IUserOptions {
240
- envId: string;
240
+ envId: string
241
241
  }
242
242
  export class LoginState implements ILoginState {
243
243
  public user: IUser;
244
- public oauthLoginState: any
244
+ public oauthLoginState: any;
245
245
 
246
- private _oauthInstance: CloudbaseOAuth
247
- private _cache: ICloudbaseCache;
246
+ private oauthInstance: CloudbaseOAuth;
247
+ private cache: ICloudbaseCache;
248
248
 
249
249
  constructor(options: ILoginStateOptions) {
250
- const { envId, cache, oauthInstance } = options;
250
+ const { envId, cache, oauthInstance } = options
251
251
  if (!envId) {
252
- throwError(ERRORS.INVALID_PARAMS, 'envId is not defined');
252
+ throwError(ERRORS.INVALID_PARAMS, 'envId is not defined')
253
253
  }
254
- this._cache = cache;
255
- this._oauthInstance = oauthInstance
254
+ this.cache = cache
255
+ this.oauthInstance = oauthInstance
256
256
 
257
257
  this.user = new User({
258
- cache: this._cache,
259
- oauthInstance
260
- });
258
+ cache: this.cache,
259
+ oauthInstance,
260
+ })
261
261
  }
262
262
 
263
263
  public checkLocalState() {
264
- this.oauthLoginState = this._oauthInstance?.authApi.hasLoginStateSync()
265
- this.user.checkLocalInfo();
264
+ this.oauthLoginState = this.oauthInstance?.authApi.hasLoginStateSync()
265
+ this.user.checkLocalInfo()
266
266
  }
267
267
 
268
268
  public async checkLocalStateAsync() {
269
- await this._oauthInstance?.authApi.getLoginState()
270
- await this.user.checkLocalInfoAsync();
269
+ await this.oauthInstance?.authApi.getLoginState()
270
+ await this.user.checkLocalInfoAsync()
271
271
  }
272
272
  }
273
273
 
274
274
  class Auth {
275
- private readonly _config: ICloudbaseAuthConfig;
276
- private readonly _cache: ICloudbaseCache
277
- // private readonly _request: ICloudbaseRequest;
275
+ private readonly config: ICloudbaseAuthConfig;
276
+ private readonly cache: ICloudbaseCache;
278
277
 
279
- private _oauthInstance: CloudbaseOAuth
278
+ private oauthInstance: CloudbaseOAuth;
280
279
 
281
- constructor(config: ICloudbaseAuthConfig & { cache: ICloudbaseCache, request?: ICloudbaseRequest, runtime?: string }) {
282
- this._config = config;
283
- this._cache = config.cache;
284
- // this._request = config.request;
285
- this._oauthInstance = config.oauthInstance
280
+ constructor(config: ICloudbaseAuthConfig
281
+ & { cache: ICloudbaseCache, request?: ICloudbaseRequest, runtime?: string }) {
282
+ this.config = config
283
+ this.cache = config.cache
284
+ this.oauthInstance = config.oauthInstance
286
285
  }
287
286
 
288
287
  /**
@@ -296,11 +295,11 @@ class Auth {
296
295
  '请确认以下各项:',
297
296
  ' 1 - 调用 auth().bindPhoneNumber() 的语法或参数是否正确',
298
297
  ' 2 - 当前环境是否开通了短信验证码登录',
299
- `如果问题依然存在,建议到官方问答社区提问或寻找帮助:${COMMUNITY_SITE_URL}`
300
- ]
298
+ `如果问题依然存在,建议到官方问答社区提问或寻找帮助:${COMMUNITY_SITE_URL}`,
299
+ ],
301
300
  })
302
301
  public async bindPhoneNumber(params: authModels.BindPhoneRequest) {
303
- return this._oauthInstance.authApi.bindPhone(params)
302
+ return this.oauthInstance.authApi.bindPhone(params)
304
303
  }
305
304
 
306
305
  /**
@@ -313,11 +312,11 @@ class Auth {
313
312
  '请确认以下各项:',
314
313
  ' 1 - 调用 auth().unbindProvider() 的语法或参数是否正确',
315
314
  ' 2 - 当前账户是否已经与此登录方式解绑',
316
- `如果问题依然存在,建议到官方问答社区提问或寻找帮助:${COMMUNITY_SITE_URL}`
317
- ]
315
+ `如果问题依然存在,建议到官方问答社区提问或寻找帮助:${COMMUNITY_SITE_URL}`,
316
+ ],
318
317
  })
319
318
  public async unbindProvider(params: authModels.UnbindProviderRequest): Promise<void> {
320
- return this._oauthInstance.authApi.unbindProvider(params)
319
+ return this.oauthInstance.authApi.unbindProvider(params)
321
320
  }
322
321
 
323
322
  /**
@@ -332,11 +331,11 @@ class Auth {
332
331
  '请确认以下各项:',
333
332
  ' 1 - 调用 auth().bindEmail() 的语法或参数是否正确',
334
333
  ' 2 - 当前环境是否开通了邮箱密码登录',
335
- `如果问题依然存在,建议到官方问答社区提问或寻找帮助:${COMMUNITY_SITE_URL}`
336
- ]
334
+ `如果问题依然存在,建议到官方问答社区提问或寻找帮助:${COMMUNITY_SITE_URL}`,
335
+ ],
337
336
  })
338
337
  public bindEmail(params: authModels.BindEmailRequest) {
339
- return this._oauthInstance.authApi.bindEmail(params)
338
+ return this.oauthInstance.authApi.bindEmail(params)
340
339
  }
341
340
 
342
341
  /**
@@ -351,11 +350,11 @@ class Auth {
351
350
  '请确认以下各项:',
352
351
  ' 1 - 调用 auth().verify() 的语法或参数是否正确',
353
352
  ' 2 - 当前环境是否开通了手机验证码/邮箱登录',
354
- `如果问题依然存在,建议到官方问答社区提问或寻找帮助:${COMMUNITY_SITE_URL}`
355
- ]
353
+ `如果问题依然存在,建议到官方问答社区提问或寻找帮助:${COMMUNITY_SITE_URL}`,
354
+ ],
356
355
  })
357
356
  public async verify(params: authModels.VerifyRequest): Promise<authModels.VerifyResponse> {
358
- return this._oauthInstance.authApi.verify(params)
357
+ return this.oauthInstance.authApi.verify(params)
359
358
  }
360
359
 
361
360
  /**
@@ -370,32 +369,32 @@ class Auth {
370
369
  '请确认以下各项:',
371
370
  ' 1 - 调用 auth().getVerification() 的语法或参数是否正确',
372
371
  ' 2 - 当前环境是否开通了手机验证码/邮箱登录',
373
- `如果问题依然存在,建议到官方问答社区提问或寻找帮助:${COMMUNITY_SITE_URL}`
374
- ]
372
+ `如果问题依然存在,建议到官方问答社区提问或寻找帮助:${COMMUNITY_SITE_URL}`,
373
+ ],
375
374
  })
376
- public async getVerification(
377
- params: authModels.GetVerificationRequest,
378
- ): Promise<authModels.GetVerificationResponse> {
379
- return this._oauthInstance.authApi.getVerification(params)
375
+ public async getVerification(params: authModels.GetVerificationRequest): Promise<authModels.GetVerificationResponse> {
376
+ return this.oauthInstance.authApi.getVerification(params)
380
377
  }
381
378
 
382
379
  /**
383
380
  * 获取当前登录的用户信息-同步
384
381
  */
385
382
  get currentUser() {
386
- if (this._cache.mode === 'async') {
383
+ if (this.cache.mode === 'async') {
387
384
  // async storage的平台调用此API提示
388
- printWarn(ERRORS.INVALID_OPERATION, 'current platform\'s storage is asynchronous, please use getCurrentUser insteed');
389
- return;
385
+ printWarn(
386
+ ERRORS.INVALID_OPERATION,
387
+ 'current platform\'s storage is asynchronous, please use getCurrentUser insteed'
388
+ )
389
+ return
390
390
  }
391
391
 
392
- const loginState = this.hasLoginState();
392
+ const loginState = this.hasLoginState()
393
393
 
394
394
  if (loginState) {
395
- return loginState.user || null;
396
- } else {
397
- return null;
395
+ return loginState.user || null
398
396
  }
397
+ return null
399
398
  }
400
399
 
401
400
  /**
@@ -406,17 +405,16 @@ class Auth {
406
405
  messages: [
407
406
  '请确认以下各项:',
408
407
  ' 1 - 调用 auth().getCurrenUser() 的语法或参数是否正确',
409
- `如果问题依然存在,建议到官方问答社区提问或寻找帮助:${COMMUNITY_SITE_URL}`
410
- ]
408
+ `如果问题依然存在,建议到官方问答社区提问或寻找帮助:${COMMUNITY_SITE_URL}`,
409
+ ],
411
410
  })
412
411
  public async getCurrentUser() {
413
- const loginState = await this.getLoginState();
412
+ const loginState = await this.getLoginState()
414
413
  if (loginState) {
415
- await loginState.user.checkLocalInfoAsync();
416
- return loginState.user || null;
417
- } else {
418
- return null;
414
+ await loginState.user.checkLocalInfoAsync()
415
+ return loginState.user || null
419
416
  }
417
+ return null
420
418
  }
421
419
 
422
420
 
@@ -431,11 +429,11 @@ class Auth {
431
429
  '请确认以下各项:',
432
430
  ' 1 - 当前环境是否开启了匿名登录',
433
431
  ' 2 - 调用 auth().signInAnonymously() 的语法或参数是否正确',
434
- `如果问题依然存在,建议到官方问答社区提问或寻找帮助:${COMMUNITY_SITE_URL}`
435
- ]
432
+ `如果问题依然存在,建议到官方问答社区提问或寻找帮助:${COMMUNITY_SITE_URL}`,
433
+ ],
436
434
  })
437
435
  public async signInAnonymously(): Promise<LoginState> {
438
- await this._oauthInstance.authApi.signInAnonymously()
436
+ await this.oauthInstance.authApi.signInAnonymously()
439
437
  return this.createLoginState()
440
438
  }
441
439
 
@@ -445,7 +443,7 @@ class Auth {
445
443
  * @memberof Auth
446
444
  */
447
445
  public setCustomSignFunc(getTickFn: authModels.GetCustomSignTicketFn): void {
448
- this._oauthInstance.authApi.setCustomSignFunc(getTickFn)
446
+ this.oauthInstance.authApi.setCustomSignFunc(getTickFn)
449
447
  }
450
448
 
451
449
  /**
@@ -461,11 +459,11 @@ class Auth {
461
459
  ' 2 - 调用 auth().signInWithCustomTicket() 的语法或参数是否正确',
462
460
  ' 3 - ticket 是否归属于当前环境',
463
461
  ' 4 - 创建 ticket 的自定义登录私钥是否过期',
464
- `如果问题依然存在,建议到官方问答社区提问或寻找帮助:${COMMUNITY_SITE_URL}`
465
- ]
462
+ `如果问题依然存在,建议到官方问答社区提问或寻找帮助:${COMMUNITY_SITE_URL}`,
463
+ ],
466
464
  })
467
465
  public async signInWithCustomTicket(): Promise<LoginState> {
468
- await this._oauthInstance.authApi.signInWithCustomTicket()
466
+ await this.oauthInstance.authApi.signInWithCustomTicket()
469
467
  return this.createLoginState()
470
468
  }
471
469
 
@@ -476,7 +474,7 @@ class Auth {
476
474
  * @memberof Auth
477
475
  */
478
476
  public async signIn(params: authModels.SignInRequest): Promise<LoginState> {
479
- await this._oauthInstance.authApi.signIn(params)
477
+ await this.oauthInstance.authApi.signIn(params)
480
478
  return this.createLoginState()
481
479
  }
482
480
 
@@ -492,11 +490,11 @@ class Auth {
492
490
  '请确认以下各项:',
493
491
  ' 1 - 当前环境是否开启了指定登录方式',
494
492
  ' 2 - 调用 auth().signUp() 的语法或参数是否正确',
495
- `如果问题依然存在,建议到官方问答社区提问或寻找帮助:${COMMUNITY_SITE_URL}`
496
- ]
493
+ `如果问题依然存在,建议到官方问答社区提问或寻找帮助:${COMMUNITY_SITE_URL}`,
494
+ ],
497
495
  })
498
496
  public async signUp(params: authModels.SignUpRequest): Promise<LoginState> {
499
- await this._oauthInstance.authApi.signUp(params)
497
+ await this.oauthInstance.authApi.signUp(params)
500
498
  return this.createLoginState()
501
499
  }
502
500
 
@@ -507,7 +505,7 @@ class Auth {
507
505
  * @memberof Auth
508
506
  */
509
507
  public async setPassword(params: authModels.SetPasswordRequest): Promise<void> {
510
- return this._oauthInstance.authApi.setPassword(params)
508
+ return this.oauthInstance.authApi.setPassword(params)
511
509
  }
512
510
 
513
511
  /**
@@ -519,15 +517,15 @@ class Auth {
519
517
  messages: [
520
518
  '请确认以下各项:',
521
519
  ' 1 - 调用 auth().isUsernameRegistered() 的语法或参数是否正确',
522
- `如果问题依然存在,建议到官方问答社区提问或寻找帮助:${COMMUNITY_SITE_URL}`
523
- ]
520
+ `如果问题依然存在,建议到官方问答社区提问或寻找帮助:${COMMUNITY_SITE_URL}`,
521
+ ],
524
522
  })
525
523
  public async isUsernameRegistered(username: string): Promise<boolean> {
526
524
  if (typeof username !== 'string') {
527
- throwError(ERRORS.INVALID_PARAMS, 'username must be a string');
525
+ throwError(ERRORS.INVALID_PARAMS, 'username must be a string')
528
526
  }
529
527
 
530
- const { exist } = await this._oauthInstance.authApi.checkIfUserExist({ username })
528
+ const { exist } = await this.oauthInstance.authApi.checkIfUserExist({ username })
531
529
  return exist
532
530
  }
533
531
 
@@ -540,13 +538,13 @@ class Auth {
540
538
  '请确认以下各项:',
541
539
  ' 1 - 调用 auth().signOut() 的语法或参数是否正确',
542
540
  ' 2 - 当前用户是否为匿名登录(匿名登录不支持signOut)',
543
- `如果问题依然存在,建议到官方问答社区提问或寻找帮助:${COMMUNITY_SITE_URL}`
544
- ]
541
+ `如果问题依然存在,建议到官方问答社区提问或寻找帮助:${COMMUNITY_SITE_URL}`,
542
+ ],
545
543
  })
546
544
  public async signOut() {
547
- const { userInfoKey } = this._cache.keys;
548
- await this._oauthInstance.authApi.signOut()
549
- await this._cache.removeStoreAsync(userInfoKey)
545
+ const { userInfoKey } = this.cache.keys
546
+ await this.oauthInstance.authApi.signOut()
547
+ await this.cache.removeStoreAsync(userInfoKey)
550
548
  eventBus.fire(EVENTS.LOGIN_STATE_CHANGED)
551
549
  }
552
550
 
@@ -554,23 +552,25 @@ class Auth {
554
552
  * 获取本地登录态-同步
555
553
  */
556
554
  public hasLoginState(): LoginState | null {
557
- if (this._cache.mode === 'async') {
555
+ if (this.cache.mode === 'async') {
558
556
  // async storage的平台调用此API提示
559
- printWarn(ERRORS.INVALID_OPERATION, 'current platform\'s storage is asynchronous, please use getLoginState insteed');
560
- return;
557
+ printWarn(
558
+ ERRORS.INVALID_OPERATION,
559
+ 'current platform\'s storage is asynchronous, please use getLoginState insteed'
560
+ )
561
+ return
561
562
  }
562
563
 
563
- const oauthLoginState = this._oauthInstance?.authApi.hasLoginStateSync()
564
+ const oauthLoginState = this.oauthInstance?.authApi.hasLoginStateSync()
564
565
  if (oauthLoginState) {
565
566
  const loginState = new LoginState({
566
- envId: this._config.env,
567
- cache: this._cache,
568
- oauthInstance: this._oauthInstance,
567
+ envId: this.config.env,
568
+ cache: this.cache,
569
+ oauthInstance: this.oauthInstance,
569
570
  })
570
571
  return loginState
571
- } else {
572
- return null
573
572
  }
573
+ return null
574
574
  }
575
575
 
576
576
  /**
@@ -582,16 +582,16 @@ class Auth {
582
582
  messages: [
583
583
  '请确认以下各项:',
584
584
  ' 1 - 调用 auth().getLoginState() 的语法或参数是否正确',
585
- `如果问题依然存在,建议到官方问答社区提问或寻找帮助:${COMMUNITY_SITE_URL}`
586
- ]
585
+ `如果问题依然存在,建议到官方问答社区提问或寻找帮助:${COMMUNITY_SITE_URL}`,
586
+ ],
587
587
  })
588
588
  public async getLoginState() {
589
- const oauthLoginState = await this._oauthInstance.authApi.getLoginState()
589
+ const oauthLoginState = await this.oauthInstance.authApi.getLoginState()
590
590
  if (oauthLoginState) {
591
591
  const loginState = new LoginState({
592
- envId: this._config.env,
593
- cache: this._cache,
594
- oauthInstance: this._oauthInstance,
592
+ envId: this.config.env,
593
+ cache: this.cache,
594
+ oauthInstance: this.oauthInstance,
595
595
  })
596
596
  return loginState
597
597
  }
@@ -605,13 +605,13 @@ class Auth {
605
605
  '请确认以下各项:',
606
606
  ' 1 - 是否已登录',
607
607
  ' 2 - 调用 auth().getUserInfo() 的语法或参数是否正确',
608
- `如果问题依然存在,建议到官方问答社区提问或寻找帮助:${COMMUNITY_SITE_URL}`
609
- ]
608
+ `如果问题依然存在,建议到官方问答社区提问或寻找帮助:${COMMUNITY_SITE_URL}`,
609
+ ],
610
610
  })
611
611
  public async getUserInfo(): Promise<IUserInfo> {
612
- return this._oauthInstance.authApi.getUserInfo()
612
+ return this.oauthInstance.authApi.getUserInfo()
613
613
  }
614
-
614
+
615
615
  /**
616
616
  * getAuthHeader 兼容处理
617
617
  * 返回空对象
@@ -620,7 +620,7 @@ class Auth {
620
620
  console.error('Auth.getAuthHeader API 已废弃')
621
621
  return {}
622
622
  }
623
-
623
+
624
624
  /**
625
625
  * 为已有账户绑第三方账户
626
626
  * @param {authModels.BindWithProviderRequest} params
@@ -633,13 +633,11 @@ class Auth {
633
633
  '请确认以下各项:',
634
634
  ' 1 - 调用 auth().bindWithProvider() 的语法或参数是否正确',
635
635
  ' 2 - 此账户是否已经绑定此第三方',
636
- `如果问题依然存在,建议到官方问答社区提问或寻找帮助:${COMMUNITY_SITE_URL}`
637
- ]
636
+ `如果问题依然存在,建议到官方问答社区提问或寻找帮助:${COMMUNITY_SITE_URL}`,
637
+ ],
638
638
  })
639
- public async bindWithProvider(
640
- params: authModels.BindWithProviderRequest,
641
- ): Promise<void> {
642
- return this._oauthInstance.authApi.bindWithProvider(params)
639
+ public async bindWithProvider(params: authModels.BindWithProviderRequest,): Promise<void> {
640
+ return this.oauthInstance.authApi.bindWithProvider(params)
643
641
  }
644
642
 
645
643
  /**
@@ -648,92 +646,84 @@ class Auth {
648
646
  * @returns {Promise<authModels.UserProfile>}
649
647
  * @memberof Auth
650
648
  */
651
- public async queryUser(
652
- queryObj: authModels.QueryUserProfileRequest,
653
- ): Promise<authModels.QueryUserProfileResponse> {
654
- return this._oauthInstance.authApi.queryUserProfile(queryObj)
649
+ public async queryUser(queryObj: authModels.QueryUserProfileRequest): Promise<authModels.QueryUserProfileResponse> {
650
+ return this.oauthInstance.authApi.queryUserProfile(queryObj)
655
651
  }
656
652
 
657
653
  public async getAccessToken() {
658
- const oauthAccessTokenRes = await this._oauthInstance.oauth2client.getAccessToken()
654
+ const oauthAccessTokenRes = await this.oauthInstance.oauth2client.getAccessToken()
659
655
  return {
660
656
  accessToken: oauthAccessTokenRes,
661
- env: this._config.env
662
- };
657
+ env: this.config.env,
658
+ }
663
659
  }
664
660
 
665
- public async grantProviderToken(
666
- params: authModels.GrantProviderTokenRequest,
667
- ): Promise<authModels.GrantProviderTokenResponse> {
668
- return this._oauthInstance.authApi.grantProviderToken(params)
661
+ public async grantProviderToken(params: authModels.GrantProviderTokenRequest): Promise<authModels.GrantProviderTokenResponse> {
662
+ return this.oauthInstance.authApi.grantProviderToken(params)
669
663
  }
670
664
 
671
- public async signInWithProvider(
672
- params: authModels.SignInWithProviderRequest,
673
- ): Promise<LoginState> {
674
- await this._oauthInstance.authApi.signInWithProvider(params)
665
+ public async signInWithProvider(params: authModels.SignInWithProviderRequest): Promise<LoginState> {
666
+ await this.oauthInstance.authApi.signInWithProvider(params)
675
667
  return this.createLoginState()
676
668
  }
677
669
 
678
670
  public async grantToken(params: authModels.GrantTokenRequest): Promise<LoginState> {
679
- await this._oauthInstance.authApi.grantToken(params)
671
+ await this.oauthInstance.authApi.grantToken(params)
680
672
  return this.createLoginState()
681
673
  }
682
674
 
683
- public async genProviderRedirectUri(
684
- params: authModels.GenProviderRedirectUriRequest,
685
- ): Promise<authModels.GenProviderRedirectUriResponse> {
686
- return this._oauthInstance.authApi.genProviderRedirectUri(params)
675
+ public async genProviderRedirectUri(params: authModels.GenProviderRedirectUriRequest): Promise<authModels.GenProviderRedirectUriResponse> {
676
+ return this.oauthInstance.authApi.genProviderRedirectUri(params)
687
677
  }
688
678
 
689
679
  public async resetPassword(params: authModels.ResetPasswordRequest): Promise<void> {
690
- return this._oauthInstance.authApi.resetPassword(params)
680
+ return this.oauthInstance.authApi.resetPassword(params)
691
681
  }
692
682
 
693
683
  public async deviceAuthorize(params: authModels.DeviceAuthorizeRequest): Promise<authModels.DeviceAuthorizeResponse> {
694
- return this._oauthInstance.authApi.deviceAuthorize(params)
684
+ return this.oauthInstance.authApi.deviceAuthorize(params)
695
685
  }
696
686
 
697
687
  public async sudo(params: authModels.SudoRequest): Promise<authModels.SudoResponse> {
698
- return this._oauthInstance.authApi.sudo(params)
688
+ return this.oauthInstance.authApi.sudo(params)
699
689
  }
700
690
 
701
691
  public async deleteMe(params: authModels.WithSudoRequest): Promise<authModels.UserProfile> {
702
- return this._oauthInstance.authApi.deleteMe(params)
692
+ return this.oauthInstance.authApi.deleteMe(params)
703
693
  }
704
694
 
705
695
  public async getProviders(): Promise<authModels.UserProfileProvider> {
706
- return this._oauthInstance.authApi.getProviders()
696
+ return this.oauthInstance.authApi.getProviders()
707
697
  }
708
698
 
709
699
  public async loginScope(): Promise<string> {
710
- return this._oauthInstance.authApi.loginScope()
700
+ return this.oauthInstance.authApi.loginScope()
711
701
  }
712
702
 
713
703
  public async loginGroups(): Promise<string[]> {
714
- return this._oauthInstance.authApi.loginGroups()
704
+ return this.oauthInstance.authApi.loginGroups()
715
705
  }
716
706
 
717
707
  public async onLoginStateChanged(callback: Function) {
718
708
  eventBus.on(EVENTS.LOGIN_STATE_CHANGED, async () => {
719
- const loginState = await this.getLoginState();
720
- callback.call(this, loginState);
721
- });
709
+ const loginState = await this.getLoginState()
710
+ callback.call(this, loginState)
711
+ })
722
712
  // 立刻执行一次回调
723
- const loginState = await this.getLoginState();
724
- callback.call(this, loginState);
713
+ const loginState = await this.getLoginState()
714
+ callback.call(this, loginState)
725
715
  }
726
716
 
727
717
  private async createLoginState(): Promise<LoginState> {
728
718
  const loginState = new LoginState({
729
- envId: this._config.env,
730
- cache: this._cache,
731
- oauthInstance: this._oauthInstance,
719
+ envId: this.config.env,
720
+ cache: this.cache,
721
+ oauthInstance: this.oauthInstance,
732
722
  })
733
723
 
734
- await loginState.checkLocalStateAsync();
735
- await loginState.user.refresh();
736
- eventBus.fire(EVENTS.LOGIN_STATE_CHANGED);
724
+ await loginState.checkLocalStateAsync()
725
+ await loginState.user.refresh()
726
+ eventBus.fire(EVENTS.LOGIN_STATE_CHANGED)
737
727
  return loginState
738
728
  }
739
729
  }
@@ -741,21 +731,22 @@ class Auth {
741
731
  const component: ICloudbaseComponent = {
742
732
  name: COMPONENT_NAME,
743
733
  namespace: 'auth',
744
- entity: function (config: Pick<ICloudbaseAuthConfig, 'region' | 'persistence'> = { region: '', persistence: 'local' }) {
734
+ entity(config: Pick<ICloudbaseAuthConfig, 'region' | 'persistence'>
735
+ = { region: '', persistence: 'local' }) {
745
736
  if (this.authInstance) {
746
- printWarn(ERRORS.INVALID_OPERATION, 'every cloudbase instance should has only one auth object');
747
- return this.authInstance;
737
+ printWarn(ERRORS.INVALID_OPERATION, 'every cloudbase instance should has only one auth object')
738
+ return this.authInstance
748
739
  }
749
- const { adapter, runtime } = this.platform;
740
+ const { adapter, runtime } = this.platform
750
741
  // 如不明确指定persistence则优先取各平台adapter首选,其次localStorage
751
- const newPersistence = config.persistence || adapter.primaryStorage;
742
+ const newPersistence = config.persistence || adapter.primaryStorage
752
743
  if (newPersistence && (newPersistence !== this.config.persistence)) {
753
744
  this.updateConfig({ persistence: newPersistence })
754
745
  }
755
746
 
756
- const { env, persistence, debug, clientId } = this.config;
747
+ const { env, persistence, debug, clientId } = this.config
757
748
  const oauthInstance = new CloudbaseOAuth({
758
- clientId: clientId,
749
+ clientId,
759
750
  apiOrigin: this.request.getBaseEndPoint(),
760
751
  })
761
752
 
@@ -768,34 +759,34 @@ const component: ICloudbaseComponent = {
768
759
  debug,
769
760
  cache: this.cache,
770
761
  // request: this.request,
771
- runtime: runtime,
762
+ runtime,
772
763
  _fromApp: this,
773
764
  // oauthInstance: this.oauthInstance || (this as any).oauth()
774
- oauthInstance
775
- });
765
+ oauthInstance,
766
+ })
776
767
 
777
- return this.authInstance;
778
- }
768
+ return this.authInstance
769
+ },
779
770
  }
780
771
 
781
772
  try {
782
773
  // 尝试自动注册至全局变量cloudbase
783
774
  // 此行为只在浏览器环境下有效
784
- cloudbase.registerComponent(component);
775
+ cloudbase.registerComponent(component)
785
776
  } catch (e) { }
786
777
 
787
778
  export {
788
779
  UserInfo,
789
780
  Auth,
790
- };
781
+ }
791
782
  /**
792
783
  * @api 手动注册至cloudbase app
793
784
  */
794
785
  export function registerAuth(app: Pick<ICloudbase, 'registerComponent'>) {
795
786
  try {
796
- app.registerComponent(component);
787
+ app.registerComponent(component)
797
788
  } catch (e) {
798
- console.warn(e);
789
+ console.warn(e)
799
790
  }
800
791
  }
801
792