diff --git a/.changeset/fluffy-cooks-behave.md b/.changeset/fluffy-cooks-behave.md new file mode 100644 index 00000000..3025b368 --- /dev/null +++ b/.changeset/fluffy-cooks-behave.md @@ -0,0 +1,5 @@ +--- +'@capacitor-firebase/authentication': minor +--- + +feat(android): add `timeout` option to `signInWithPhoneNumber(…)` diff --git a/packages/authentication/README.md b/packages/authentication/README.md index bf7d4511..6dc5a5e1 100644 --- a/packages/authentication/README.md +++ b/packages/authentication/README.md @@ -1667,6 +1667,7 @@ Remove all listeners for this plugin. | **`phoneNumber`** | string | The phone number to be verified in E.164 format. | | 0.1.0 | | **`recaptchaVerifier`** | unknown | The reCAPTCHA verifier. Must be an instance of `firebase.auth.RecaptchaVerifier`. Only available for Web. | | 5.2.0 | | **`resendCode`** | boolean | Resend the verification code to the specified phone number. `signInWithPhoneNumber` must be called once before using this option. Only available for Android. | false | 1.3.0 | +| **`timeout`** | number | The maximum amount of time in seconds to wait for the SMS auto-retrieval. Use 0 to disable SMS-auto-retrieval. Only available for Android. | 60 | 5.4.0 | #### SendPasswordResetEmailOptions diff --git a/packages/authentication/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/authentication/FirebaseAuthenticationPlugin.java b/packages/authentication/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/authentication/FirebaseAuthenticationPlugin.java index c55e5078..07d4dce3 100644 --- a/packages/authentication/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/authentication/FirebaseAuthenticationPlugin.java +++ b/packages/authentication/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/authentication/FirebaseAuthenticationPlugin.java @@ -334,7 +334,8 @@ public void linkWithPhoneNumber(PluginCall call) { return; } boolean resendCode = call.getBoolean("resendCode", false); - LinkWithPhoneNumberOptions options = new LinkWithPhoneNumberOptions(phoneNumber, resendCode); + Long timeout = call.getLong("timeout", 60L); + LinkWithPhoneNumberOptions options = new LinkWithPhoneNumberOptions(phoneNumber, resendCode, timeout); implementation.linkWithPhoneNumber(options); call.resolve(); @@ -617,7 +618,8 @@ public void signInWithPhoneNumber(PluginCall call) { return; } boolean resendCode = call.getBoolean("resendCode", false); - SignInWithPhoneNumberOptions options = new SignInWithPhoneNumberOptions(skipNativeAuth, phoneNumber, resendCode); + Long timeout = call.getLong("timeout", 60L); + SignInWithPhoneNumberOptions options = new SignInWithPhoneNumberOptions(skipNativeAuth, phoneNumber, resendCode, timeout); implementation.signInWithPhoneNumber(options); call.resolve(); diff --git a/packages/authentication/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/authentication/classes/LinkWithPhoneNumberOptions.java b/packages/authentication/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/authentication/classes/LinkWithPhoneNumberOptions.java index a4ff12c0..bbb89d24 100644 --- a/packages/authentication/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/authentication/classes/LinkWithPhoneNumberOptions.java +++ b/packages/authentication/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/authentication/classes/LinkWithPhoneNumberOptions.java @@ -1,8 +1,10 @@ package io.capawesome.capacitorjs.plugins.firebase.authentication.classes; +import androidx.annotation.NonNull; + public class LinkWithPhoneNumberOptions extends SignInWithPhoneNumberOptions { - public LinkWithPhoneNumberOptions(String phoneNumber, boolean resendCode) { - super(false, phoneNumber, resendCode); + public LinkWithPhoneNumberOptions(String phoneNumber, boolean resendCode, @NonNull Long timeout) { + super(false, phoneNumber, resendCode, timeout); } } diff --git a/packages/authentication/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/authentication/classes/SignInWithPhoneNumberOptions.java b/packages/authentication/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/authentication/classes/SignInWithPhoneNumberOptions.java index 38d79c4f..c2f0be54 100644 --- a/packages/authentication/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/authentication/classes/SignInWithPhoneNumberOptions.java +++ b/packages/authentication/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/authentication/classes/SignInWithPhoneNumberOptions.java @@ -1,14 +1,21 @@ package io.capawesome.capacitorjs.plugins.firebase.authentication.classes; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; + public class SignInWithPhoneNumberOptions extends SignInOptions { private String phoneNumber; private boolean resendCode; - public SignInWithPhoneNumberOptions(boolean skipNativeAuth, String phoneNumber, boolean resendCode) { + @NonNull + private Long timeout; + + public SignInWithPhoneNumberOptions(boolean skipNativeAuth, String phoneNumber, boolean resendCode, @NonNull Long timeout) { super(skipNativeAuth); this.phoneNumber = phoneNumber; this.resendCode = resendCode; + this.timeout = timeout; } public String getPhoneNumber() { @@ -18,4 +25,9 @@ public String getPhoneNumber() { public boolean getResendCode() { return resendCode; } + + @NonNull + public Long getTimeout() { + return timeout; + } } diff --git a/packages/authentication/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/authentication/handlers/PhoneAuthProviderHandler.java b/packages/authentication/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/authentication/handlers/PhoneAuthProviderHandler.java index 7803da6e..c31d7953 100644 --- a/packages/authentication/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/authentication/handlers/PhoneAuthProviderHandler.java +++ b/packages/authentication/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/authentication/handlers/PhoneAuthProviderHandler.java @@ -57,7 +57,7 @@ private void verifyPhoneNumber(@NonNull SignInWithPhoneNumberOptions options, bo PhoneAuthOptions.Builder builder = PhoneAuthOptions .newBuilder(pluginImplementation.getFirebaseAuthInstance()) .setPhoneNumber(options.getPhoneNumber()) - .setTimeout(60L, TimeUnit.SECONDS) + .setTimeout(options.getTimeout(), TimeUnit.SECONDS) .setActivity(pluginImplementation.getPlugin().getActivity()) .setCallbacks(createCallbacks(options, isLink)); if (options.getResendCode()) { diff --git a/packages/authentication/docs/firebase-js-sdk.md b/packages/authentication/docs/firebase-js-sdk.md index 96dbbfa3..11f928dc 100644 --- a/packages/authentication/docs/firebase-js-sdk.md +++ b/packages/authentication/docs/firebase-js-sdk.md @@ -104,6 +104,7 @@ const signInWithPhoneNumber = async () => { // 1. Start phone number verification await FirebaseAuthentication.signInWithPhoneNumber({ phoneNumber: '123456789', + timeout: 0, // Disable SMS auto-retrieval }); }); }; @@ -174,6 +175,5 @@ When using the Firebase JS SDK on Android and iOS, you must be aware of the foll - **Apple Sign-In**: Works on Android and iOS only with `skipNativeAuth=true` (see [here](https://github.com/robingenz/capacitor-firebase-authentication/issues/41#issuecomment-884106449)). - **Microsoft Sign-In**: Not supported (see https://github.com/capawesome-team/capacitor-firebase/discussions/216#discussioncomment-3803525) - **Twitter Sign-In**: Works on iOS only with `skipNativeAuth=false` (see [here](https://github.com/robingenz/capacitor-firebase-authentication/issues/93#issuecomment-939459594)). -- **Phone Number Sign-In**: To create the `PhoneAuthCredential` in the Firebase JS SDK, the `verificationId` and the `verificationCode` are required. However, on Android, it may happen that no `verificationCode` is provided (see [`addListener('phoneVerificationCompleted', ...)`](https://github.com/capawesome-team/capacitor-firebase/tree/main/packages/authentication#addlistenerphoneverificationcompleted-)). In this case, the user cannot be additionally signed in to the Firebase JS SDK. Unfortunately, this behavior cannot be disabled on Android (see [firebase/quickstart-android#296](https://github.com/firebase/quickstart-android/issues/296)). **Note**: The [`skipNativeAuth`](https://github.com/capawesome-team/capacitor-firebase/blob/main/packages/authentication/README.md#configuration) configuration option can be overwritten for each plugin call individually (see `skipNativeAuth` parameter in [SignInOptions](https://github.com/capawesome-team/capacitor-firebase/blob/main/packages/authentication/README.md#signinoptions)). diff --git a/packages/authentication/src/definitions.ts b/packages/authentication/src/definitions.ts index aed1c3e2..8b139174 100644 --- a/packages/authentication/src/definitions.ts +++ b/packages/authentication/src/definitions.ts @@ -877,6 +877,18 @@ export interface SignInWithPhoneNumberOptions extends SignInOptions { * @default false */ resendCode?: boolean; + /** + * The maximum amount of time in seconds to wait for the SMS auto-retrieval. + * + * Use 0 to disable SMS-auto-retrieval. + * + * Only available for Android. + * + * @since 5.4.0 + * @default 60 + * @see https://firebase.google.com/docs/reference/android/com/google/firebase/auth/PhoneAuthOptions.Builder#setTimeout(java.lang.Long,java.util.concurrent.TimeUnit) + */ + timeout?: number; } /**