Skip to content

Commit

Permalink
feat(authentication): add timeout option to `signInWithPhoneNumber(…
Browse files Browse the repository at this point in the history
……)` (#522)

* feat(authentication): add `timeout` option to `signInWithPhoneNumber(...)`

* docs [skip ci]

* docs: add changeset [skip ci]
  • Loading branch information
robingenz committed Dec 14, 2023
1 parent 07302f8 commit 07d038a
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/fluffy-cooks-behave.md
@@ -0,0 +1,5 @@
---
'@capacitor-firebase/authentication': minor
---

feat(android): add `timeout` option to `signInWithPhoneNumber(…)`
1 change: 1 addition & 0 deletions packages/authentication/README.md
Expand Up @@ -1667,6 +1667,7 @@ Remove all listeners for this plugin.
| **`phoneNumber`** | <code>string</code> | The phone number to be verified in E.164 format. | | 0.1.0 |
| **`recaptchaVerifier`** | <code>unknown</code> | The reCAPTCHA verifier. Must be an instance of `firebase.auth.RecaptchaVerifier`. Only available for Web. | | 5.2.0 |
| **`resendCode`** | <code>boolean</code> | Resend the verification code to the specified phone number. `signInWithPhoneNumber` must be called once before using this option. Only available for Android. | <code>false</code> | 1.3.0 |
| **`timeout`** | <code>number</code> | 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. | <code>60</code> | 5.4.0 |


#### SendPasswordResetEmailOptions
Expand Down
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
@@ -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);
}
}
@@ -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() {
Expand All @@ -18,4 +25,9 @@ public String getPhoneNumber() {
public boolean getResendCode() {
return resendCode;
}

@NonNull
public Long getTimeout() {
return timeout;
}
}
Expand Up @@ -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()) {
Expand Down
2 changes: 1 addition & 1 deletion packages/authentication/docs/firebase-js-sdk.md
Expand Up @@ -104,6 +104,7 @@ const signInWithPhoneNumber = async () => {
// 1. Start phone number verification
await FirebaseAuthentication.signInWithPhoneNumber({
phoneNumber: '123456789',
timeout: 0, // Disable SMS auto-retrieval
});
});
};
Expand Down Expand Up @@ -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)).
12 changes: 12 additions & 0 deletions packages/authentication/src/definitions.ts
Expand Up @@ -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;
}

/**
Expand Down

0 comments on commit 07d038a

Please sign in to comment.