Skip to content

Commit ec94aa6

Browse files
committed
changing verifywithphonenumber
1 parent faba2c3 commit ec94aa6

File tree

3 files changed

+45
-18
lines changed

3 files changed

+45
-18
lines changed

common/api-review/auth.api.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,7 @@ export class PhoneAuthProvider {
603603
static readonly PHONE_SIGN_IN_METHOD: 'phone';
604604
static readonly PROVIDER_ID: 'phone';
605605
readonly providerId: "phone";
606-
verifyPhoneNumber(phoneOptions: PhoneInfoOptions | string, applicationVerifier: ApplicationVerifier): Promise<string>;
606+
verifyPhoneNumber(phoneOptions: PhoneInfoOptions | string, applicationVerifier: ApplicationVerifier): Promise<string | UserCredential>;
607607
}
608608

609609
// @public

packages/auth/src/platform_browser/providers/phone.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,13 @@ export class PhoneAuthProvider {
105105
verifyPhoneNumber(
106106
phoneOptions: PhoneInfoOptions | string,
107107
applicationVerifier: ApplicationVerifier
108-
): Promise<string> {
108+
): Promise<string | UserCredential> {
109109
return _verifyPhoneNumber(
110110
this.auth,
111111
phoneOptions,
112-
getModularInstance(applicationVerifier as ApplicationVerifierInternal)
112+
getModularInstance(applicationVerifier as ApplicationVerifierInternal),
113+
false,
114+
30
113115
);
114116
}
115117

packages/auth/src/platform_browser/strategies/phone.ts

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import { ApplicationVerifierInternal } from '../../model/application_verifier';
3131
import { PhoneAuthCredential } from '../../core/credentials/phone';
3232
import { AuthErrorCode } from '../../core/errors';
3333
import { _assertLinkedStatus, _link } from '../../core/user/link_unlink';
34-
import { _assert } from '../../core/util/assert';
34+
import { _assert, _errorWithCustomMessage } from '../../core/util/assert';
3535
import { AuthInternal } from '../../model/auth';
3636
import {
3737
linkWithCredential,
@@ -78,12 +78,16 @@ class ConfirmationResultImpl implements ConfirmationResult {
7878
return this.onConfirmation(authCredential);
7979
}
8080

81-
async confirmWithWebOTP(webOTPTimeout: number): Promise<UserCredential> {
81+
async confirmWithWebOTP (auth:Auth, webOTPTimeout: number): Promise<UserCredential> {
8282
if ('OTPCredential' in window) {
8383
const abortController = new AbortController();
8484
const timer = setTimeout(() => {
8585
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+
);
8791
}, webOTPTimeout * 1000);
8892

8993
// @ts-ignore - ignore types for testing
@@ -185,8 +189,10 @@ export async function signInWithPhoneNumber(
185189
const verificationId = await _verifyPhoneNumber(
186190
authInternal,
187191
phoneNumber,
188-
getModularInstance(appVerifier as ApplicationVerifierInternal)
189-
);
192+
getModularInstance(appVerifier as ApplicationVerifierInternal),
193+
false,
194+
30
195+
) as string;
190196

191197
const confirmationRes = new ConfirmationResultImpl(verificationId, cred =>
192198
signInWithCredential(authInternal, cred)
@@ -225,8 +231,10 @@ export async function linkWithPhoneNumber(
225231
const verificationId = await _verifyPhoneNumber(
226232
userInternal.auth,
227233
phoneNumber,
228-
getModularInstance(appVerifier as ApplicationVerifierInternal)
229-
);
234+
getModularInstance(appVerifier as ApplicationVerifierInternal),
235+
false,
236+
30
237+
) as string;
230238
return new ConfirmationResultImpl(verificationId, cred =>
231239
linkWithCredential(userInternal, cred)
232240
);
@@ -255,8 +263,10 @@ export async function reauthenticateWithPhoneNumber(
255263
const verificationId = await _verifyPhoneNumber(
256264
userInternal.auth,
257265
phoneNumber,
258-
getModularInstance(appVerifier as ApplicationVerifierInternal)
259-
);
266+
getModularInstance(appVerifier as ApplicationVerifierInternal),
267+
false,
268+
30
269+
) as string;
260270
return new ConfirmationResultImpl(verificationId, cred =>
261271
reauthenticateWithCredential(userInternal, cred)
262272
);
@@ -269,8 +279,10 @@ export async function reauthenticateWithPhoneNumber(
269279
export async function _verifyPhoneNumber(
270280
auth: AuthInternal,
271281
options: PhoneInfoOptions | string,
272-
verifier: ApplicationVerifierInternal
273-
): Promise<string> {
282+
verifier: ApplicationVerifierInternal,
283+
useWebOTP = false,
284+
webOTPTimeout = 30
285+
): Promise<string | UserCredential>{
274286
const recaptchaToken = await verifier.verify();
275287

276288
try {
@@ -294,7 +306,7 @@ export async function _verifyPhoneNumber(
294306
} else {
295307
phoneInfoOptions = options;
296308
}
297-
309+
let verificationId = "";
298310
if ('session' in phoneInfoOptions) {
299311
const session = phoneInfoOptions.session as MultiFactorSessionImpl;
300312

@@ -311,7 +323,7 @@ export async function _verifyPhoneNumber(
311323
recaptchaToken
312324
}
313325
});
314-
return response.phoneSessionInfo.sessionInfo;
326+
verificationId = response.phoneSessionInfo.sessionInfo;
315327
} else {
316328
_assert(
317329
session.type === MultiFactorSessionType.SIGN_IN,
@@ -329,14 +341,27 @@ export async function _verifyPhoneNumber(
329341
recaptchaToken
330342
}
331343
});
332-
return response.phoneResponseInfo.sessionInfo;
344+
verificationId = response.phoneResponseInfo.sessionInfo;
333345
}
334346
} else {
335347
const { sessionInfo } = await sendPhoneVerificationCode(auth, {
336348
phoneNumber: phoneInfoOptions.phoneNumber,
337349
recaptchaToken
338350
});
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;
340365
}
341366
} finally {
342367
verifier._reset();

0 commit comments

Comments
 (0)