问题
I am developing a Chat library in which I want to show the conversations of the logged in user. The reason to make the library is that I want to integrate it in multiple projects.
The problem I am facing right now is that FirebaseAuth
is indicating that the user is not logged-in (FirebaseAuth.getInstance(mFirebaseApp).getCurrentUser()
always returning null
) even if the user is authenticated inside the app.
How the Module/Library can know that the user is authenticated?
I am trying the following code:
mAPIKey = getArguments().getString(ConversationModuleConstants.API_KEY_KEY);
mApplicatoinId = getArguments().getString(ConversationModuleConstants.APPLICATION_ID_KEY);
mDatabaseUrl = getArguments().getString(ConversationModuleConstants.DATABASE_URL_KEY);
FirebaseOptions options = new FirebaseOptions.Builder()
.setApiKey(mAPIKey)
.setApplicationId(mApplicatoinId)
.setDatabaseUrl(mDatabaseUrl)
.build();
boolean hasBeenInitialized = false;
List<FirebaseApp> firebaseApps = FirebaseApp.getApps(getActivity());
for (FirebaseApp app : firebaseApps) {
if (app.getName().equals(FIREBASE_APP_NAME)) {
hasBeenInitialized = true;
mFirebaseApp = app;
}
}
if (!hasBeenInitialized) {
mFirebaseApp = FirebaseApp.initializeApp(getActivity(), options, FIREBASE_APP_NAME);
}
if (FirebaseAuth.getInstance(mFirebaseApp).getCurrentUser() == null) {
Toast.makeText(getActivity(), "Login Required!", Toast.LENGTH_SHORT).show();
return;
}
回答1:
You need to add a listener/Observer to get the userid for every auth state change.
FirebaseAuth.getInstance().addAuthStateListener(firebaseAuth -> {
if (firebaseAuth.getCurrentUser() == null) {
your code goes here
}
});
https://firebase.google.com/docs/auth/android/manage-users
Another method wkile login get the user id and store in LocalStorage, while logout clear the localstorage, so that you will be able to maintain in offline too.
回答2:
In Android, FirebaseApp
object manages the configuration for all the Firebase APIs. This is initialized automatically by a content provider when your app is launched, and you typically never need to interact with it. However, when you want to access multiple projects from a single app, you'll need a distinct FirebaseApp to reference each one individually.
Before initializing your second project, make sure Firebase defaultApp instance is initialized. Call FirebaseDatabase.getInstance()
from your base/main project's Application class - OnCreate() as below,
// Primary project database
FirebaseDatabase database = FirebaseDatabase.getInstance();
To connect second project database from your library project, initialize FirebaseApp
with secondary configuration along with some distinct identifier(eg:"secondary") as below,
FirebaseOptions options = new FirebaseOptions.Builder()
.setApplicationId("1:530266078999:android:481c4ecf3253701e") // Required for Analytics.
.setApiKey("AIzaSyBRxOyIj5dJkKgAVPXRLYFkdZwh2Xxq51k") // Required for Auth.
.setDatabaseUrl("https://project-1765055333176374514.firebaseio.com/") // Required for RTDB.
.build();
FirebaseApp.initializeApp(this /* Context */, options, "secondary");
Then retrive the secound App
// Retrieve the second app.
FirebaseApp secondApp = FirebaseApp.getInstance("secondary");
// Get the database of second app.
FirebaseDatabase secondaryDatabase = FirebaseDatabase.getInstance(secondApp);
Note: Inside your library module, make sure you always refer your second project. Better create a singleton reference
// Make singleton reference
public static FirebaseApp getFirebaseApp() {
if (mFirebaseApp == null) {
Context context = getAppContext();
FirebaseOptions options = getLibraryModuleFirebaseOptions();
mFirebaseApp = FirebaseApp.initializeApp(context, options, "library");
}
return mFirebaseApp;
}
For detailed information, please refer the Firebase blog post here
Hope this would help you..
来源:https://stackoverflow.com/questions/44719807/how-to-work-with-firebaseauth-inside-android-module