Skip to content

Commit ae40dcf

Browse files
authored
Merge faba2c3 into 66cebfb
2 parents 66cebfb + faba2c3 commit ae40dcf

File tree

15 files changed

+5715
-84
lines changed

15 files changed

+5715
-84
lines changed

common/api-review/auth.api.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,11 @@ export const AuthErrorCodes: {
235235
readonly MISSING_RECAPTCHA_VERSION: "auth/missing-recaptcha-version";
236236
readonly INVALID_RECAPTCHA_VERSION: "auth/invalid-recaptcha-version";
237237
readonly INVALID_REQ_TYPE: "auth/invalid-req-type";
238+
readonly WEB_OTP_TIMEOUT: "auth/web-otp-timeout";
239+
readonly WEB_OTP_BROKEN: "auth/web-otp-broken";
240+
readonly WEB_OTP_NOT_RETRIEVED: "auth/web-otp-not-retrieved";
241+
readonly WEB_OTP_NOT_SUPPORTED: "auth/web-otp-not-supported";
242+
readonly WEB_OTP_NOT_DEFINED: "auth/web-otp-not-defined";
238243
};
239244

240245
// @public
@@ -281,6 +286,7 @@ export interface Config {
281286
// @public
282287
export interface ConfirmationResult {
283288
confirm(verificationCode: string): Promise<UserCredential>;
289+
confirmWithWebOTP(webOTPTimeout: number): Promise<UserCredential>;
284290
readonly verificationId: string;
285291
}
286292

@@ -747,7 +753,7 @@ export function signInWithEmailAndPassword(auth: Auth, email: string, password:
747753
export function signInWithEmailLink(auth: Auth, email: string, emailLink?: string): Promise<UserCredential>;
748754

749755
// @public
750-
export function signInWithPhoneNumber(auth: Auth, phoneNumber: string, appVerifier: ApplicationVerifier): Promise<ConfirmationResult>;
756+
export function signInWithPhoneNumber(auth: Auth, phoneNumber: string, appVerifier: ApplicationVerifier, useWebOTP?: boolean, webOTPTimeout?: number): Promise<ConfirmationResult | UserCredential>;
751757

752758
// @public
753759
export function signInWithPopup(auth: Auth, provider: AuthProvider, resolver?: PopupRedirectResolver): Promise<UserCredential>;

docs-devsite/auth.confirmationresult.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export interface ConfirmationResult
2929
| Method | Description |
3030
| --- | --- |
3131
| [confirm(verificationCode)](./auth.confirmationresult.md#confirmationresultconfirm) | Finishes a phone number sign-in, link, or reauthentication. |
32+
| [confirmWithWebOTP(webOTPTimeout)](./auth.confirmationresult.md#confirmationresultconfirmwithwebotp) | Automatically fetches the verification code from SMS message. Then, it calls confirm(verificationCode) to finish a phone number sign-in, link, or reauthentication. |
3233

3334
## ConfirmationResult.verificationId
3435

@@ -72,3 +73,23 @@ const userCredential = await confirmationResult.confirm(verificationCode);
7273

7374
```
7475

76+
## ConfirmationResult.confirmWithWebOTP()
77+
78+
Automatically fetches the verification code from SMS message. Then, it calls confirm(verificationCode) to finish a phone number sign-in, link, or reauthentication.
79+
80+
<b>Signature:</b>
81+
82+
```typescript
83+
confirmWithWebOTP(webOTPTimeout: number): Promise<UserCredential>;
84+
```
85+
86+
### Parameters
87+
88+
| Parameter | Type | Description |
89+
| --- | --- | --- |
90+
| webOTPTimeout | number | Error would be thrown if WebOTP does not resolve within this specified timeout parameter (milliseconds). |
91+
92+
<b>Returns:</b>
93+
94+
Promise&lt;[UserCredential](./auth.usercredential.md#usercredential_interface)<!-- -->&gt;
95+

docs-devsite/auth.md

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ Firebase Authentication
4343
| [signInWithCustomToken(auth, customToken)](./auth.md#signinwithcustomtoken) | Asynchronously signs in using a custom token. |
4444
| [signInWithEmailAndPassword(auth, email, password)](./auth.md#signinwithemailandpassword) | Asynchronously signs in using an email and password. |
4545
| [signInWithEmailLink(auth, email, emailLink)](./auth.md#signinwithemaillink) | Asynchronously signs in using an email and sign-in email link. |
46-
| [signInWithPhoneNumber(auth, phoneNumber, appVerifier)](./auth.md#signinwithphonenumber) | Asynchronously signs in using a phone number. |
46+
| [signInWithPhoneNumber(auth, phoneNumber, appVerifier, useWebOTP, webOTPTimeout)](./auth.md#signinwithphonenumber) | Asynchronously signs in using a phone number. |
4747
| [signInWithPopup(auth, provider, resolver)](./auth.md#signinwithpopup) | Authenticates a Firebase client using a popup-based OAuth authentication flow. |
4848
| [signInWithRedirect(auth, provider, resolver)](./auth.md#signinwithredirect) | Authenticates a Firebase client using a full-page redirect flow. |
4949
| [signOut(auth)](./auth.md#signout) | Signs out the current user. |
@@ -878,7 +878,11 @@ if(isSignInWithEmailLink(auth, emailLink)) {
878878

879879
Asynchronously signs in using a phone number.
880880

881-
This method sends a code via SMS to the given phone number, and returns a [ConfirmationResult](./auth.confirmationresult.md#confirmationresult_interface)<!-- -->. After the user provides the code sent to their phone, call [ConfirmationResult.confirm()](./auth.confirmationresult.md#confirmationresultconfirm) with the code to sign the user in.
881+
This method sends a code via SMS to the given phone number. It returns a [ConfirmationResult](./auth.confirmationresult.md#confirmationresult_interface) if is set to false. If is set to true, the method will return a [UserCredential](./auth.usercredential.md#usercredential_interface)<!-- -->.
882+
883+
If is set to `false`<!-- -->, webOTP autofill is disabled. After the user provides the code sent to their phone, call [ConfirmationResult.confirm()](./auth.confirmationresult.md#confirmationresultconfirm) with the code to sign the user in. [ConfirmationResult](./auth.confirmationresult.md#confirmationresult_interface) is returned.
884+
885+
If is set to `true`<!-- -->, webOTP autofill is enabled and the verfication code is automatically fetched, then [ConfirmationResult.confirm()](./auth.confirmationresult.md#confirmationresultconfirm) will be called with the code to sign the user in. [UserCredential](./auth.usercredential.md#usercredential_interface) is returned.
882886

883887
For abuse prevention, this method also requires a [ApplicationVerifier](./auth.applicationverifier.md#applicationverifier_interface)<!-- -->. This SDK includes a reCAPTCHA-based implementation, [RecaptchaVerifier](./auth.recaptchaverifier.md#recaptchaverifier_class)<!-- -->. This function can work on other platforms that do not support the [RecaptchaVerifier](./auth.recaptchaverifier.md#recaptchaverifier_class) (like React Native), but you need to use a third-party [ApplicationVerifier](./auth.applicationverifier.md#applicationverifier_interface) implementation.
884888

@@ -887,7 +891,7 @@ This method does not work in a Node.js environment.
887891
<b>Signature:</b>
888892

889893
```typescript
890-
export declare function signInWithPhoneNumber(auth: Auth, phoneNumber: string, appVerifier: ApplicationVerifier): Promise<ConfirmationResult>;
894+
export declare function signInWithPhoneNumber(auth: Auth, phoneNumber: string, appVerifier: ApplicationVerifier, useWebOTP?: boolean, webOTPTimeout?: number): Promise<ConfirmationResult | UserCredential>;
891895
```
892896

893897
### Parameters
@@ -897,18 +901,23 @@ export declare function signInWithPhoneNumber(auth: Auth, phoneNumber: string, a
897901
| auth | [Auth](./auth.auth.md#auth_interface) | The [Auth](./auth.auth.md#auth_interface) instance. |
898902
| phoneNumber | string | The user's phone number in E.164 format (e.g. +16505550101). |
899903
| appVerifier | [ApplicationVerifier](./auth.applicationverifier.md#applicationverifier_interface) | The [ApplicationVerifier](./auth.applicationverifier.md#applicationverifier_interface)<!-- -->. |
904+
| useWebOTP | boolean | Specifies whether to use WebOTP autofill or not in the sign in |
905+
| webOTPTimeout | number | Error would be thrown if WebOTP autofill is used and does not resolve within this specified timeout parameter (milliseconds). |
900906

901907
<b>Returns:</b>
902908

903-
Promise&lt;[ConfirmationResult](./auth.confirmationresult.md#confirmationresult_interface)<!-- -->&gt;
909+
Promise&lt;[ConfirmationResult](./auth.confirmationresult.md#confirmationresult_interface) \| [UserCredential](./auth.usercredential.md#usercredential_interface)<!-- -->&gt;
904910

905911
### Example
906912

907913

908914
```javascript
909915
// 'recaptcha-container' is the ID of an element in the DOM.
910916
const applicationVerifier = new firebase.auth.RecaptchaVerifier('recaptcha-container');
911-
const confirmationResult = await signInWithPhoneNumber(auth, phoneNumber, applicationVerifier);
917+
const credential = await signInWithPhoneNumber(auth, phoneNumber, applicationVerifier, true);
918+
919+
const applicationVerifier = new firebase.auth.RecaptchaVerifier('recaptcha-container');
920+
const confirmationResult = await signInWithPhoneNumber(auth, phoneNumber, applicationVerifier, false);
912921
// Obtain a verificationCode from the user.
913922
const credential = await confirmationResult.confirm(verificationCode);
914923

packages/auth-compat/src/auth.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import { _isPopupRedirectSupported } from './platform';
3131
import { CompatPopupRedirectResolver } from './popup_redirect';
3232
import { User } from './user';
3333
import {
34-
convertConfirmationResult,
34+
convertPhoneSignInPromiseResult,
3535
convertCredential
3636
} from './user_credential';
3737
import { ReverseWrapper, Wrapper } from './wrap';
@@ -280,14 +280,18 @@ export class Auth
280280
}
281281
signInWithPhoneNumber(
282282
phoneNumber: string,
283-
applicationVerifier: compat.ApplicationVerifier
284-
): Promise<compat.ConfirmationResult> {
285-
return convertConfirmationResult(
283+
applicationVerifier: compat.ApplicationVerifier,
284+
useWebOTP = false,
285+
webOTPTimeout = 30
286+
): Promise<compat.ConfirmationResult | compat.UserCredential> {
287+
return convertPhoneSignInPromiseResult(
286288
this._delegate,
287289
exp.signInWithPhoneNumber(
288290
this._delegate,
289291
phoneNumber,
290-
applicationVerifier
292+
applicationVerifier,
293+
useWebOTP,
294+
webOTPTimeout
291295
)
292296
);
293297
}

packages/auth-compat/src/user_credential.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,38 @@ export async function convertConfirmationResult(
178178
};
179179
}
180180

181+
export async function convertPhoneSignInPromiseResult(
182+
auth: exp.Auth,
183+
phoneSignInPromise: Promise<exp.ConfirmationResult | exp.UserCredential>
184+
): Promise<compat.ConfirmationResult | compat.UserCredential> {
185+
const result = await phoneSignInPromise;
186+
const resultAsConfirmationResult = result as exp.ConfirmationResult;
187+
if (resultAsConfirmationResult) {
188+
return {
189+
verificationId: resultAsConfirmationResult.verificationId,
190+
confirm: (verificationCode: string) =>
191+
convertCredential(
192+
auth,
193+
resultAsConfirmationResult.confirm(verificationCode)
194+
)
195+
};
196+
} else {
197+
const resultAsUserCredential = result as exp.UserCredential;
198+
const operationType = resultAsUserCredential.operationType;
199+
const user = resultAsUserCredential.user;
200+
return {
201+
operationType,
202+
credential: credentialFromResponse(
203+
resultAsUserCredential as exp.UserCredentialInternal
204+
),
205+
additionalUserInfo: exp.getAdditionalUserInfo(
206+
resultAsUserCredential as exp.UserCredential
207+
),
208+
user: User.getOrCreate(user)
209+
};
210+
}
211+
}
212+
181213
class MultiFactorResolver implements compat.MultiFactorResolver {
182214
readonly auth: Auth;
183215
constructor(

0 commit comments

Comments
 (0)