@@ -61,9 +61,14 @@ export class RecoveryPhoneService {
6161 * by sending the phone number provided an OTP code to verify.
6262 * @param uid The account id
6363 * @param phoneNumber The phone number to register
64+ * @param localizedMessageBody Optional localized message body
6465 * @returns True if code was sent and stored
6566 */
66- public async setupPhoneNumber ( uid : string , phoneNumber : string ) {
67+ public async setupPhoneNumber (
68+ uid : string ,
69+ phoneNumber : string ,
70+ getFormattedMessage ?: ( code : string ) => Promise < string >
71+ ) {
6772 if ( ! this . config . enabled ) {
6873 throw new RecoveryPhoneNotEnabled ( ) ;
6974 }
@@ -90,21 +95,26 @@ export class RecoveryPhoneService {
9095 }
9196
9297 const code = await this . otpCode . generateCode ( ) ;
93- const msg = await this . smsManager . sendSMS ( {
94- to : phoneNumber ,
95- body : code ,
96- } ) ;
9798
98- if ( ! this . isSuccessfulSmsSend ( msg ) ) {
99- return false ;
100- }
10199 await this . recoveryPhoneManager . storeUnconfirmed (
102100 uid ,
103101 code ,
104102 phoneNumber ,
105103 true
106104 ) ;
107- return true ;
105+
106+ const formattedSMSbody = getFormattedMessage
107+ ? await getFormattedMessage ( code )
108+ : undefined ;
109+
110+ const smsBody = formattedSMSbody || `${ code } ` ;
111+
112+ const msg = await this . smsManager . sendSMS ( {
113+ to : phoneNumber ,
114+ body : smsBody ,
115+ } ) ;
116+
117+ return this . isSuccessfulSmsSend ( msg ) ;
108118 }
109119
110120 /**
@@ -323,9 +333,13 @@ export class RecoveryPhoneService {
323333 /**
324334 * Sends an totp code to a user
325335 * @param uid Account id
336+ * @param getFormattedMessage Optional template function to format the message
326337 * @returns True if message didn't fail to send.
327338 */
328- public async sendCode ( uid : string ) {
339+ public async sendCode (
340+ uid : string ,
341+ getFormattedMessage ?: ( code : string ) => Promise < string >
342+ ) {
329343 if ( ! this . config . enabled ) {
330344 throw new RecoveryPhoneNotEnabled ( ) ;
331345 }
@@ -339,9 +353,16 @@ export class RecoveryPhoneService {
339353 phoneNumber ,
340354 false
341355 ) ;
356+
357+ const formattedSMSbody = getFormattedMessage
358+ ? await getFormattedMessage ( code )
359+ : undefined ;
360+
361+ const smsBody = formattedSMSbody || `${ code } ` ;
362+
342363 const msg = await this . smsManager . sendSMS ( {
343364 to : phoneNumber ,
344- body : ` ${ code } ` , // TODO: Other text or translation around code?
365+ body : smsBody ,
345366 } ) ;
346367
347368 return this . isSuccessfulSmsSend ( msg ) ;
0 commit comments