@cloudbase/auth 1.7.2-alpha.0 → 1.8.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/.eslintrc.js +13 -0
- package/dist/cjs/constants.d.ts +0 -1
- package/dist/cjs/constants.js +2 -3
- package/dist/cjs/index.d.ts +8 -12
- package/dist/cjs/index.js +108 -147
- package/dist/cjs/providers/anonymousAuthProvider.js +24 -21
- package/dist/cjs/providers/base.js +12 -5
- package/dist/cjs/providers/customAuthProvider.js +14 -12
- package/dist/cjs/providers/emailAuthProvider.js +26 -26
- package/dist/cjs/providers/phoneAuthProvider.js +21 -21
- package/dist/cjs/providers/usernameAuthProvider.js +16 -15
- package/dist/cjs/providers/weixinAuthProvider.js +31 -30
- package/dist/esm/constants.d.ts +0 -1
- package/dist/esm/constants.js +1 -2
- package/dist/esm/index.d.ts +8 -12
- package/dist/esm/index.js +109 -148
- package/dist/esm/providers/anonymousAuthProvider.js +24 -21
- package/dist/esm/providers/base.js +12 -5
- package/dist/esm/providers/customAuthProvider.js +14 -12
- package/dist/esm/providers/emailAuthProvider.js +26 -26
- package/dist/esm/providers/phoneAuthProvider.js +21 -21
- package/dist/esm/providers/usernameAuthProvider.js +16 -15
- package/dist/esm/providers/weixinAuthProvider.js +31 -30
- package/package.json +8 -12
- package/src/constants.ts +0 -2
- package/src/index.ts +129 -203
- package/src/providers/anonymousAuthProvider.ts +20 -22
- package/src/providers/base.ts +10 -11
- package/src/providers/customAuthProvider.ts +14 -16
- package/src/providers/emailAuthProvider.ts +32 -32
- package/src/providers/phoneAuthProvider.ts +21 -20
- package/src/providers/usernameAuthProvider.ts +11 -11
- package/src/providers/weixinAuthProvider.ts +45 -46
- package/tsconfig.esm.json +2 -19
- package/tsconfig.json +2 -19
- package/.eslintrc +0 -30
- package/dist/cjs/providers/oauth2AuthProvider.d.ts +0 -61
- package/dist/cjs/providers/oauth2AuthProvider.js +0 -404
- package/dist/esm/providers/oauth2AuthProvider.d.ts +0 -61
- package/dist/esm/providers/oauth2AuthProvider.js +0 -401
- package/src/providers/oauth2AuthProvider.ts +0 -585
package/src/index.ts
CHANGED
|
@@ -1,22 +1,18 @@
|
|
|
1
|
+
/* eslint-disable new-cap */
|
|
1
2
|
import { ICloudbase } from '@cloudbase/types';
|
|
2
3
|
import { events, adapters, utils, constants, helpers } from '@cloudbase/utilities';
|
|
3
4
|
import { ICloudbaseCache } from '@cloudbase/types/cache';
|
|
4
5
|
import { ICloudbaseRequest } from '@cloudbase/types/request';
|
|
5
6
|
import { ICloudbaseAuthConfig, ICredential, IUser, IUserInfo, IAuthProvider, ILoginState } from '@cloudbase/types/auth';
|
|
6
7
|
import { ICloudbaseComponent } from '@cloudbase/types/component';
|
|
7
|
-
|
|
8
|
-
import { LOGINTYPE, OAUTH2_LOGINTYPE_PREFIX } from './constants';
|
|
9
|
-
|
|
10
|
-
import { AuthProvider } from './providers/base';
|
|
11
|
-
|
|
12
|
-
import { OAuth2AuthProvider, IOAuth2AuthProviderOptions } from './providers/oauth2AuthProvider';
|
|
13
|
-
|
|
8
|
+
import { WeixinAuthProvider } from './providers/weixinAuthProvider';
|
|
14
9
|
import { AnonymousAuthProvider } from './providers/anonymousAuthProvider';
|
|
15
10
|
import { CustomAuthProvider } from './providers/customAuthProvider';
|
|
11
|
+
import { LOGINTYPE } from './constants';
|
|
12
|
+
import { AuthProvider } from './providers/base';
|
|
16
13
|
import { EmailAuthProvider } from './providers/emailAuthProvider';
|
|
17
|
-
import { PhoneAuthProvider, SIGN_METHOD } from './providers/phoneAuthProvider'
|
|
18
14
|
import { UsernameAuthProvider } from './providers/usernameAuthProvider';
|
|
19
|
-
import {
|
|
15
|
+
import { PhoneAuthProvider, SIGN_METHOD } from './providers/phoneAuthProvider';
|
|
20
16
|
|
|
21
17
|
declare const cloudbase: ICloudbase;
|
|
22
18
|
|
|
@@ -96,12 +92,12 @@ class User implements IUser {
|
|
|
96
92
|
this.avatarUrl = this._getLocalUserInfo('avatarUrl');
|
|
97
93
|
this.email = this._getLocalUserInfo('email');
|
|
98
94
|
this.hasPassword = Boolean(this._getLocalUserInfo('hasPassword'));
|
|
99
|
-
this.phone = this._getLocalUserInfo('phone')
|
|
100
|
-
this.username = this._getLocalUserInfo('username')
|
|
95
|
+
this.phone = this._getLocalUserInfo('phone');
|
|
96
|
+
this.username = this._getLocalUserInfo('username');
|
|
101
97
|
this.location = {
|
|
102
98
|
country: this._getLocalUserInfo('country'),
|
|
103
99
|
province: this._getLocalUserInfo('province'),
|
|
104
|
-
city: this._getLocalUserInfo('city')
|
|
100
|
+
city: this._getLocalUserInfo('city'),
|
|
105
101
|
};
|
|
106
102
|
}
|
|
107
103
|
/**
|
|
@@ -121,12 +117,12 @@ class User implements IUser {
|
|
|
121
117
|
this.avatarUrl = await this._getLocalUserInfoAsync('avatarUrl');
|
|
122
118
|
this.email = await this._getLocalUserInfoAsync('email');
|
|
123
119
|
this.hasPassword = Boolean(await this._getLocalUserInfoAsync('hasPassword'));
|
|
124
|
-
this.phone = await this._getLocalUserInfoAsync('phone')
|
|
125
|
-
this.username = await this._getLocalUserInfoAsync('username')
|
|
120
|
+
this.phone = await this._getLocalUserInfoAsync('phone');
|
|
121
|
+
this.username = await this._getLocalUserInfoAsync('username');
|
|
126
122
|
this.location = {
|
|
127
123
|
country: await this._getLocalUserInfoAsync('country'),
|
|
128
124
|
province: await this._getLocalUserInfoAsync('province'),
|
|
129
|
-
city: await this._getLocalUserInfoAsync('city')
|
|
125
|
+
city: await this._getLocalUserInfoAsync('city'),
|
|
130
126
|
};
|
|
131
127
|
}
|
|
132
128
|
|
|
@@ -142,8 +138,8 @@ class User implements IUser {
|
|
|
142
138
|
' 2 - 此账户是否已经绑定自定义登录',
|
|
143
139
|
' 3 - ticket 参数是否归属当前环境',
|
|
144
140
|
' 4 - 创建 ticket 的自定义登录私钥是否过期',
|
|
145
|
-
`如果问题依然存在,建议到官方问答社区提问或寻找帮助:${COMMUNITY_SITE_URL}
|
|
146
|
-
]
|
|
141
|
+
`如果问题依然存在,建议到官方问答社区提问或寻找帮助:${COMMUNITY_SITE_URL}`,
|
|
142
|
+
],
|
|
147
143
|
})
|
|
148
144
|
public linkWithTicket(ticket: string): Promise<void> {
|
|
149
145
|
if (typeof ticket !== 'string') {
|
|
@@ -162,8 +158,8 @@ class User implements IUser {
|
|
|
162
158
|
' 1 - 调用 User.linkWithRedirect() 的语法或参数是否正确',
|
|
163
159
|
' 2 - 此账户是否已经绑定此第三方',
|
|
164
160
|
' 3 - 此第三方是否已经授权',
|
|
165
|
-
`如果问题依然存在,建议到官方问答社区提问或寻找帮助:${COMMUNITY_SITE_URL}
|
|
166
|
-
]
|
|
161
|
+
`如果问题依然存在,建议到官方问答社区提问或寻找帮助:${COMMUNITY_SITE_URL}`,
|
|
162
|
+
],
|
|
167
163
|
})
|
|
168
164
|
public linkWithRedirect(provider: IAuthProvider): void {
|
|
169
165
|
provider.signInWithRedirect();
|
|
@@ -176,8 +172,8 @@ class User implements IUser {
|
|
|
176
172
|
messages: [
|
|
177
173
|
'请确认以下各项:',
|
|
178
174
|
' 1 - 调用 User.getLinkedUidList() 的语法或参数是否正确',
|
|
179
|
-
`如果问题依然存在,建议到官方问答社区提问或寻找帮助:${COMMUNITY_SITE_URL}
|
|
180
|
-
]
|
|
175
|
+
`如果问题依然存在,建议到官方问答社区提问或寻找帮助:${COMMUNITY_SITE_URL}`,
|
|
176
|
+
],
|
|
181
177
|
})
|
|
182
178
|
public async getLinkedUidList() {
|
|
183
179
|
const { data } = await this._request.send('auth.getLinkedUidList', {});
|
|
@@ -191,7 +187,7 @@ class User implements IUser {
|
|
|
191
187
|
}
|
|
192
188
|
return {
|
|
193
189
|
users,
|
|
194
|
-
hasPrimaryUid
|
|
190
|
+
hasPrimaryUid,
|
|
195
191
|
};
|
|
196
192
|
}
|
|
197
193
|
/**
|
|
@@ -204,8 +200,8 @@ class User implements IUser {
|
|
|
204
200
|
messages: [
|
|
205
201
|
'请确认以下各项:',
|
|
206
202
|
' 1 - 调用 User.setPrimaryUid() 的语法或参数是否正确',
|
|
207
|
-
`如果问题依然存在,建议到官方问答社区提问或寻找帮助:${COMMUNITY_SITE_URL}
|
|
208
|
-
]
|
|
203
|
+
`如果问题依然存在,建议到官方问答社区提问或寻找帮助:${COMMUNITY_SITE_URL}`,
|
|
204
|
+
],
|
|
209
205
|
})
|
|
210
206
|
public setPrimaryUid(uid: string) {
|
|
211
207
|
return this._request.send('auth.setPrimaryUid', { uid });
|
|
@@ -220,8 +216,8 @@ class User implements IUser {
|
|
|
220
216
|
'请确认以下各项:',
|
|
221
217
|
' 1 - 调用 User.unlink() 的语法或参数是否正确',
|
|
222
218
|
' 2 - 当前账户是否已经与此登录方式解绑',
|
|
223
|
-
`如果问题依然存在,建议到官方问答社区提问或寻找帮助:${COMMUNITY_SITE_URL}
|
|
224
|
-
]
|
|
219
|
+
`如果问题依然存在,建议到官方问答社区提问或寻找帮助:${COMMUNITY_SITE_URL}`,
|
|
220
|
+
],
|
|
225
221
|
})
|
|
226
222
|
public unlink(loginType: 'CUSTOM' | 'WECHAT-OPEN' | 'WECHAT-PUBLIC' | 'WECHAT-UNION' | 'PHONE') {
|
|
227
223
|
return this._request.send('auth.unlink', { platform: loginType });
|
|
@@ -236,8 +232,8 @@ class User implements IUser {
|
|
|
236
232
|
'请确认以下各项:',
|
|
237
233
|
' 1 - 调用 User.update() 的语法或参数是否正确',
|
|
238
234
|
' 2 - 用户信息中是否包含非法值',
|
|
239
|
-
`如果问题依然存在,建议到官方问答社区提问或寻找帮助:${COMMUNITY_SITE_URL}
|
|
240
|
-
]
|
|
235
|
+
`如果问题依然存在,建议到官方问答社区提问或寻找帮助:${COMMUNITY_SITE_URL}`,
|
|
236
|
+
],
|
|
241
237
|
})
|
|
242
238
|
public async update(userInfo: IUserInfo): Promise<void> {
|
|
243
239
|
const { nickName, gender, avatarUrl, province, country, city } = userInfo;
|
|
@@ -255,13 +251,13 @@ class User implements IUser {
|
|
|
255
251
|
'请确认以下各项:',
|
|
256
252
|
' 1 - 调用 User.updatePassword() 的语法或参数是否正确',
|
|
257
253
|
' 3 - 新密码中是否包含非法字符',
|
|
258
|
-
`如果问题依然存在,建议到官方问答社区提问或寻找帮助:${COMMUNITY_SITE_URL}
|
|
259
|
-
]
|
|
254
|
+
`如果问题依然存在,建议到官方问答社区提问或寻找帮助:${COMMUNITY_SITE_URL}`,
|
|
255
|
+
],
|
|
260
256
|
})
|
|
261
257
|
public updatePassword(newPassword: string, oldPassword: string) {
|
|
262
258
|
return this._request.send('auth.updatePassword', {
|
|
263
259
|
oldPassword,
|
|
264
|
-
newPassword
|
|
260
|
+
newPassword,
|
|
265
261
|
});
|
|
266
262
|
}
|
|
267
263
|
/**
|
|
@@ -274,13 +270,13 @@ class User implements IUser {
|
|
|
274
270
|
'请确认以下各项:',
|
|
275
271
|
' 1 - 调用 User.updateEmail() 的语法或参数是否正确',
|
|
276
272
|
' 2 - 当前环境是否开通了邮箱密码登录',
|
|
277
|
-
`如果问题依然存在,建议到官方问答社区提问或寻找帮助:${COMMUNITY_SITE_URL}
|
|
278
|
-
]
|
|
273
|
+
`如果问题依然存在,建议到官方问答社区提问或寻找帮助:${COMMUNITY_SITE_URL}`,
|
|
274
|
+
],
|
|
279
275
|
})
|
|
280
276
|
public updateEmail(newEmail: string, password?: string) {
|
|
281
277
|
return this._request.send('auth.updateEmail', {
|
|
282
278
|
newEmail,
|
|
283
|
-
password
|
|
279
|
+
password,
|
|
284
280
|
});
|
|
285
281
|
}
|
|
286
282
|
/**
|
|
@@ -293,8 +289,8 @@ class User implements IUser {
|
|
|
293
289
|
'请确认以下各项:',
|
|
294
290
|
' 1 - 调用 User.updateUsername() 的语法或参数是否正确',
|
|
295
291
|
' 2 - 当前环境是否开通了用户名密码登录',
|
|
296
|
-
`如果问题依然存在,建议到官方问答社区提问或寻找帮助:${COMMUNITY_SITE_URL}
|
|
297
|
-
]
|
|
292
|
+
`如果问题依然存在,建议到官方问答社区提问或寻找帮助:${COMMUNITY_SITE_URL}`,
|
|
293
|
+
],
|
|
298
294
|
})
|
|
299
295
|
public updateUsername(username: string) {
|
|
300
296
|
if (typeof username !== 'string') {
|
|
@@ -302,7 +298,7 @@ class User implements IUser {
|
|
|
302
298
|
}
|
|
303
299
|
|
|
304
300
|
return this._request.send('auth.updateUsername', {
|
|
305
|
-
username
|
|
301
|
+
username,
|
|
306
302
|
});
|
|
307
303
|
}
|
|
308
304
|
/**
|
|
@@ -313,8 +309,8 @@ class User implements IUser {
|
|
|
313
309
|
messages: [
|
|
314
310
|
'请确认以下各项:',
|
|
315
311
|
' 1 - 调用 User.refresh() 的语法或参数是否正确',
|
|
316
|
-
`如果问题依然存在,建议到官方问答社区提问或寻找帮助:${COMMUNITY_SITE_URL}
|
|
317
|
-
]
|
|
312
|
+
`如果问题依然存在,建议到官方问答社区提问或寻找帮助:${COMMUNITY_SITE_URL}`,
|
|
313
|
+
],
|
|
318
314
|
})
|
|
319
315
|
public async refresh(): Promise<IUserInfo> {
|
|
320
316
|
const action = 'auth.getUserInfo';
|
|
@@ -334,13 +330,13 @@ class User implements IUser {
|
|
|
334
330
|
'请确认以下各项:',
|
|
335
331
|
' 1 - 调用 auth().linkWithPhoneNumber() 的语法或参数是否正确',
|
|
336
332
|
' 2 - 当前环境是否开通了短信验证码登录',
|
|
337
|
-
`如果问题依然存在,建议到官方问答社区提问或寻找帮助:${COMMUNITY_SITE_URL}
|
|
338
|
-
]
|
|
333
|
+
`如果问题依然存在,建议到官方问答社区提问或寻找帮助:${COMMUNITY_SITE_URL}`,
|
|
334
|
+
],
|
|
339
335
|
})
|
|
340
336
|
public async linkWithPhoneNumber(phoneNumber: string, phoneCode: string) {
|
|
341
337
|
return this._request.send('auth.linkOrUpdatePhoneNumber', {
|
|
342
338
|
phoneNumber: transformPhone(phoneNumber),
|
|
343
|
-
phoneCode
|
|
339
|
+
phoneCode,
|
|
344
340
|
});
|
|
345
341
|
}
|
|
346
342
|
/**
|
|
@@ -354,13 +350,13 @@ class User implements IUser {
|
|
|
354
350
|
'请确认以下各项:',
|
|
355
351
|
' 1 - 调用语法或参数是否正确',
|
|
356
352
|
' 2 - 当前环境是否开通了短信验证码登录',
|
|
357
|
-
`如果问题依然存在,建议到官方问答社区提问或寻找帮助:${COMMUNITY_SITE_URL}
|
|
358
|
-
]
|
|
353
|
+
`如果问题依然存在,建议到官方问答社区提问或寻找帮助:${COMMUNITY_SITE_URL}`,
|
|
354
|
+
],
|
|
359
355
|
})
|
|
360
356
|
public async updatePhoneNumber(phoneNumber: string, phoneCode: string) {
|
|
361
357
|
return this._request.send('auth.linkOrUpdatePhoneNumber', {
|
|
362
358
|
phoneNumber: transformPhone(phoneNumber),
|
|
363
|
-
phoneCode
|
|
359
|
+
phoneCode,
|
|
364
360
|
});
|
|
365
361
|
}
|
|
366
362
|
|
|
@@ -394,15 +390,15 @@ class User implements IUser {
|
|
|
394
390
|
'gender',
|
|
395
391
|
'avatarUrl',
|
|
396
392
|
'phone',
|
|
397
|
-
'username'
|
|
398
|
-
].forEach(infoKey => {
|
|
393
|
+
'username',
|
|
394
|
+
].forEach((infoKey) => {
|
|
399
395
|
this[infoKey] = userInfo[infoKey];
|
|
400
396
|
});
|
|
401
397
|
|
|
402
398
|
this.location = {
|
|
403
|
-
country: userInfo
|
|
404
|
-
province: userInfo
|
|
405
|
-
city: userInfo
|
|
399
|
+
country: userInfo.country,
|
|
400
|
+
province: userInfo.province,
|
|
401
|
+
city: userInfo.city,
|
|
406
402
|
};
|
|
407
403
|
}
|
|
408
404
|
|
|
@@ -431,7 +427,7 @@ export class LoginState implements ILoginState {
|
|
|
431
427
|
|
|
432
428
|
this.user = new User({
|
|
433
429
|
cache,
|
|
434
|
-
request
|
|
430
|
+
request,
|
|
435
431
|
});
|
|
436
432
|
}
|
|
437
433
|
|
|
@@ -445,7 +441,7 @@ export class LoginState implements ILoginState {
|
|
|
445
441
|
this.credential = {
|
|
446
442
|
refreshToken,
|
|
447
443
|
accessToken,
|
|
448
|
-
accessTokenExpire
|
|
444
|
+
accessTokenExpire,
|
|
449
445
|
};
|
|
450
446
|
|
|
451
447
|
this._loginType = this._cache.getStore(this._cache.keys.loginTypeKey);
|
|
@@ -461,7 +457,7 @@ export class LoginState implements ILoginState {
|
|
|
461
457
|
this.credential = {
|
|
462
458
|
refreshToken,
|
|
463
459
|
accessToken,
|
|
464
|
-
accessTokenExpire
|
|
460
|
+
accessTokenExpire,
|
|
465
461
|
};
|
|
466
462
|
|
|
467
463
|
this._loginType = await this._cache.getStoreAsync(this._cache.keys.loginTypeKey);
|
|
@@ -487,17 +483,17 @@ export class LoginState implements ILoginState {
|
|
|
487
483
|
}
|
|
488
484
|
|
|
489
485
|
get loginType() {
|
|
490
|
-
return this._loginType
|
|
486
|
+
return this._loginType;
|
|
491
487
|
}
|
|
492
488
|
|
|
493
489
|
get isPhoneAuth() {
|
|
494
|
-
return this.loginType === LOGINTYPE.PHONE
|
|
490
|
+
return this.loginType === LOGINTYPE.PHONE;
|
|
495
491
|
}
|
|
496
492
|
}
|
|
497
493
|
|
|
498
494
|
class Auth {
|
|
499
495
|
private readonly _config: ICloudbaseAuthConfig;
|
|
500
|
-
private readonly _cache: ICloudbaseCache
|
|
496
|
+
private readonly _cache: ICloudbaseCache;
|
|
501
497
|
private readonly _request: ICloudbaseRequest;
|
|
502
498
|
private readonly _runtime: string;
|
|
503
499
|
private _anonymousAuthProvider: AnonymousAuthProvider;
|
|
@@ -507,13 +503,11 @@ class Auth {
|
|
|
507
503
|
private _usernameAuthProvider: UsernameAuthProvider;
|
|
508
504
|
private _phoneAuthProvider: PhoneAuthProvider;
|
|
509
505
|
|
|
510
|
-
private _oAuth2AuthProvider: OAuth2AuthProvider;
|
|
511
|
-
|
|
512
506
|
constructor(config: ICloudbaseAuthConfig & { cache: ICloudbaseCache, request: ICloudbaseRequest, runtime?: string }) {
|
|
513
507
|
this._config = config;
|
|
514
508
|
this._cache = config.cache;
|
|
515
509
|
this._request = config.request;
|
|
516
|
-
this._runtime = config.runtime || RUNTIME.WEB
|
|
510
|
+
this._runtime = config.runtime || RUNTIME.WEB;
|
|
517
511
|
|
|
518
512
|
eventBus.on(EVENTS.LOGIN_TYPE_CHANGED, this._onLoginTypeChanged.bind(this));
|
|
519
513
|
}
|
|
@@ -524,7 +518,7 @@ class Auth {
|
|
|
524
518
|
get currentUser() {
|
|
525
519
|
if (this._cache.mode === 'async') {
|
|
526
520
|
// async storage的平台调用此API提示
|
|
527
|
-
printWarn(ERRORS.INVALID_OPERATION, 'current platform\'s storage is asynchronous, please use
|
|
521
|
+
printWarn(ERRORS.INVALID_OPERATION, 'current platform\'s storage is asynchronous, please use getCurrentUser insteed');
|
|
528
522
|
return;
|
|
529
523
|
}
|
|
530
524
|
|
|
@@ -532,9 +526,8 @@ class Auth {
|
|
|
532
526
|
|
|
533
527
|
if (loginState) {
|
|
534
528
|
return loginState.user || null;
|
|
535
|
-
} else {
|
|
536
|
-
return null;
|
|
537
529
|
}
|
|
530
|
+
return null;
|
|
538
531
|
}
|
|
539
532
|
|
|
540
533
|
/**
|
|
@@ -545,6 +538,7 @@ class Auth {
|
|
|
545
538
|
}
|
|
546
539
|
|
|
547
540
|
/**
|
|
541
|
+
* @deprecated
|
|
548
542
|
* 获取当前登录的用户信息-异步
|
|
549
543
|
*/
|
|
550
544
|
@catchErrorsDecorator({
|
|
@@ -552,17 +546,30 @@ class Auth {
|
|
|
552
546
|
messages: [
|
|
553
547
|
'请确认以下各项:',
|
|
554
548
|
' 1 - 调用 auth().getCurrenUser() 的语法或参数是否正确',
|
|
555
|
-
`如果问题依然存在,建议到官方问答社区提问或寻找帮助:${COMMUNITY_SITE_URL}
|
|
556
|
-
]
|
|
549
|
+
`如果问题依然存在,建议到官方问答社区提问或寻找帮助:${COMMUNITY_SITE_URL}`,
|
|
550
|
+
],
|
|
557
551
|
})
|
|
558
552
|
public async getCurrenUser() {
|
|
553
|
+
return await this.getCurrentUser();
|
|
554
|
+
}
|
|
555
|
+
/**
|
|
556
|
+
* 获取当前登录的用户信息-异步
|
|
557
|
+
*/
|
|
558
|
+
@catchErrorsDecorator({
|
|
559
|
+
title: '获取用户信息失败',
|
|
560
|
+
messages: [
|
|
561
|
+
'请确认以下各项:',
|
|
562
|
+
' 1 - 调用 auth().getCurrentUser() 的语法或参数是否正确',
|
|
563
|
+
`如果问题依然存在,建议到官方问答社区提问或寻找帮助:${COMMUNITY_SITE_URL}`,
|
|
564
|
+
],
|
|
565
|
+
})
|
|
566
|
+
public async getCurrentUser() {
|
|
559
567
|
const loginState = await this.getLoginState();
|
|
560
568
|
if (loginState) {
|
|
561
569
|
await loginState.user.checkLocalInfoAsync();
|
|
562
570
|
return loginState.user || null;
|
|
563
|
-
} else {
|
|
564
|
-
return null;
|
|
565
571
|
}
|
|
572
|
+
return null;
|
|
566
573
|
}
|
|
567
574
|
/**
|
|
568
575
|
* 获取当前登录类型-异步
|
|
@@ -573,7 +580,7 @@ class Auth {
|
|
|
573
580
|
public async getAccessToken() {
|
|
574
581
|
return {
|
|
575
582
|
accessToken: (await this._request.getAccessToken()).accessToken,
|
|
576
|
-
env: this._config.env
|
|
583
|
+
env: this._config.env,
|
|
577
584
|
};
|
|
578
585
|
}
|
|
579
586
|
public weixinAuthProvider({ appid, scope, state }): WeixinAuthProvider {
|
|
@@ -582,7 +589,7 @@ class Auth {
|
|
|
582
589
|
...this._config,
|
|
583
590
|
cache: this._cache,
|
|
584
591
|
request: this._request,
|
|
585
|
-
runtime: this._runtime
|
|
592
|
+
runtime: this._runtime,
|
|
586
593
|
}, appid, scope, state);
|
|
587
594
|
}
|
|
588
595
|
return this._weixinAuthProvider;
|
|
@@ -592,7 +599,7 @@ class Auth {
|
|
|
592
599
|
this._anonymousAuthProvider = new AnonymousAuthProvider({
|
|
593
600
|
...this._config,
|
|
594
601
|
cache: this._cache,
|
|
595
|
-
request: this._request
|
|
602
|
+
request: this._request,
|
|
596
603
|
});
|
|
597
604
|
}
|
|
598
605
|
return this._anonymousAuthProvider;
|
|
@@ -602,7 +609,7 @@ class Auth {
|
|
|
602
609
|
this._customAuthProvider = new CustomAuthProvider({
|
|
603
610
|
...this._config,
|
|
604
611
|
cache: this._cache,
|
|
605
|
-
request: this._request
|
|
612
|
+
request: this._request,
|
|
606
613
|
});
|
|
607
614
|
}
|
|
608
615
|
return this._customAuthProvider;
|
|
@@ -612,7 +619,7 @@ class Auth {
|
|
|
612
619
|
this._emailAuthProvider = new EmailAuthProvider({
|
|
613
620
|
...this._config,
|
|
614
621
|
cache: this._cache,
|
|
615
|
-
request: this._request
|
|
622
|
+
request: this._request,
|
|
616
623
|
});
|
|
617
624
|
}
|
|
618
625
|
return this._emailAuthProvider;
|
|
@@ -622,7 +629,7 @@ class Auth {
|
|
|
622
629
|
this._usernameAuthProvider = new UsernameAuthProvider({
|
|
623
630
|
...this._config,
|
|
624
631
|
cache: this._cache,
|
|
625
|
-
request: this._request
|
|
632
|
+
request: this._request,
|
|
626
633
|
});
|
|
627
634
|
}
|
|
628
635
|
return this._usernameAuthProvider;
|
|
@@ -633,53 +640,11 @@ class Auth {
|
|
|
633
640
|
this._phoneAuthProvider = new PhoneAuthProvider({
|
|
634
641
|
...this._config,
|
|
635
642
|
cache: this._cache,
|
|
636
|
-
request: this._request
|
|
643
|
+
request: this._request,
|
|
637
644
|
});
|
|
638
645
|
}
|
|
639
646
|
return this._phoneAuthProvider;
|
|
640
647
|
}
|
|
641
|
-
|
|
642
|
-
/**
|
|
643
|
-
* oAuth2AuthProvider
|
|
644
|
-
* options
|
|
645
|
-
* {
|
|
646
|
-
* providerId: 'google',
|
|
647
|
-
* scope: 'openid+email+profile',
|
|
648
|
-
* redirectUri: 'https://'
|
|
649
|
-
* }
|
|
650
|
-
* @param {Object} options
|
|
651
|
-
* @param {string} options.providerId - 供应商Id,如 WeChat、Google、Github 等
|
|
652
|
-
* @param {string} options.clientId - 客户端Id,平台提供的客户端标识Id
|
|
653
|
-
* @param {string} [options.responseType=token] - 响应类型:token、code
|
|
654
|
-
* @param {string} options.scope - 权限范围
|
|
655
|
-
* @param {string} options.redirectUri - 授权成功回调地址
|
|
656
|
-
* @param {boolean} options.syncProfile - 是否同步用户 Profile 信息
|
|
657
|
-
* @param {boolean} options.forceDisableSignUp - 是否强制关闭用户注册
|
|
658
|
-
* @returns
|
|
659
|
-
*/
|
|
660
|
-
public oAuth2AuthProvider(options: IOAuth2AuthProviderOptions = {}): OAuth2AuthProvider {
|
|
661
|
-
if (!this._oAuth2AuthProvider) {
|
|
662
|
-
this._oAuth2AuthProvider = new OAuth2AuthProvider({
|
|
663
|
-
...this._config,
|
|
664
|
-
cache: this._cache,
|
|
665
|
-
request: this._request,
|
|
666
|
-
runtime: this._runtime
|
|
667
|
-
}, options)
|
|
668
|
-
}
|
|
669
|
-
return this._oAuth2AuthProvider
|
|
670
|
-
}
|
|
671
|
-
|
|
672
|
-
/**
|
|
673
|
-
* signWithOAuth2Popup - OAuth2弹窗登录
|
|
674
|
-
*/
|
|
675
|
-
public signWithOAuth2Popup() {
|
|
676
|
-
this._oAuth2AuthProvider.signInWithPopup()
|
|
677
|
-
}
|
|
678
|
-
|
|
679
|
-
public signInWithOAuth2Modal(elemId: string) {
|
|
680
|
-
this._oAuth2AuthProvider.signInWithModal(elemId)
|
|
681
|
-
}
|
|
682
|
-
|
|
683
648
|
/**
|
|
684
649
|
* 用户名密码登录
|
|
685
650
|
* @param username
|
|
@@ -697,8 +662,8 @@ class Auth {
|
|
|
697
662
|
messages: [
|
|
698
663
|
'请确认以下各项:',
|
|
699
664
|
' 1 - 调用 auth().isUsernameRegistered() 的语法或参数是否正确',
|
|
700
|
-
`如果问题依然存在,建议到官方问答社区提问或寻找帮助:${COMMUNITY_SITE_URL}
|
|
701
|
-
]
|
|
665
|
+
`如果问题依然存在,建议到官方问答社区提问或寻找帮助:${COMMUNITY_SITE_URL}`,
|
|
666
|
+
],
|
|
702
667
|
})
|
|
703
668
|
public async isUsernameRegistered(username: string): Promise<boolean> {
|
|
704
669
|
if (typeof username !== 'string') {
|
|
@@ -706,7 +671,7 @@ class Auth {
|
|
|
706
671
|
}
|
|
707
672
|
|
|
708
673
|
const { data } = await this._request.send('auth.isUsernameRegistered', {
|
|
709
|
-
username
|
|
674
|
+
username,
|
|
710
675
|
});
|
|
711
676
|
return data?.isRegistered;
|
|
712
677
|
}
|
|
@@ -742,75 +707,39 @@ class Auth {
|
|
|
742
707
|
'请确认以下各项:',
|
|
743
708
|
' 1 - 调用 auth().signOut() 的语法或参数是否正确',
|
|
744
709
|
' 2 - 当前用户是否为匿名登录(匿名登录不支持signOut)',
|
|
745
|
-
`如果问题依然存在,建议到官方问答社区提问或寻找帮助:${COMMUNITY_SITE_URL}
|
|
746
|
-
]
|
|
710
|
+
`如果问题依然存在,建议到官方问答社区提问或寻找帮助:${COMMUNITY_SITE_URL}`,
|
|
711
|
+
],
|
|
747
712
|
})
|
|
748
713
|
public async signOut() {
|
|
749
|
-
const loginType = await this.getLoginType()
|
|
714
|
+
// const loginType = await this.getLoginType();
|
|
750
715
|
// if (loginType === LOGINTYPE.ANONYMOUS) {
|
|
751
716
|
// throw new Error(JSON.stringify({
|
|
752
717
|
// code: ERRORS.INVALID_OPERATION,
|
|
753
718
|
// msg: 'anonymous user doesn\'t support signOut action'
|
|
754
|
-
// }))
|
|
719
|
+
// }));
|
|
755
720
|
// }
|
|
756
|
-
const { refreshTokenKey, accessTokenKey, accessTokenExpireKey } = this._cache.keys
|
|
721
|
+
const { refreshTokenKey, accessTokenKey, accessTokenExpireKey } = this._cache.keys;
|
|
722
|
+
const action = 'auth.logout';
|
|
757
723
|
|
|
758
724
|
const refresh_token = await this._cache.getStoreAsync(refreshTokenKey);
|
|
759
725
|
if (!refresh_token) {
|
|
760
|
-
return
|
|
761
|
-
}
|
|
762
|
-
|
|
763
|
-
if (loginType.startsWith(OAUTH2_LOGINTYPE_PREFIX)) {
|
|
764
|
-
const accessToken = await this._cache.getStoreAsync(accessTokenKey)
|
|
765
|
-
const accessTokenExpire = Number(await this._cache.getStoreAsync(accessTokenExpireKey))
|
|
766
|
-
if (accessToken) {
|
|
767
|
-
if (Date.now() < accessTokenExpire) {
|
|
768
|
-
const resp = await this._request.fetch('/auth/v1/revoke', {
|
|
769
|
-
method: 'POST',
|
|
770
|
-
headers: {
|
|
771
|
-
'Accept': 'application/json',
|
|
772
|
-
'Content-Type': 'application/json'
|
|
773
|
-
},
|
|
774
|
-
body: JSON.stringify({
|
|
775
|
-
token: accessToken
|
|
776
|
-
})
|
|
777
|
-
})
|
|
778
|
-
const seqIdFromHeader = resp.headers.get('SeqId') || resp.headers.get('RequestId')
|
|
779
|
-
if (resp.status >= 400 && resp.status < 500) {
|
|
780
|
-
const body: any = await resp.json()
|
|
781
|
-
const seqId = body.request_id || seqIdFromHeader
|
|
782
|
-
throw new Error(`[OAuth2AuthProvider][status:${resp.status}][${body.error}(${body.error_code})] ${body.error_description} (${seqId})`)
|
|
783
|
-
}
|
|
784
|
-
else if (resp.status >= 500) {
|
|
785
|
-
const body: any = await resp.json()
|
|
786
|
-
const seqId = body.request_id || seqIdFromHeader
|
|
787
|
-
throw new Error(`[OAuth2AuthProvider][status:${resp.status}][${body.error}(${body.error_code})] ${body.error_description} (${seqId})`)
|
|
788
|
-
}
|
|
789
|
-
}
|
|
790
|
-
else {
|
|
791
|
-
// console.warn(`[SignOut] accesstoken expired`)
|
|
792
|
-
}
|
|
793
|
-
}
|
|
794
|
-
else {
|
|
795
|
-
// console.warn(`[SignOut] accesstoken not exists`)
|
|
796
|
-
}
|
|
797
|
-
}
|
|
798
|
-
else {
|
|
799
|
-
await this._request.send('auth.logout', { refresh_token })
|
|
726
|
+
return;
|
|
800
727
|
}
|
|
728
|
+
const res = await this._request.send(action, { refresh_token });
|
|
801
729
|
|
|
802
|
-
this._cache.removeStoreAsync(refreshTokenKey)
|
|
803
|
-
this._cache.removeStoreAsync(accessTokenKey)
|
|
804
|
-
this._cache.removeStoreAsync(accessTokenExpireKey)
|
|
730
|
+
this._cache.removeStoreAsync(refreshTokenKey);
|
|
731
|
+
this._cache.removeStoreAsync(accessTokenKey);
|
|
732
|
+
this._cache.removeStoreAsync(accessTokenExpireKey);
|
|
805
733
|
|
|
806
|
-
eventBus.fire(EVENTS.LOGIN_STATE_CHANGED)
|
|
734
|
+
eventBus.fire(EVENTS.LOGIN_STATE_CHANGED);
|
|
807
735
|
eventBus.fire(EVENTS.LOGIN_TYPE_CHANGED, {
|
|
808
736
|
env: this._config.env,
|
|
809
737
|
loginType: LOGINTYPE.NULL,
|
|
810
|
-
persistence: this._config.persistence
|
|
811
|
-
})
|
|
738
|
+
persistence: this._config.persistence,
|
|
739
|
+
});
|
|
812
740
|
|
|
813
|
-
|
|
741
|
+
|
|
742
|
+
return res;
|
|
814
743
|
}
|
|
815
744
|
public async onLoginStateChanged(callback: Function) {
|
|
816
745
|
eventBus.on(EVENTS.LOGIN_STATE_CHANGED, async () => {
|
|
@@ -852,13 +781,12 @@ class Auth {
|
|
|
852
781
|
const loginState = new LoginState({
|
|
853
782
|
envId: this._config.env,
|
|
854
783
|
cache: this._cache,
|
|
855
|
-
request: this._request
|
|
784
|
+
request: this._request,
|
|
856
785
|
});
|
|
857
786
|
loginState.checkLocalState();
|
|
858
787
|
return loginState;
|
|
859
|
-
} else {
|
|
860
|
-
return null;
|
|
861
788
|
}
|
|
789
|
+
return null;
|
|
862
790
|
}
|
|
863
791
|
/**
|
|
864
792
|
* 获取本地登录态-异步
|
|
@@ -869,8 +797,8 @@ class Auth {
|
|
|
869
797
|
messages: [
|
|
870
798
|
'请确认以下各项:',
|
|
871
799
|
' 1 - 调用 auth().getLoginState() 的语法或参数是否正确',
|
|
872
|
-
`如果问题依然存在,建议到官方问答社区提问或寻找帮助:${COMMUNITY_SITE_URL}
|
|
873
|
-
]
|
|
800
|
+
`如果问题依然存在,建议到官方问答社区提问或寻找帮助:${COMMUNITY_SITE_URL}`,
|
|
801
|
+
],
|
|
874
802
|
})
|
|
875
803
|
public async getLoginState() {
|
|
876
804
|
const { refreshTokenKey } = this._cache.keys;
|
|
@@ -879,13 +807,12 @@ class Auth {
|
|
|
879
807
|
const loginState = new LoginState({
|
|
880
808
|
envId: this._config.env,
|
|
881
809
|
cache: this._cache,
|
|
882
|
-
request: this._request
|
|
810
|
+
request: this._request,
|
|
883
811
|
});
|
|
884
812
|
await loginState.checkLocalStateAsync();
|
|
885
813
|
return loginState;
|
|
886
|
-
} else {
|
|
887
|
-
return null;
|
|
888
814
|
}
|
|
815
|
+
return null;
|
|
889
816
|
}
|
|
890
817
|
|
|
891
818
|
public shouldRefreshAccessToken(hook) {
|
|
@@ -898,8 +825,8 @@ class Auth {
|
|
|
898
825
|
'请确认以下各项:',
|
|
899
826
|
' 1 - 是否已登录',
|
|
900
827
|
' 2 - 调用 auth().getUserInfo() 的语法或参数是否正确',
|
|
901
|
-
`如果问题依然存在,建议到官方问答社区提问或寻找帮助:${COMMUNITY_SITE_URL}
|
|
902
|
-
]
|
|
828
|
+
`如果问题依然存在,建议到官方问答社区提问或寻找帮助:${COMMUNITY_SITE_URL}`,
|
|
829
|
+
],
|
|
903
830
|
})
|
|
904
831
|
public async getUserInfo(): Promise<any> {
|
|
905
832
|
const action = 'auth.getUserInfo';
|
|
@@ -907,12 +834,11 @@ class Auth {
|
|
|
907
834
|
const res = await this._request.send(action, {});
|
|
908
835
|
if (res.code) {
|
|
909
836
|
return res;
|
|
910
|
-
} else {
|
|
911
|
-
return {
|
|
912
|
-
...res.data,
|
|
913
|
-
requestId: res.seqId
|
|
914
|
-
};
|
|
915
837
|
}
|
|
838
|
+
return {
|
|
839
|
+
...res.data,
|
|
840
|
+
requestId: res.seqId,
|
|
841
|
+
};
|
|
916
842
|
}
|
|
917
843
|
/**
|
|
918
844
|
* 获取Http鉴权header,用于云接入 HTTP 访问云函数时的鉴权
|
|
@@ -922,7 +848,7 @@ class Auth {
|
|
|
922
848
|
const refreshToken = this._cache.getStore(refreshTokenKey);
|
|
923
849
|
const accessToken = this._cache.getStore(accessTokenKey);
|
|
924
850
|
return {
|
|
925
|
-
'x-cloudbase-credentials': accessToken
|
|
851
|
+
'x-cloudbase-credentials': `${accessToken}/@@/${refreshToken}`,
|
|
926
852
|
};
|
|
927
853
|
}
|
|
928
854
|
/**
|
|
@@ -936,7 +862,7 @@ class Auth {
|
|
|
936
862
|
const refreshToken = await this._cache.getStoreAsync(refreshTokenKey);
|
|
937
863
|
const accessToken = await this._cache.getStoreAsync(accessTokenKey);
|
|
938
864
|
return {
|
|
939
|
-
'x-cloudbase-credentials': accessToken
|
|
865
|
+
'x-cloudbase-credentials': `${accessToken}/@@/${refreshToken}`,
|
|
940
866
|
};
|
|
941
867
|
}
|
|
942
868
|
|
|
@@ -951,14 +877,14 @@ class Auth {
|
|
|
951
877
|
'请确认以下各项:',
|
|
952
878
|
' 1 - 调用语法或参数是否正确',
|
|
953
879
|
' 2 - 当前环境是否开通了短信验证码登录',
|
|
954
|
-
`如果问题依然存在,建议到官方问答社区提问或寻找帮助:${COMMUNITY_SITE_URL}
|
|
955
|
-
]
|
|
880
|
+
`如果问题依然存在,建议到官方问答社区提问或寻找帮助:${COMMUNITY_SITE_URL}`,
|
|
881
|
+
],
|
|
956
882
|
})
|
|
957
883
|
public async sendPhoneCode(phoneNumber: string): Promise<boolean> {
|
|
958
884
|
const { data } = await this._request.send('auth.sendPhoneCode', {
|
|
959
|
-
phoneNumber: transformPhone(phoneNumber)
|
|
885
|
+
phoneNumber: transformPhone(phoneNumber),
|
|
960
886
|
});
|
|
961
|
-
return data.SendStatus === 'Ok'
|
|
887
|
+
return data.SendStatus === 'Ok';
|
|
962
888
|
}
|
|
963
889
|
|
|
964
890
|
/**
|
|
@@ -991,7 +917,7 @@ class Auth {
|
|
|
991
917
|
}) {
|
|
992
918
|
return this.phoneAuthProvider().signIn({
|
|
993
919
|
...param,
|
|
994
|
-
signMethod: SIGN_METHOD.FORCERESETPWD
|
|
920
|
+
signMethod: SIGN_METHOD.FORCERESETPWD,
|
|
995
921
|
});
|
|
996
922
|
}
|
|
997
923
|
|
|
@@ -1016,7 +942,7 @@ const EVENTS = {
|
|
|
1016
942
|
// 匿名账户被转正后触发
|
|
1017
943
|
ANONYMOUS_CONVERTED: 'anonymousConverted',
|
|
1018
944
|
// access token刷新后触发
|
|
1019
|
-
ACCESS_TOKEN_REFRESHD: 'refreshAccessToken'
|
|
945
|
+
ACCESS_TOKEN_REFRESHD: 'refreshAccessToken',
|
|
1020
946
|
};
|
|
1021
947
|
|
|
1022
948
|
const component: ICloudbaseComponent = {
|
|
@@ -1029,10 +955,10 @@ const component: ICloudbaseComponent = {
|
|
|
1029
955
|
EVENTS.LOGIN_STATE_EXPIRED,
|
|
1030
956
|
EVENTS.LOGIN_STATE_CHANGED,
|
|
1031
957
|
EVENTS.ACCESS_TOKEN_REFRESHD,
|
|
1032
|
-
EVENTS.ANONYMOUS_CONVERTED
|
|
1033
|
-
]
|
|
958
|
+
EVENTS.ANONYMOUS_CONVERTED,
|
|
959
|
+
],
|
|
1034
960
|
},
|
|
1035
|
-
entity
|
|
961
|
+
entity(config: Pick<ICloudbaseAuthConfig, 'region' | 'persistence'> = { region: '', persistence: 'local' }) {
|
|
1036
962
|
if (this.authInstance) {
|
|
1037
963
|
printWarn(ERRORS.INVALID_OPERATION, 'every cloudbase instance should has only one auth object');
|
|
1038
964
|
return this.authInstance;
|
|
@@ -1041,7 +967,7 @@ const component: ICloudbaseComponent = {
|
|
|
1041
967
|
// 如不明确指定persistence则优先取各平台adapter首选,其次session
|
|
1042
968
|
const newPersistence = config.persistence || adapter.primaryStorage;
|
|
1043
969
|
if (newPersistence && (newPersistence !== this.config.persistence)) {
|
|
1044
|
-
this.updateConfig({ persistence: newPersistence })
|
|
970
|
+
this.updateConfig({ persistence: newPersistence });
|
|
1045
971
|
}
|
|
1046
972
|
|
|
1047
973
|
const { env, persistence, debug } = this.config;
|
|
@@ -1052,11 +978,11 @@ const component: ICloudbaseComponent = {
|
|
|
1052
978
|
debug,
|
|
1053
979
|
cache: this.cache,
|
|
1054
980
|
request: this.request,
|
|
1055
|
-
runtime
|
|
981
|
+
runtime,
|
|
1056
982
|
});
|
|
1057
983
|
return this.authInstance;
|
|
1058
|
-
}
|
|
1059
|
-
}
|
|
984
|
+
},
|
|
985
|
+
};
|
|
1060
986
|
|
|
1061
987
|
try {
|
|
1062
988
|
// 尝试自动注册至全局变量cloudbase
|
|
@@ -1069,7 +995,7 @@ export {
|
|
|
1069
995
|
Auth,
|
|
1070
996
|
AuthProvider,
|
|
1071
997
|
EVENTS,
|
|
1072
|
-
eventBus
|
|
998
|
+
eventBus,
|
|
1073
999
|
};
|
|
1074
1000
|
/**
|
|
1075
1001
|
* @api 手动注册至cloudbase app
|
|
@@ -1102,9 +1028,9 @@ export function registerProvider(name: string, provider: IProvider) {
|
|
|
1102
1028
|
if (!this[privateName]) {
|
|
1103
1029
|
this[privateName] = new provider({
|
|
1104
1030
|
...options,
|
|
1105
|
-
...this._config
|
|
1031
|
+
...this._config,
|
|
1106
1032
|
});
|
|
1107
1033
|
}
|
|
1108
1034
|
return this[privateName];
|
|
1109
1035
|
};
|
|
1110
|
-
}
|
|
1036
|
+
}
|