Skip to content

Commit

Permalink
feat: introduced autoOTPVerify for Android devices
Browse files Browse the repository at this point in the history
  • Loading branch information
Efstathios Ntonas committed Nov 19, 2024
1 parent c8b7909 commit 587cab8
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -981,7 +981,7 @@ private void signInWithProvider(String appName, ReadableMap provider, final Prom
*/
@ReactMethod
public void signInWithPhoneNumber(
String appName, final String phoneNumber, final boolean forceResend, final Promise promise) {
String appName, final String phoneNumber, final boolean forceResend, final Promise promise, final boolean autoOTPVerify) {
Log.d(TAG, "signInWithPhoneNumber");
FirebaseApp firebaseApp = FirebaseApp.getInstance(appName);
final FirebaseAuth firebaseAuth = FirebaseAuth.getInstance(firebaseApp);
Expand Down Expand Up @@ -1080,10 +1080,10 @@ public void onCodeAutoRetrievalTimeOut(String verificationId) {
if (forceResend && mForceResendingToken != null) {
PhoneAuthProvider.getInstance(firebaseAuth)
.verifyPhoneNumber(
phoneNumber, 60, TimeUnit.SECONDS, activity, callbacks, mForceResendingToken);
phoneNumber, autoOTPVerify ? 60 : 0, TimeUnit.SECONDS, activity, callbacks, mForceResendingToken);
} else {
PhoneAuthProvider.getInstance(firebaseAuth)
.verifyPhoneNumber(phoneNumber, 60, TimeUnit.SECONDS, activity, callbacks);
.verifyPhoneNumber(phoneNumber, autoOTPVerify ? 60 : 0, TimeUnit.SECONDS, activity, callbacks);
}
}
}
Expand Down Expand Up @@ -1113,7 +1113,7 @@ public void getSession(final String appName, final Promise promise) {

@ReactMethod
public void verifyPhoneNumberWithMultiFactorInfo(
final String appName, final String hintUid, final String sessionKey, final Promise promise) {
final String appName, final String hintUid, final String sessionKey, final Promise promise, final boolean autoOTPVerify) {
final MultiFactorResolver resolver = mCachedResolvers.get(sessionKey);
if (resolver == null) {
// See https://firebase.google.com/docs/reference/node/firebase.auth.multifactorresolver for
Expand Down Expand Up @@ -1153,7 +1153,7 @@ public void verifyPhoneNumberWithMultiFactorInfo(
PhoneAuthOptions.newBuilder(firebaseAuth)
.setActivity(activity)
.setMultiFactorHint((PhoneMultiFactorInfo) selectedHint)
.setTimeout(30L, TimeUnit.SECONDS)
.setTimeout(autoOTPVerify ? 30L : 0L, TimeUnit.SECONDS)
.setMultiFactorSession(resolver.getSession())
.setCallbacks(
new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
Expand Down Expand Up @@ -1185,7 +1185,8 @@ public void verifyPhoneNumberForMultiFactor(
final String appName,
final String phoneNumber,
final String sessionKey,
final Promise promise) {
final Promise promise,
final boolean autoOTPVerify) {
final MultiFactorSession multiFactorSession = mMultiFactorSessions.get(sessionKey);
if (multiFactorSession == null) {
rejectPromiseWithCodeAndMessage(
Expand All @@ -1198,7 +1199,7 @@ public void verifyPhoneNumberForMultiFactor(
PhoneAuthOptions.newBuilder(firebaseAuth)
.setPhoneNumber(phoneNumber)
.setActivity(getCurrentActivity())
.setTimeout(30L, TimeUnit.SECONDS)
.setTimeout(autoOTPVerify ? 30L : 0L, TimeUnit.SECONDS)
.setMultiFactorSession(multiFactorSession)
.requireSmsValidation(true)
.setCallbacks(
Expand Down
5 changes: 4 additions & 1 deletion packages/auth/lib/PhoneAuthListener.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@ import NativeFirebaseError from '@react-native-firebase/app/lib/internal/NativeF
let REQUEST_ID = 0;

export default class PhoneAuthListener {
constructor(auth, phoneNumber, timeout, forceResend) {
constructor(auth, phoneNumber, timeout, forceResend, autoOTPVerify) {

Check warning on line 24 in packages/auth/lib/PhoneAuthListener.js

View check run for this annotation

Codecov / codecov/patch

packages/auth/lib/PhoneAuthListener.js#L24

Added line #L24 was not covered by tests
this._auth = auth;
this._reject = null;
this._resolve = null;
this._promise = null;
this._jsStack = new Error().stack;
this._autoOTPVerify = autoOTPVerify

Check failure on line 30 in packages/auth/lib/PhoneAuthListener.js

View workflow job for this annotation

GitHub Actions / Lint

Insert `;`

Check failure on line 30 in packages/auth/lib/PhoneAuthListener.js

View workflow job for this annotation

GitHub Actions / Lint

Insert `;`

Check warning on line 30 in packages/auth/lib/PhoneAuthListener.js

View check run for this annotation

Codecov / codecov/patch

packages/auth/lib/PhoneAuthListener.js#L30

Added line #L30 was not covered by tests

this._timeout = timeout || 20;
this._phoneAuthRequestId = REQUEST_ID++;
Expand All @@ -50,11 +51,13 @@ export default class PhoneAuthListener {
this._subscribeToEvents();

if (isAndroid) {
this._auth.

Check failure on line 54 in packages/auth/lib/PhoneAuthListener.js

View workflow job for this annotation

GitHub Actions / Lint

Delete `⏎······`

Check failure on line 54 in packages/auth/lib/PhoneAuthListener.js

View workflow job for this annotation

GitHub Actions / Lint

Delete `⏎······`

Check warning on line 54 in packages/auth/lib/PhoneAuthListener.js

View check run for this annotation

Codecov / codecov/patch

packages/auth/lib/PhoneAuthListener.js#L54

Added line #L54 was not covered by tests
this._auth.native.verifyPhoneNumber(
phoneNumber,
this._phoneAuthRequestId + '',
this._timeout,
this._forceResending,
this._autoOTPVerify

Check failure on line 60 in packages/auth/lib/PhoneAuthListener.js

View workflow job for this annotation

GitHub Actions / Lint

Insert `,`

Check failure on line 60 in packages/auth/lib/PhoneAuthListener.js

View workflow job for this annotation

GitHub Actions / Lint

Insert `,`
);
}

Expand Down
9 changes: 9 additions & 0 deletions packages/auth/lib/Settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,21 @@ export default class Settings {
this._auth = auth;
this._forceRecaptchaFlowForTesting = false;
this._appVerificationDisabledForTesting = false;
this._autoOTPVerify = true; /* Android only */

Check warning on line 25 in packages/auth/lib/Settings.js

View check run for this annotation

Codecov / codecov/patch

packages/auth/lib/Settings.js#L25

Added line #L25 was not covered by tests
}

get forceRecaptchaFlowForTesting() {
return this._forceRecaptchaFlowForTesting;
}

get autoOTPVerify() {
return this._autoOTPVerify;

Check warning on line 33 in packages/auth/lib/Settings.js

View check run for this annotation

Codecov / codecov/patch

packages/auth/lib/Settings.js#L32-L33

Added lines #L32 - L33 were not covered by tests
}

set autoOTPVerify(autoOTPVerify) {
this._autoOTPVerify = autoOTPVerify;

Check warning on line 37 in packages/auth/lib/Settings.js

View check run for this annotation

Codecov / codecov/patch

packages/auth/lib/Settings.js#L36-L37

Added lines #L36 - L37 were not covered by tests
}

set forceRecaptchaFlowForTesting(forceRecaptchaFlow) {
if (isAndroid) {
this._forceRecaptchaFlowForTesting = forceRecaptchaFlow;
Expand Down
27 changes: 27 additions & 0 deletions packages/auth/lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1116,6 +1116,33 @@ export namespace FirebaseAuthTypes {
* @param smsCode The pre-set SMS code.
*/
setAutoRetrievedSmsCodeForPhoneNumber(phoneNumber: string, smsCode: string): Promise<null>;

/**
* Flag to disable automatic retrieval of SMS codes for the given phone number.
* When receiving a verification code Android automagically digests the OTP code,
* in some cases this can cause issues like "OTP already used".
* Use it only if absolutely necessary.
*
* @android
*/
autoOTPVerify: boolean;

/**
* Sets whether the automatic OTP (One-Time Password) verification should be disabled on Android devices.
*
* This method allows you to control the behavior of OTP verification by disabling the automatic retrieval
* and verification of SMS codes. This can be useful in testing scenarios or when you want to handle OTP
* verification manually if your users encounter "OTP already used" errors on some devices.
*
* @example
* ```js
* await firebase.auth().settings.setAutoOTPVerify(false);
* ```
*
* @android
* @param status whether auto OTP verify should be disabled, defaults to false
*/
setAutoOTPVerify(status: boolean): Promise<null>;
}

/**
Expand Down
5 changes: 3 additions & 2 deletions packages/auth/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ class FirebaseAuthModule extends FirebaseModule {
signInWithPhoneNumber(phoneNumber, forceResend) {
if (isAndroid) {
return this.native
.signInWithPhoneNumber(phoneNumber, forceResend || false)
.signInWithPhoneNumber(phoneNumber, forceResend || false, this._settings.autoOTPVerify)

Check warning on line 285 in packages/auth/lib/index.js

View check run for this annotation

Codecov / codecov/patch

packages/auth/lib/index.js#L285

Added line #L285 was not covered by tests
.then(result => new ConfirmationResult(this, result.verificationId));
}

Expand All @@ -294,14 +294,15 @@ class FirebaseAuthModule extends FirebaseModule {
verifyPhoneNumber(phoneNumber, autoVerifyTimeoutOrForceResend, forceResend) {
let _forceResend = forceResend;
let _autoVerifyTimeout = 60;
let _autoOTPVerify = this._settings.autoOTPVerify

Check failure on line 297 in packages/auth/lib/index.js

View workflow job for this annotation

GitHub Actions / Lint

Insert `;`

Check failure on line 297 in packages/auth/lib/index.js

View workflow job for this annotation

GitHub Actions / Lint

Insert `;`

Check warning on line 297 in packages/auth/lib/index.js

View check run for this annotation

Codecov / codecov/patch

packages/auth/lib/index.js#L297

Added line #L297 was not covered by tests

if (isBoolean(autoVerifyTimeoutOrForceResend)) {
_forceResend = autoVerifyTimeoutOrForceResend;
} else {
_autoVerifyTimeout = autoVerifyTimeoutOrForceResend;
}

return new PhoneAuthListener(this, phoneNumber, _autoVerifyTimeout, _forceResend);
return new PhoneAuthListener(this, phoneNumber, _autoVerifyTimeout, _forceResend, _autoOTPVerify);

Check failure on line 305 in packages/auth/lib/index.js

View workflow job for this annotation

GitHub Actions / Lint

Replace `this,·phoneNumber,·_autoVerifyTimeout,·_forceResend,·_autoOTPVerify` with `⏎······this,⏎······phoneNumber,⏎······_autoVerifyTimeout,⏎······_forceResend,⏎······_autoOTPVerify,⏎····`

Check failure on line 305 in packages/auth/lib/index.js

View workflow job for this annotation

GitHub Actions / Lint

Replace `this,·phoneNumber,·_autoVerifyTimeout,·_forceResend,·_autoOTPVerify` with `⏎······this,⏎······phoneNumber,⏎······_autoVerifyTimeout,⏎······_forceResend,⏎······_autoOTPVerify,⏎····`

Check warning on line 305 in packages/auth/lib/index.js

View check run for this annotation

Codecov / codecov/patch

packages/auth/lib/index.js#L305

Added line #L305 was not covered by tests
}

verifyPhoneNumberWithMultiFactorInfo(multiFactorHint, session) {
Expand Down

0 comments on commit 587cab8

Please sign in to comment.