diff --git a/.changeset/soft-eagles-relate.md b/.changeset/soft-eagles-relate.md new file mode 100644 index 00000000..9bdcf15f --- /dev/null +++ b/.changeset/soft-eagles-relate.md @@ -0,0 +1,5 @@ +--- +"@capacitor-firebase/authentication": patch +--- + +fix: `getIdToken` causes app crash if no user is signed in diff --git a/packages/authentication/android/src/main/java/dev/robingenz/capacitorjs/plugins/firebase/authentication/FirebaseAuthentication.java b/packages/authentication/android/src/main/java/dev/robingenz/capacitorjs/plugins/firebase/authentication/FirebaseAuthentication.java index 9ff6f66c..07979b4e 100644 --- a/packages/authentication/android/src/main/java/dev/robingenz/capacitorjs/plugins/firebase/authentication/FirebaseAuthentication.java +++ b/packages/authentication/android/src/main/java/dev/robingenz/capacitorjs/plugins/firebase/authentication/FirebaseAuthentication.java @@ -130,6 +130,10 @@ public FirebaseUser getCurrentUser() { public void getIdToken(Boolean forceRefresh, final GetIdTokenResultCallback resultCallback) { FirebaseUser user = getCurrentUser(); + if (user == null) { + resultCallback.error(FirebaseAuthenticationPlugin.ERROR_NO_USER_SIGNED_IN); + return; + } Task tokenResultTask = user.getIdToken(forceRefresh); tokenResultTask.addOnCompleteListener( task -> { diff --git a/packages/authentication/ios/Plugin/FirebaseAuthentication.swift b/packages/authentication/ios/Plugin/FirebaseAuthentication.swift index c32c7963..0691e6f7 100644 --- a/packages/authentication/ios/Plugin/FirebaseAuthentication.swift +++ b/packages/authentication/ios/Plugin/FirebaseAuthentication.swift @@ -75,11 +75,14 @@ public typealias AuthStateChangedObserver = () -> Void return Auth.auth().currentUser } - @objc func getIdToken(_ forceRefresh: Bool, completion: @escaping (String?, Error?) -> Void) { - let user = self.getCurrentUser() - user?.getIDTokenResult(forcingRefresh: forceRefresh, completion: { result, error in + @objc func getIdToken(_ forceRefresh: Bool, completion: @escaping (String?, String?) -> Void) { + guard let user = self.getCurrentUser() else { + completion(nil, self.plugin.errorNoUserSignedIn) + return + } + user.getIDTokenResult(forcingRefresh: forceRefresh, completion: { result, error in if let error = error { - completion(nil, error) + completion(nil, error.localizedDescription) return } completion(result?.token, nil) diff --git a/packages/authentication/ios/Plugin/FirebaseAuthenticationPlugin.swift b/packages/authentication/ios/Plugin/FirebaseAuthenticationPlugin.swift index 51e5ed92..3dc42719 100644 --- a/packages/authentication/ios/Plugin/FirebaseAuthenticationPlugin.swift +++ b/packages/authentication/ios/Plugin/FirebaseAuthenticationPlugin.swift @@ -83,9 +83,9 @@ public class FirebaseAuthenticationPlugin: CAPPlugin { @objc func getIdToken(_ call: CAPPluginCall) { let forceRefresh = call.getBool("forceRefresh", false) - implementation?.getIdToken(forceRefresh, completion: { token, error in - if let error = error { - call.reject(error.localizedDescription) + implementation?.getIdToken(forceRefresh, completion: { token, errorMessage in + if let errorMessage = errorMessage { + call.reject(errorMessage) return } var result = JSObject() diff --git a/packages/authentication/src/web.ts b/packages/authentication/src/web.ts index a02ade13..d6c0418f 100644 --- a/packages/authentication/src/web.ts +++ b/packages/authentication/src/web.ts @@ -90,7 +90,10 @@ export class FirebaseAuthenticationWeb public async getIdToken(): Promise { const auth = getAuth(); - const idToken = await auth.currentUser?.getIdToken(); + if (!auth.currentUser) { + throw new Error(FirebaseAuthenticationWeb.ERROR_NO_USER_SIGNED_IN); + } + const idToken = await auth.currentUser.getIdToken(); const result: GetIdTokenResult = { token: idToken || '', };