@@ -59,9 +59,14 @@ export class RecoveryPhoneService {
5959 * by sending the phone number provided an OTP code to verify.
6060 * @param uid The account id
6161 * @param phoneNumber The phone number to register
62+ * @param localizedMessageBody Optional localized message body
6263 * @returns True if code was sent and stored
6364 */
64- public async setupPhoneNumber ( uid : string , phoneNumber : string ) {
65+ public async setupPhoneNumber (
66+ uid : string ,
67+ phoneNumber : string ,
68+ getFormattedMessage ?: ( code : string ) => Promise < string >
69+ ) {
6570 if ( ! this . config . enabled ) {
6671 throw new RecoveryPhoneNotEnabled ( ) ;
6772 }
@@ -88,21 +93,26 @@ export class RecoveryPhoneService {
8893 }
8994
9095 const code = await this . otpCode . generateCode ( ) ;
91- const msg = await this . smsManager . sendSMS ( {
92- to : phoneNumber ,
93- body : code ,
94- } ) ;
9596
96- if ( ! this . isSuccessfulSmsSend ( msg ) ) {
97- return false ;
98- }
9997 await this . recoveryPhoneManager . storeUnconfirmed (
10098 uid ,
10199 code ,
102100 phoneNumber ,
103101 true
104102 ) ;
105- return true ;
103+
104+ const formattedSMSbody = getFormattedMessage
105+ ? await getFormattedMessage ( code )
106+ : undefined ;
107+
108+ const smsBody = formattedSMSbody || `${ code } ` ;
109+
110+ const msg = await this . smsManager . sendSMS ( {
111+ to : phoneNumber ,
112+ body : smsBody ,
113+ } ) ;
114+
115+ return this . isSuccessfulSmsSend ( msg ) ;
106116 }
107117
108118 /**
@@ -294,9 +304,13 @@ export class RecoveryPhoneService {
294304 /**
295305 * Sends an totp code to a user
296306 * @param uid Account id
307+ * @param getFormattedMessage Optional template function to format the message
297308 * @returns True if message didn't fail to send.
298309 */
299- public async sendCode ( uid : string ) {
310+ public async sendCode (
311+ uid : string ,
312+ getFormattedMessage ?: ( code : string ) => Promise < string >
313+ ) {
300314 if ( ! this . config . enabled ) {
301315 throw new RecoveryPhoneNotEnabled ( ) ;
302316 }
@@ -310,9 +324,16 @@ export class RecoveryPhoneService {
310324 phoneNumber ,
311325 false
312326 ) ;
327+
328+ const formattedSMSbody = getFormattedMessage
329+ ? await getFormattedMessage ( code )
330+ : undefined ;
331+
332+ const smsBody = formattedSMSbody || `${ code } ` ;
333+
313334 const msg = await this . smsManager . sendSMS ( {
314335 to : phoneNumber ,
315- body : ` ${ code } ` , // TODO: Other text or translation around code?
336+ body : smsBody ,
316337 } ) ;
317338
318339 return this . isSuccessfulSmsSend ( msg ) ;
0 commit comments