Skip to content

fix(firebase_auth): Move communication to EventChannels (no-nullsafety) #4704

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package io.flutter.plugins.firebase.auth;

import static io.flutter.plugins.firebase.auth.FlutterFirebaseAuthPlugin.parseFirebaseUser;

import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseAuth.AuthStateListener;
import com.google.firebase.auth.FirebaseUser;
import io.flutter.plugin.common.EventChannel.EventSink;
import io.flutter.plugin.common.EventChannel.StreamHandler;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;

public class AuthStateChannelStreamHandler implements StreamHandler {

private final FirebaseAuth firebaseAuth;
private AuthStateListener authStateListener;

public AuthStateChannelStreamHandler(FirebaseAuth firebaseAuth) {
this.firebaseAuth = firebaseAuth;
}

@Override
public void onListen(Object arguments, EventSink events) {
Map<String, Object> event = new HashMap<>();
event.put(Constants.APP_NAME, firebaseAuth.getApp().getName());

final AtomicBoolean initialAuthState = new AtomicBoolean(true);

authStateListener =
auth -> {
if (initialAuthState.get()) {
initialAuthState.set(false);
return;
}

FirebaseUser user = auth.getCurrentUser();

if (user == null) {
event.put(Constants.USER, null);
} else {
event.put(Constants.USER, parseFirebaseUser(user));
}

events.success(event);
};

firebaseAuth.addAuthStateListener(authStateListener);
}

@Override
public void onCancel(Object arguments) {
if (authStateListener != null) {
firebaseAuth.removeAuthStateListener(authStateListener);
authStateListener = null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ public class Constants {
public static final String ACCESS_TOKEN = "accessToken";
public static final String CODE = "code";
public static final String RAW_NONCE = "rawNonce";
public static final String HANDLE = "handle";
public static final String EMAIL_LINK = "emailLink";
public static final String VERIFICATION_ID = "verificationId";
public static final String SMS_CODE = "smsCode";
Expand Down
Loading