Skip to content

Commit

Permalink
fix(authentication): authStateChange event is not fired right after…
Browse files Browse the repository at this point in the history
… the listener is registered (#561)

* fix(authentication): `authStateChange` event is not fired right after the listener is registered

* style: format
  • Loading branch information
robingenz committed Feb 15, 2024
1 parent 1105fcd commit f0cd1f9
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 33 deletions.
5 changes: 5 additions & 0 deletions .changeset/witty-clouds-yawn.md
@@ -0,0 +1,5 @@
---
'@capacitor-firebase/authentication': patch
---

fix: `authStateChange` event is not fired right after the listener is registered
Expand Up @@ -43,13 +43,6 @@

public class FirebaseAuthentication {

interface AuthStateChangeListener {
void onAuthStateChanged();
}

@Nullable
private AuthStateChangeListener authStateChangeListener;

private FirebaseAuthenticationPlugin plugin;
private FirebaseAuthenticationConfig config;
private FirebaseAuth firebaseAuthInstance;
Expand All @@ -68,22 +61,11 @@ public FirebaseAuthentication(FirebaseAuthenticationPlugin plugin, FirebaseAuthe
this.initAuthProviderHandlers(config);
this.firebaseAuthStateListener =
firebaseAuth -> {
if (authStateChangeListener != null) {
authStateChangeListener.onAuthStateChanged();
}
this.plugin.handleAuthStateChange();
};
firebaseAuthInstance.addAuthStateListener(this.firebaseAuthStateListener);
}

public void setAuthStateChangeListener(@Nullable AuthStateChangeListener listener) {
this.authStateChangeListener = listener;
}

@Nullable
public AuthStateChangeListener getAuthStateChangeListener() {
return authStateChangeListener;
}

public void applyActionCode(@NonNull String oobCode, @NonNull Runnable callback) {
firebaseAuthInstance
.applyActionCode(oobCode)
Expand Down
Expand Up @@ -64,7 +64,6 @@ public class FirebaseAuthenticationPlugin extends Plugin {
public void load() {
config = getFirebaseAuthenticationConfig();
implementation = new FirebaseAuthentication(this, config);
implementation.setAuthStateChangeListener(this::updateAuthState);
}

@PluginMethod
Expand Down Expand Up @@ -808,6 +807,14 @@ public void startActivityForResult(PluginCall call, Intent intent, String callba
super.startActivityForResult(call, intent, callbackName);
}

public void handleAuthStateChange() {
FirebaseUser user = implementation.getCurrentUser();
JSObject userResult = FirebaseAuthenticationHelper.createUserResult(user);
JSObject result = new JSObject();
result.put("user", (userResult == null ? JSONObject.NULL : userResult));
notifyListeners(AUTH_STATE_CHANGE_EVENT, result, true);
}

@Override
protected void handleOnActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.handleOnActivityResult(requestCode, resultCode, data);
Expand All @@ -817,14 +824,6 @@ protected void handleOnActivityResult(int requestCode, int resultCode, @Nullable
implementation.handleOnActivityResult(requestCode, resultCode, data);
}

private void updateAuthState() {
FirebaseUser user = implementation.getCurrentUser();
JSObject userResult = FirebaseAuthenticationHelper.createUserResult(user);
JSObject result = new JSObject();
result.put("user", (userResult == null ? JSONObject.NULL : userResult));
notifyListeners(AUTH_STATE_CHANGE_EVENT, result, true);
}

@ActivityCallback
private void handleGoogleAuthProviderSignInActivityResult(@Nullable PluginCall call, @Nullable ActivityResult result) {
if (call == null || result == null) {
Expand Down
Expand Up @@ -7,7 +7,6 @@ public typealias AuthStateChangedObserver = () -> Void

// swiftlint:disable type_body_length
@objc public class FirebaseAuthentication: NSObject {
public var authStateObserver: AuthStateChangedObserver?
private let plugin: FirebaseAuthenticationPlugin
private let config: FirebaseAuthenticationConfig
private var appleAuthProviderHandler: AppleAuthProviderHandler?
Expand All @@ -27,7 +26,7 @@ public typealias AuthStateChangedObserver = () -> Void
}
self.initAuthProviderHandlers(config: config)
Auth.auth().addStateDidChangeListener {_, _ in
self.authStateObserver?()
self.plugin.handleAuthStateChange()
}
}

Expand Down
Expand Up @@ -41,9 +41,6 @@ public class FirebaseAuthenticationPlugin: CAPPlugin {

override public func load() {
self.implementation = FirebaseAuthentication(plugin: self, config: firebaseAuthenticationConfig())
self.implementation?.authStateObserver = { [weak self] in
self?.handleAuthStateChange()
}
}

@objc func applyActionCode(_ call: CAPPluginCall) {
Expand Down

0 comments on commit f0cd1f9

Please sign in to comment.