From e5e6077ac4e15a2d7c3959ad26c8c8a2bbc15b3e Mon Sep 17 00:00:00 2001 From: Robin Genz Date: Sat, 3 Dec 2022 16:49:50 +0100 Subject: [PATCH 1/5] feat(authentication): provide the auto-retrieved SMS verification code --- .../FirebaseAuthentication.java | 12 +++ .../FirebaseAuthenticationPlugin.java | 22 +++++ .../handlers/PhoneAuthProviderHandler.java | 13 +++ packages/authentication/src/definitions.ts | 83 ++++++++++++++++++- 4 files changed, 128 insertions(+), 2 deletions(-) diff --git a/packages/authentication/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/authentication/FirebaseAuthentication.java b/packages/authentication/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/authentication/FirebaseAuthentication.java index 52167af4..86510b58 100644 --- a/packages/authentication/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/authentication/FirebaseAuthentication.java +++ b/packages/authentication/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/authentication/FirebaseAuthentication.java @@ -663,6 +663,18 @@ public void handleFailedLink(final PluginCall call, String message, Exception ex call.reject(message, exception); } + public void handlePhoneVerificationCompleted(String smsCode) { + plugin.handlePhoneVerificationCompleted(smsCode); + } + + public void handlePhoneVerificationFailed(Exception exception) { + plugin.handlePhoneVerificationFailed(exception); + } + + public void handlePhoneCodeSent(String verificationId) { + plugin.handlePhoneCodeSent(verificationId); + } + public FirebaseAuth getFirebaseAuthInstance() { return firebaseAuthInstance; } 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 3058c3f2..09e11e33 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 @@ -8,6 +8,7 @@ import com.getcapacitor.PluginMethod; import com.getcapacitor.annotation.ActivityCallback; import com.getcapacitor.annotation.CapacitorPlugin; +import com.google.firebase.FirebaseException; import com.google.firebase.auth.ActionCodeSettings; import com.google.firebase.auth.FirebaseUser; import io.capawesome.capacitorjs.plugins.firebase.authentication.handlers.FacebookAuthProviderHandler; @@ -16,6 +17,9 @@ public class FirebaseAuthenticationPlugin extends Plugin { public static final String TAG = "FirebaseAuthentication"; + public static final String PHONE_VERIFICATION_COMPLETED_EVENT = "phoneVerificationCompleted"; + public static final String PHONE_VERIFICATION_FAILED_EVENT = "phoneVerificationFailed"; + public static final String PHONE_CODE_SENT_EVENT = "phoneCodeSent"; public static final String ERROR_PROVIDER_ID_MISSING = "providerId must be provided."; public static final String ERROR_NO_USER_SIGNED_IN = "No user is signed in."; public static final String ERROR_OOB_CODE_MISSING = "oobCode must be provided."; @@ -591,6 +595,24 @@ public void useEmulator(PluginCall call) { } } + public void handlePhoneVerificationCompleted(String smsCode) { + JSObject result = new JSObject(); + // TODO + notifyListeners(PHONE_VERIFICATION_COMPLETED_EVENT, result, true); + } + + public void handlePhoneVerificationFailed(Exception exception) { + JSObject result = new JSObject(); + // TODO + notifyListeners(PHONE_VERIFICATION_FAILED_EVENT, result, true); + } + + public void handlePhoneCodeSent(String verificationId) { + JSObject result = new JSObject(); + // TODO + notifyListeners(PHONE_CODE_SENT_EVENT, result, true); + } + @Override public void startActivityForResult(PluginCall call, Intent intent, String callbackName) { super.startActivityForResult(call, intent, callbackName); 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 56a71fb4..3ae488fa 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 @@ -67,6 +67,11 @@ private PhoneAuthProvider.OnVerificationStateChangedCallbacks createCallbacks(fi return new PhoneAuthProvider.OnVerificationStateChangedCallbacks() { @Override public void onVerificationCompleted(PhoneAuthCredential credential) { + String smsCode = credential.getSmsCode(); + pluginImplementation.handlePhoneVerificationCompleted(smsCode); + /** + * @deprecated This code was replaced by event listener. + */ if (isLink) { pluginImplementation.handleSuccessfulLink(call, credential, null, null, null); } else { @@ -76,6 +81,10 @@ public void onVerificationCompleted(PhoneAuthCredential credential) { @Override public void onVerificationFailed(FirebaseException exception) { + pluginImplementation.handlePhoneVerificationFailed(exception); + /** + * @deprecated This code was replaced by event listener. + */ if (isLink) { pluginImplementation.handleFailedLink(call, null, exception); } else { @@ -85,6 +94,10 @@ public void onVerificationFailed(FirebaseException exception) { @Override public void onCodeSent(@NonNull String verificationId, @NonNull PhoneAuthProvider.ForceResendingToken token) { + pluginImplementation.handlePhoneCodeSent(verificationId); + /** + * @deprecated This code was replaced by event listener. + */ JSObject result = FirebaseAuthenticationHelper.createSignInResult(null, null, null, null, null, null); result.put("verificationId", verificationId); call.resolve(result); diff --git a/packages/authentication/src/definitions.ts b/packages/authentication/src/definitions.ts index e54f4e98..a26d7e7e 100644 --- a/packages/authentication/src/definitions.ts +++ b/packages/authentication/src/definitions.ts @@ -395,6 +395,33 @@ export interface FirebaseAuthenticationPlugin { eventName: 'authStateChange', listenerFunc: AuthStateChangeListener, ): Promise & PluginListenerHandle; + /** + * Listen for a completed phone verification. + * + * @since 1.3.0 + */ + addListener( + eventName: 'phoneVerificationCompleted', + listenerFunc: PhoneVerificationCompletedListener, + ): Promise & PluginListenerHandle; + /** + * Listen for a failed phone verification. + * + * @since 1.3.0 + */ + addListener( + eventName: 'phoneVerificationFailed', + listenerFunc: PhoneVerificationFailedListener, + ): Promise & PluginListenerHandle; + /** + * Listen for a phone verification code. + * + * @since 1.3.0 + */ + addListener( + eventName: 'phoneCodeSent', + listenerFunc: PhoneCodeSentListener, + ): Promise & PluginListenerHandle; /** * Remove all listeners for this plugin. * @@ -717,19 +744,27 @@ export interface SignInCustomParameter { export interface SignInWithPhoneNumberOptions extends SignInOptions { /** * The phone number to be verified. + * + * Cannot be used in combination with `verificationId` and `verificationCode`. + * + * Use the `phoneVerificationCompleted` listener to be notified when the verification is completed. + * Use the `phoneVerificationFailed` listener to be notified when the verification is failed. + * Use the `phoneCodeSent` listener to get the verification id. * * @since 0.1.0 */ phoneNumber?: string; /** - * The verification ID which will be returned when `signInWithPhoneNumber` is called for the first time. + * The verification ID received from the `phoneCodeSent` listener. + * * The `verificationCode` must also be provided. * * @since 0.1.0 */ verificationId?: string; /** - * The verification code from the SMS message. + * The verification code either received from the `phoneCodeSent` listener or entered by the user. + * * The `verificationId` must also be provided. * * @since 0.1.0 @@ -899,6 +934,7 @@ export interface SignInWithPhoneNumberResult extends SignInResult { * The verification ID, which is needed to identify the verification code. * * @since 0.1.0 + * @deprecated Use `addListener('phoneCodeSent', ...)` instead. */ verificationId?: string; } @@ -1019,6 +1055,7 @@ export interface AdditionalUserInfo { */ username?: string; } + /** * Callback to receive the user's sign-in state change notifications. * @@ -1038,6 +1075,48 @@ export interface AuthStateChange { user: User | null; } +/** + * Callback to receive the verification code sent to the user's phone number. + * + * @since 1.3.0 + */ +export type PhoneVerificationCompletedListener = (event: { + /** + * The verification code sent to the user's phone number. + * + * @since 1.3.0 + */ + verificationCode: string; +}) => void; + +/** + * Callback to receive notifications of failed phone verification. + * + * @since 1.3.0 + */ +export type PhoneVerificationFailedListener = (event: { + /** + * The error message. + * + * @since 1.3.0 + */ + message: string; +}) => void; + +/** + * Callback to receive the verification ID. + * + * @since 1.3.0 + */ +export type PhoneCodeSentListener = (event: { + /** + * The verification ID, which is needed to identify the verification code. + * + * @since 1.3.0 + */ + verificationId: string; +}) => void; + /** * An interface that defines the required continue/state URL with optional Android and iOS * bundle identifiers. From 19b47ba477d96b5f0037098d9439de8d0a48af4f Mon Sep 17 00:00:00 2001 From: Robin Genz Date: Sat, 3 Dec 2022 16:51:28 +0100 Subject: [PATCH 2/5] wip --- .changeset/spotty-tigers-laugh.md | 5 ++ packages/authentication/README.md | 94 +++++++++++++++++++++++++++++-- 2 files changed, 94 insertions(+), 5 deletions(-) create mode 100644 .changeset/spotty-tigers-laugh.md diff --git a/.changeset/spotty-tigers-laugh.md b/.changeset/spotty-tigers-laugh.md new file mode 100644 index 00000000..ef530d5f --- /dev/null +++ b/.changeset/spotty-tigers-laugh.md @@ -0,0 +1,5 @@ +--- +"@capacitor-firebase/authentication": minor +--- + +feat(android): provide the auto-retrieved SMS verification code diff --git a/packages/authentication/README.md b/packages/authentication/README.md index 858f1c81..b2ff8865 100644 --- a/packages/authentication/README.md +++ b/packages/authentication/README.md @@ -401,6 +401,9 @@ const useEmulator = async () => { * [`useAppLanguage()`](#useapplanguage) * [`useEmulator(...)`](#useemulator) * [`addListener('authStateChange', ...)`](#addlistenerauthstatechange) +* [`addListener('phoneVerificationCompleted', ...)`](#addlistenerphoneverificationcompleted) +* [`addListener('phoneVerificationFailed', ...)`](#addlistenerphoneverificationfailed) +* [`addListener('phoneCodeSent', ...)`](#addlistenerphonecodesent) * [`removeAllListeners()`](#removealllisteners) * [Interfaces](#interfaces) * [Type Aliases](#type-aliases) @@ -1293,6 +1296,66 @@ Listen for the user's sign-in state changes. -------------------- +### addListener('phoneVerificationCompleted', ...) + +```typescript +addListener(eventName: 'phoneVerificationCompleted', listenerFunc: PhoneVerificationCompletedListener) => Promise & PluginListenerHandle +``` + +Listen for a completed phone verification. + +| Param | Type | +| ------------------ | ------------------------------------------------------------------------------------------------- | +| **`eventName`** | 'phoneVerificationCompleted' | +| **`listenerFunc`** | PhoneVerificationCompletedListener | + +**Returns:** Promise<PluginListenerHandle> & PluginListenerHandle + +**Since:** 1.3.0 + +-------------------- + + +### addListener('phoneVerificationFailed', ...) + +```typescript +addListener(eventName: 'phoneVerificationFailed', listenerFunc: PhoneVerificationFailedListener) => Promise & PluginListenerHandle +``` + +Listen for a failed phone verification. + +| Param | Type | +| ------------------ | ------------------------------------------------------------------------------------------- | +| **`eventName`** | 'phoneVerificationFailed' | +| **`listenerFunc`** | PhoneVerificationFailedListener | + +**Returns:** Promise<PluginListenerHandle> & PluginListenerHandle + +**Since:** 1.3.0 + +-------------------- + + +### addListener('phoneCodeSent', ...) + +```typescript +addListener(eventName: 'phoneCodeSent', listenerFunc: PhoneCodeSentListener) => Promise & PluginListenerHandle +``` + +Listen for a phone verification code. + +| Param | Type | +| ------------------ | ----------------------------------------------------------------------- | +| **`eventName`** | 'phoneCodeSent' | +| **`listenerFunc`** | PhoneCodeSentListener | + +**Returns:** Promise<PluginListenerHandle> & PluginListenerHandle + +**Since:** 1.3.0 + +-------------------- + + ### removeAllListeners() ```typescript @@ -1544,11 +1607,11 @@ bundle identifiers. #### SignInWithPhoneNumberOptions -| Prop | Type | Description | Since | -| ---------------------- | ------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------- | ----- | -| **`phoneNumber`** | string | The phone number to be verified. | 0.1.0 | -| **`verificationId`** | string | The verification ID which will be returned when `signInWithPhoneNumber` is called for the first time. The `verificationCode` must also be provided. | 0.1.0 | -| **`verificationCode`** | string | The verification code from the SMS message. The `verificationId` must also be provided. | 0.1.0 | +| Prop | Type | Description | Since | +| ---------------------- | ------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----- | +| **`phoneNumber`** | string | The phone number to be verified. Cannot be used in combination with `verificationId` and `verificationCode`. Use the `phoneVerificationCompleted` listener to be notified when the verification is completed. Use the `phoneVerificationFailed` listener to be notified when the verification is failed. Use the `phoneCodeSent` listener to get the verification id. | 0.1.0 | +| **`verificationId`** | string | The verification ID received from the `phoneCodeSent` listener. The `verificationCode` must also be provided. | 0.1.0 | +| **`verificationCode`** | string | The verification code either received from the `phoneCodeSent` listener or entered by the user. The `verificationId` must also be provided. | 0.1.0 | #### UnlinkResult @@ -1621,6 +1684,27 @@ Callback to receive the user's sign-in state change notifications. (change: AuthStateChange): void +#### PhoneVerificationCompletedListener + +Callback to receive the verification code sent to the user's phone number. + +(event: { verificationCode: string; }): void + + +#### PhoneVerificationFailedListener + +Callback to receive notifications of failed phone verification. + +(event: { message: string; }): void + + +#### PhoneCodeSentListener + +Callback to receive the verification ID. + +(event: { verificationId: string; }): void + + ### Enums From e8077506ce2a42f2af10373426014e2719de1f67 Mon Sep 17 00:00:00 2001 From: Robin Genz Date: Sat, 3 Dec 2022 17:59:08 +0100 Subject: [PATCH 3/5] wip --- package-lock.json | 1 + packages/authentication/README.md | 2 ++ .../authentication/FirebaseAuthenticationPlugin.java | 6 +++--- .../handlers/PhoneAuthProviderHandler.java | 6 ++++++ .../Plugin/Handlers/PhoneAuthProviderHandler.swift | 2 ++ packages/authentication/src/definitions.ts | 12 +++++++----- 6 files changed, 21 insertions(+), 8 deletions(-) diff --git a/package-lock.json b/package-lock.json index 6461fb1d..e3cb95cb 100644 --- a/package-lock.json +++ b/package-lock.json @@ -6909,6 +6909,7 @@ } }, "packages/remote-config": { + "name": "@capacitor-firebase/remote-config", "version": "1.2.0", "funding": [ { diff --git a/packages/authentication/README.md b/packages/authentication/README.md index b2ff8865..d5ef2010 100644 --- a/packages/authentication/README.md +++ b/packages/authentication/README.md @@ -1304,6 +1304,8 @@ addListener(eventName: 'phoneVerificationCompleted', listenerFunc: PhoneVerifica Listen for a completed phone verification. +Only available for Android. + | Param | Type | | ------------------ | ------------------------------------------------------------------------------------------------- | | **`eventName`** | 'phoneVerificationCompleted' | 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 09e11e33..0500eb74 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 @@ -597,19 +597,19 @@ public void useEmulator(PluginCall call) { public void handlePhoneVerificationCompleted(String smsCode) { JSObject result = new JSObject(); - // TODO + result.put("verificationCode", smsCode); notifyListeners(PHONE_VERIFICATION_COMPLETED_EVENT, result, true); } public void handlePhoneVerificationFailed(Exception exception) { JSObject result = new JSObject(); - // TODO + result.put("message", exception.getLocalizedMessage()); notifyListeners(PHONE_VERIFICATION_FAILED_EVENT, result, true); } public void handlePhoneCodeSent(String verificationId) { JSObject result = new JSObject(); - // TODO + result.put("verificationId", verificationId); notifyListeners(PHONE_CODE_SENT_EVENT, result, true); } 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 3ae488fa..f6c8f849 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 @@ -71,6 +71,8 @@ public void onVerificationCompleted(PhoneAuthCredential credential) { pluginImplementation.handlePhoneVerificationCompleted(smsCode); /** * @deprecated This code was replaced by event listener. + * + * Caution: The call must be resolved earlier. */ if (isLink) { pluginImplementation.handleSuccessfulLink(call, credential, null, null, null); @@ -84,6 +86,8 @@ public void onVerificationFailed(FirebaseException exception) { pluginImplementation.handlePhoneVerificationFailed(exception); /** * @deprecated This code was replaced by event listener. + * + * Caution: The call must be resolved earlier. */ if (isLink) { pluginImplementation.handleFailedLink(call, null, exception); @@ -97,6 +101,8 @@ public void onCodeSent(@NonNull String verificationId, @NonNull PhoneAuthProvide pluginImplementation.handlePhoneCodeSent(verificationId); /** * @deprecated This code was replaced by event listener. + * + * Caution: The call must be resolved earlier. */ JSObject result = FirebaseAuthenticationHelper.createSignInResult(null, null, null, null, null, null); result.put("verificationId", verificationId); diff --git a/packages/authentication/ios/Plugin/Handlers/PhoneAuthProviderHandler.swift b/packages/authentication/ios/Plugin/Handlers/PhoneAuthProviderHandler.swift index f5b16bc9..436f7e9b 100644 --- a/packages/authentication/ios/Plugin/Handlers/PhoneAuthProviderHandler.swift +++ b/packages/authentication/ios/Plugin/Handlers/PhoneAuthProviderHandler.swift @@ -41,6 +41,7 @@ class PhoneAuthProviderHandler: NSObject { } PhoneAuthProvider.provider() .verifyPhoneNumber(phoneNumber, uiDelegate: nil) { verificationID, error in + // TODO: `phoneVerificationFailed` listener if let error = error { if isLink == true { self.pluginImplementation.handleFailedLink(message: nil, error: error) @@ -51,6 +52,7 @@ class PhoneAuthProviderHandler: NSObject { } var result = FirebaseAuthenticationHelper.createSignInResult(credential: nil, user: nil, idToken: nil, nonce: nil, accessToken: nil, additionalUserInfo: nil) + // TODO: `phoneVerificationCompleted` listener result["verificationId"] = verificationID call.resolve(result) } diff --git a/packages/authentication/src/definitions.ts b/packages/authentication/src/definitions.ts index a26d7e7e..1ffbc68f 100644 --- a/packages/authentication/src/definitions.ts +++ b/packages/authentication/src/definitions.ts @@ -397,6 +397,8 @@ export interface FirebaseAuthenticationPlugin { ): Promise & PluginListenerHandle; /** * Listen for a completed phone verification. + * + * Only available for Android. * * @since 1.3.0 */ @@ -744,9 +746,9 @@ export interface SignInCustomParameter { export interface SignInWithPhoneNumberOptions extends SignInOptions { /** * The phone number to be verified. - * + * * Cannot be used in combination with `verificationId` and `verificationCode`. - * + * * Use the `phoneVerificationCompleted` listener to be notified when the verification is completed. * Use the `phoneVerificationFailed` listener to be notified when the verification is failed. * Use the `phoneCodeSent` listener to get the verification id. @@ -756,7 +758,7 @@ export interface SignInWithPhoneNumberOptions extends SignInOptions { phoneNumber?: string; /** * The verification ID received from the `phoneCodeSent` listener. - * + * * The `verificationCode` must also be provided. * * @since 0.1.0 @@ -764,7 +766,7 @@ export interface SignInWithPhoneNumberOptions extends SignInOptions { verificationId?: string; /** * The verification code either received from the `phoneCodeSent` listener or entered by the user. - * + * * The `verificationId` must also be provided. * * @since 0.1.0 @@ -1097,7 +1099,7 @@ export type PhoneVerificationCompletedListener = (event: { export type PhoneVerificationFailedListener = (event: { /** * The error message. - * + * * @since 1.3.0 */ message: string; From 9e3d67e71782e59f40c1ede83d5c4c63d3405187 Mon Sep 17 00:00:00 2001 From: Robin Genz Date: Sat, 3 Dec 2022 17:59:35 +0100 Subject: [PATCH 4/5] style: format --- packages/authentication/src/definitions.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/authentication/src/definitions.ts b/packages/authentication/src/definitions.ts index 1ffbc68f..b0979b79 100644 --- a/packages/authentication/src/definitions.ts +++ b/packages/authentication/src/definitions.ts @@ -397,7 +397,7 @@ export interface FirebaseAuthenticationPlugin { ): Promise & PluginListenerHandle; /** * Listen for a completed phone verification. - * + * * Only available for Android. * * @since 1.3.0 From 048a0c5b75d2404844624a907cf5538b1efa029c Mon Sep 17 00:00:00 2001 From: Robin Genz Date: Sat, 3 Dec 2022 19:50:48 +0100 Subject: [PATCH 5/5] wip --- .../ios/Plugin/FirebaseAuthentication.swift | 8 ++++++++ .../ios/Plugin/FirebaseAuthenticationPlugin.swift | 14 ++++++++++++++ .../Plugin/Handlers/PhoneAuthProviderHandler.swift | 9 +++++++-- 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/packages/authentication/ios/Plugin/FirebaseAuthentication.swift b/packages/authentication/ios/Plugin/FirebaseAuthentication.swift index 8adb89f5..70527a5b 100644 --- a/packages/authentication/ios/Plugin/FirebaseAuthentication.swift +++ b/packages/authentication/ios/Plugin/FirebaseAuthentication.swift @@ -480,6 +480,14 @@ public typealias AuthStateChangedObserver = () -> Void savedCall.reject(errorMessage, nil, error) } + func handlePhoneVerificationFailed(_ error: Error) { + plugin.handlePhoneVerificationFailed(error) + } + + func handlePhoneCodeSent(_ verificationId: String) { + plugin.handlePhoneCodeSent(verificationId) + } + func getPlugin() -> FirebaseAuthenticationPlugin { return self.plugin } diff --git a/packages/authentication/ios/Plugin/FirebaseAuthenticationPlugin.swift b/packages/authentication/ios/Plugin/FirebaseAuthenticationPlugin.swift index cd02444b..8bdd3a9f 100644 --- a/packages/authentication/ios/Plugin/FirebaseAuthenticationPlugin.swift +++ b/packages/authentication/ios/Plugin/FirebaseAuthenticationPlugin.swift @@ -34,6 +34,8 @@ public class FirebaseAuthenticationPlugin: CAPPlugin { "signInAnonymously cannot be used in combination with skipNativeAuth." public let errorDeviceUnsupported = "Device is not supported. At least iOS 13 is required." public let authStateChangeEvent = "authStateChange" + public let phoneVerificationFailedEvent = "phoneVerificationFailed" + public let phoneCodeSentEvent = "phoneCodeSent" private var implementation: FirebaseAuthentication? override public func load() { @@ -429,6 +431,18 @@ public class FirebaseAuthenticationPlugin: CAPPlugin { notifyListeners(authStateChangeEvent, data: result, retainUntilConsumed: true) } + @objc func handlePhoneVerificationFailed(_ error: Error) { + var result = JSObject() + result["message"] = error.localizedDescription + notifyListeners(phoneVerificationFailedEvent, data: result, retainUntilConsumed: true) + } + + @objc func handlePhoneCodeSent(_ verificationId: String) { + var result = JSObject() + result["verificationId"] = verificationId + notifyListeners(phoneCodeSentEvent, data: result, retainUntilConsumed: true) + } + private func firebaseAuthenticationConfig() -> FirebaseAuthenticationConfig { var config = FirebaseAuthenticationConfig() diff --git a/packages/authentication/ios/Plugin/Handlers/PhoneAuthProviderHandler.swift b/packages/authentication/ios/Plugin/Handlers/PhoneAuthProviderHandler.swift index 436f7e9b..b4d86993 100644 --- a/packages/authentication/ios/Plugin/Handlers/PhoneAuthProviderHandler.swift +++ b/packages/authentication/ios/Plugin/Handlers/PhoneAuthProviderHandler.swift @@ -41,8 +41,13 @@ class PhoneAuthProviderHandler: NSObject { } PhoneAuthProvider.provider() .verifyPhoneNumber(phoneNumber, uiDelegate: nil) { verificationID, error in - // TODO: `phoneVerificationFailed` listener + /** + * @deprecated This code was replaced by event listener. + * + * Caution: The call must be resolved earlier. + */ if let error = error { + self.pluginImplementation.handlePhoneVerificationFailed(error) if isLink == true { self.pluginImplementation.handleFailedLink(message: nil, error: error) } else { @@ -51,8 +56,8 @@ class PhoneAuthProviderHandler: NSObject { return } + self.pluginImplementation.handlePhoneCodeSent(verificationID ?? "") var result = FirebaseAuthenticationHelper.createSignInResult(credential: nil, user: nil, idToken: nil, nonce: nil, accessToken: nil, additionalUserInfo: nil) - // TODO: `phoneVerificationCompleted` listener result["verificationId"] = verificationID call.resolve(result) }