@@ -31,7 +31,7 @@ import { ApplicationVerifierInternal } from '../../model/application_verifier';
31
31
import { PhoneAuthCredential } from '../../core/credentials/phone' ;
32
32
import { AuthErrorCode } from '../../core/errors' ;
33
33
import { _assertLinkedStatus , _link } from '../../core/user/link_unlink' ;
34
- import { _assert } from '../../core/util/assert' ;
34
+ import { _assert , _errorWithCustomMessage } from '../../core/util/assert' ;
35
35
import { AuthInternal } from '../../model/auth' ;
36
36
import {
37
37
linkWithCredential ,
@@ -78,12 +78,16 @@ class ConfirmationResultImpl implements ConfirmationResult {
78
78
return this . onConfirmation ( authCredential ) ;
79
79
}
80
80
81
- async confirmWithWebOTP ( webOTPTimeout : number ) : Promise < UserCredential > {
81
+ async confirmWithWebOTP ( auth : Auth , webOTPTimeout : number ) : Promise < UserCredential > {
82
82
if ( 'OTPCredential' in window ) {
83
83
const abortController = new AbortController ( ) ;
84
84
const timer = setTimeout ( ( ) => {
85
85
abortController . abort ( ) ;
86
- throw new FirebaseError ( 'WEB_OTP_TIMEOUT' , 'auth/web-otp-timeout' ) ;
86
+ throw _errorWithCustomMessage (
87
+ auth ,
88
+ AuthErrorCode . WEB_OTP_NOT_RETRIEVED ,
89
+ `Web OTP code is not fetched before timeout`
90
+ ) ;
87
91
} , webOTPTimeout * 1000 ) ;
88
92
89
93
// @ts -ignore - ignore types for testing
@@ -185,8 +189,10 @@ export async function signInWithPhoneNumber(
185
189
const verificationId = await _verifyPhoneNumber (
186
190
authInternal ,
187
191
phoneNumber ,
188
- getModularInstance ( appVerifier as ApplicationVerifierInternal )
189
- ) ;
192
+ getModularInstance ( appVerifier as ApplicationVerifierInternal ) ,
193
+ false ,
194
+ 30
195
+ ) as string ;
190
196
191
197
const confirmationRes = new ConfirmationResultImpl ( verificationId , cred =>
192
198
signInWithCredential ( authInternal , cred )
@@ -225,8 +231,10 @@ export async function linkWithPhoneNumber(
225
231
const verificationId = await _verifyPhoneNumber (
226
232
userInternal . auth ,
227
233
phoneNumber ,
228
- getModularInstance ( appVerifier as ApplicationVerifierInternal )
229
- ) ;
234
+ getModularInstance ( appVerifier as ApplicationVerifierInternal ) ,
235
+ false ,
236
+ 30
237
+ ) as string ;
230
238
return new ConfirmationResultImpl ( verificationId , cred =>
231
239
linkWithCredential ( userInternal , cred )
232
240
) ;
@@ -255,8 +263,10 @@ export async function reauthenticateWithPhoneNumber(
255
263
const verificationId = await _verifyPhoneNumber (
256
264
userInternal . auth ,
257
265
phoneNumber ,
258
- getModularInstance ( appVerifier as ApplicationVerifierInternal )
259
- ) ;
266
+ getModularInstance ( appVerifier as ApplicationVerifierInternal ) ,
267
+ false ,
268
+ 30
269
+ ) as string ;
260
270
return new ConfirmationResultImpl ( verificationId , cred =>
261
271
reauthenticateWithCredential ( userInternal , cred )
262
272
) ;
@@ -269,8 +279,10 @@ export async function reauthenticateWithPhoneNumber(
269
279
export async function _verifyPhoneNumber (
270
280
auth : AuthInternal ,
271
281
options : PhoneInfoOptions | string ,
272
- verifier : ApplicationVerifierInternal
273
- ) : Promise < string > {
282
+ verifier : ApplicationVerifierInternal ,
283
+ useWebOTP = false ,
284
+ webOTPTimeout = 30
285
+ ) : Promise < string | UserCredential > {
274
286
const recaptchaToken = await verifier . verify ( ) ;
275
287
276
288
try {
@@ -294,7 +306,7 @@ export async function _verifyPhoneNumber(
294
306
} else {
295
307
phoneInfoOptions = options ;
296
308
}
297
-
309
+ let verificationId = "" ;
298
310
if ( 'session' in phoneInfoOptions ) {
299
311
const session = phoneInfoOptions . session as MultiFactorSessionImpl ;
300
312
@@ -311,7 +323,7 @@ export async function _verifyPhoneNumber(
311
323
recaptchaToken
312
324
}
313
325
} ) ;
314
- return response . phoneSessionInfo . sessionInfo ;
326
+ verificationId = response . phoneSessionInfo . sessionInfo ;
315
327
} else {
316
328
_assert (
317
329
session . type === MultiFactorSessionType . SIGN_IN ,
@@ -329,14 +341,27 @@ export async function _verifyPhoneNumber(
329
341
recaptchaToken
330
342
}
331
343
} ) ;
332
- return response . phoneResponseInfo . sessionInfo ;
344
+ verificationId = response . phoneResponseInfo . sessionInfo ;
333
345
}
334
346
} else {
335
347
const { sessionInfo } = await sendPhoneVerificationCode ( auth , {
336
348
phoneNumber : phoneInfoOptions . phoneNumber ,
337
349
recaptchaToken
338
350
} ) ;
339
- return sessionInfo ;
351
+ verificationId = sessionInfo ;
352
+ }
353
+ const authInternal = _castAuth ( auth ) ;
354
+ const confirmationRes = new ConfirmationResultImpl ( verificationId , cred =>
355
+ signInWithCredential ( authInternal , cred )
356
+ ) ;
357
+ if ( useWebOTP ) {
358
+ try {
359
+ return confirmationRes . confirmWithWebOTP ( webOTPTimeout ) ;
360
+ } catch ( error ) {
361
+ throw new FirebaseError ( 'WEB_OTP_BROKEN' , 'auth/web-otp-broken' ) ;
362
+ }
363
+ } else {
364
+ return verificationId ;
340
365
}
341
366
} finally {
342
367
verifier . _reset ( ) ;
0 commit comments