Skip to content

Commit afeab7c

Browse files
authored
feat: verify phone number before update
1 parent a45a50c commit afeab7c

File tree

4 files changed

+61
-8
lines changed

4 files changed

+61
-8
lines changed

src/cloudfunction.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,7 @@ module.exports = function(AV) {
8383
* @param {String} data.mobilePhoneNumber
8484
* @param {String} [data.template] sms template name
8585
* @param {String} [data.sign] sms signature name
86-
* @param {AuthOptions} [options] AuthOptions plus:
87-
* @param {String} [options.validateToken] a validate token returned by {@link AV.Cloud.verifyCaptcha}
86+
* @param {SMSAuthOptions} [options]
8887
* @return {Promise} A promise that will be resolved if the request succeed
8988
*/
9089
requestSmsCode(data, options = {}) {

src/index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,12 @@ module.exports = AV;
4646
* @property {AV.User} [user] Specify a user to excute the operation as. The user must have _sessionToken. This option will be ignored if sessionToken option provided.
4747
* @property {Boolean} [useMasterKey] Indicates whether masterKey is used for this operation. Only valid when masterKey is set.
4848
*/
49+
50+
/**
51+
* Options to controll the authentication for an SMS operation
52+
* @typedef {Object} SMSAuthOptions
53+
* @property {String} [sessionToken] Specify a user to excute the operation as.
54+
* @property {AV.User} [user] Specify a user to excute the operation as. The user must have _sessionToken. This option will be ignored if sessionToken option provided.
55+
* @property {Boolean} [useMasterKey] Indicates whether masterKey is used for this operation. Only valid when masterKey is set.
56+
* @property {String} [validateToken] a validate token returned by {@link AV.Cloud.verifyCaptcha}
57+
*/

src/user.js

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1537,8 +1537,7 @@ module.exports = function(AV) {
15371537
*
15381538
* @param {String} mobilePhoneNumber The mobile phone number associated with the
15391539
* user that doesn't verify their mobile phone number.
1540-
* @param {AuthOptions} [options] AuthOptions plus:
1541-
* @param {String} [options.validateToken] a validate token returned by {@link AV.Cloud.verifyCaptcha}
1540+
* @param {SMSAuthOptions} [options]
15421541
* @return {Promise}
15431542
*/
15441543
requestMobilePhoneVerify: function(mobilePhoneNumber, options = {}) {
@@ -1566,8 +1565,7 @@ module.exports = function(AV) {
15661565
*
15671566
* @param {String} mobilePhoneNumber The mobile phone number associated with the
15681567
* user that doesn't verify their mobile phone number.
1569-
* @param {AuthOptions} [options] AuthOptions plus:
1570-
* @param {String} [options.validateToken] a validate token returned by {@link AV.Cloud.verifyCaptcha}
1568+
* @param {SMSAuthOptions} [options]
15711569
* @return {Promise}
15721570
*/
15731571
requestPasswordResetBySmsCode: function(mobilePhoneNumber, options = {}) {
@@ -1588,6 +1586,45 @@ module.exports = function(AV) {
15881586
return request;
15891587
},
15901588

1589+
/**
1590+
* Requests a change mobile phone number sms code to be sent to the mobilePhoneNumber.
1591+
* This sms code allows current user to reset it's mobilePhoneNumber by
1592+
* calling {@link AV.User.changePhoneNumber}
1593+
* @param {String} mobilePhoneNumber
1594+
* @param {Number} [ttl] ttl of sms code (default is 6 minutes)
1595+
* @param {SMSAuthOptions} [options]
1596+
* @return {Promise}
1597+
*/
1598+
requestChangePhoneNumber(mobilePhoneNumber, ttl, options) {
1599+
const data = { mobilePhoneNumber };
1600+
if (ttl) {
1601+
data.ttl = options.ttl;
1602+
}
1603+
if (options && options.validateToken) {
1604+
data.validate_token = options.validateToken;
1605+
}
1606+
return AVRequest(
1607+
'requestChangePhoneNumber',
1608+
null,
1609+
null,
1610+
'POST',
1611+
data,
1612+
options
1613+
);
1614+
},
1615+
1616+
/**
1617+
* Makes a call to reset user's account mobilePhoneNumber by sms code.
1618+
* The sms code is sent by {@link AV.User.requestChangePhoneNumber}
1619+
* @param {String} mobilePhoneNumber
1620+
* @param {String} code The sms code.
1621+
* @return {Promise}
1622+
*/
1623+
changePhoneNumber(mobilePhoneNumber, code) {
1624+
const data = { mobilePhoneNumber, code };
1625+
return AVRequest('changePhoneNumber', null, null, 'POST', data);
1626+
},
1627+
15911628
/**
15921629
* Makes a call to reset user's account password by sms code and new password.
15931630
* The sms code is sent by AV.User.requestPasswordResetBySmsCode.
@@ -1627,8 +1664,7 @@ module.exports = function(AV) {
16271664
*
16281665
* @param {String} mobilePhoneNumber The mobile phone number associated with the
16291666
* user that want to login by AV.User.logInWithMobilePhoneSmsCode
1630-
* @param {AuthOptions} [options] AuthOptions plus:
1631-
* @param {String} [options.validateToken] a validate token returned by {@link AV.Cloud.verifyCaptcha}
1667+
* @param {SMSAuthOptions} [options]
16321668
* @return {Promise}
16331669
*/
16341670
requestLoginSmsCode: function(mobilePhoneNumber, options = {}) {

storage.d.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -787,6 +787,15 @@ export class User extends Object {
787787
options?: AuthOptions
788788
): Promise<User>;
789789
static verifyMobilePhone(code: string, options?: AuthOptions): Promise<User>;
790+
static requestChangePhoneNumber(
791+
mobilePhoneNumber: string,
792+
ttl?: number,
793+
options?: SMSAuthOptions
794+
): Promise<void>;
795+
static changePhoneNumber(
796+
mobilePhoneNumber: string,
797+
code: string
798+
): Promise<void>;
790799

791800
static followerQuery<T extends User>(userObjectId: string): Query<T>;
792801
static followeeQuery<T extends User>(userObjectId: string): Query<T>;

0 commit comments

Comments
 (0)