Skip to content

Commit 95d014c

Browse files
billykwokmikehardy
andauthored
feat(auth, android): add forceRecaptchaFlowForTesting API (#7148)
Co-authored-by: Mike Hardy <[email protected]>
1 parent 8da6951 commit 95d014c

File tree

4 files changed

+51
-0
lines changed

4 files changed

+51
-0
lines changed

docs/auth/phone-auth.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ For reliable automated testing, you may want to disable both automatic and fallb
2929

3030
Ensure that all parts of step 1 and 2 from [the official firebase Android phone auth docs](https://firebase.google.com/docs/auth/android/phone-auth#enable-phone-number-sign-in-for-your-firebase-project) have been followed.
3131

32+
To bypass Play Integrity for manual testing, you may [force reCAPTCHA to be used](https://rnfirebase.io/reference/auth/authsettings#appVerificationDisabledForTesting) prior to calling [`verifyPhoneNumber`](https://rnfirebase.io/reference/auth/phoneauthprovider#verifyPhoneNumber).
33+
3234
# Expo Setup
3335

3436
To use phone auth in an expo app, add the `@react-native-firebase/auth` config plug-in to the [`plugins`](https://docs.expo.io/versions/latest/config/app/#plugins) section of your `app.json`. This is in addition to the `@react-native-firebase/app` plugin.

packages/auth/android/src/main/java/io/invertase/firebase/auth/ReactNativeFirebaseAuthModule.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,29 @@ public void removeIdTokenListener(String appName) {
246246
}
247247
}
248248

249+
/**
250+
* Forces application verification to use the web reCAPTCHA flow for Phone Authentication.
251+
*
252+
* <p>Once this has been called, every call to PhoneAuthProvider#verifyPhoneNumber() will skip the
253+
* Play Integrity API verification flow and use the reCAPTCHA flow instead.
254+
*
255+
* <p>Calling this method a second time will overwrite the previously passed parameter.
256+
*
257+
* @param appName
258+
* @param forceRecaptchaFlow
259+
* @param promise
260+
*/
261+
@ReactMethod
262+
public void forceRecaptchaFlowForTesting(
263+
String appName, boolean forceRecaptchaFlow, Promise promise) {
264+
Log.d(TAG, "forceRecaptchaFlowForTesting");
265+
FirebaseApp firebaseApp = FirebaseApp.getInstance(appName);
266+
FirebaseAuth firebaseAuth = FirebaseAuth.getInstance(firebaseApp);
267+
FirebaseAuthSettings firebaseAuthSettings = firebaseAuth.getFirebaseAuthSettings();
268+
firebaseAuthSettings.forceRecaptchaFlowForTesting(forceRecaptchaFlow);
269+
promise.resolve(null);
270+
}
271+
249272
/**
250273
* The phone number and SMS code here must have been configured in the Firebase Console
251274
* (Authentication > Sign In Method > Phone).

packages/auth/lib/Settings.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,21 @@ import { isAndroid } from '@react-native-firebase/app/lib/common';
2020
export default class Settings {
2121
constructor(auth) {
2222
this._auth = auth;
23+
this._forceRecaptchaFlowForTesting = false;
2324
this._appVerificationDisabledForTesting = false;
2425
}
2526

27+
get forceRecaptchaFlowForTesting() {
28+
return this._forceRecaptchaFlowForTesting;
29+
}
30+
31+
set forceRecaptchaFlowForTesting(forceRecaptchaFlow) {
32+
if (isAndroid) {
33+
this._forceRecaptchaFlowForTesting = forceRecaptchaFlow;
34+
this._auth.native.forceRecaptchaFlowForTesting(forceRecaptchaFlow);
35+
}
36+
}
37+
2638
get appVerificationDisabledForTesting() {
2739
return this._appVerificationDisabledForTesting;
2840
}

packages/auth/lib/index.d.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,6 +1020,20 @@ export namespace FirebaseAuthTypes {
10201020
* ```
10211021
*/
10221022
export interface AuthSettings {
1023+
/**
1024+
* Forces application verification to use the web reCAPTCHA flow for Phone Authentication.
1025+
*
1026+
* Once this has been called, every call to PhoneAuthProvider#verifyPhoneNumber() will skip the Play Integrity API verification flow and use the reCAPTCHA flow instead.
1027+
*
1028+
* <p>Calling this method a second time will overwrite the previously passed parameter.
1029+
*
1030+
* @android
1031+
* @param appName
1032+
* @param forceRecaptchaFlow
1033+
* @param promise
1034+
*/
1035+
forceRecaptchaFlowForTesting: boolean;
1036+
10231037
/**
10241038
* Flag to disable app verification for the purpose of testing phone authentication. For this property to take effect, it needs to be set before rendering a reCAPTCHA app verifier. When this is disabled, a mock reCAPTCHA is rendered instead. This is useful for manual testing during development or for automated integration tests.
10251039
*

0 commit comments

Comments
 (0)