如何在Android模块中使用FirebaseAuth [英] How to work with FirebaseAuth inside Android Module
问题描述
我正在开发一个聊天库,在其中我想显示登录的用户的谈话。做图书馆的原因是我想把它整合到多个项目中。
我现在面对的问题是 FirebaseAuth
表示用户没有登录( FirebaseAuth.getInstance(mFirebaseApp).getCurrentUser()
总是返回 null
),即使用户在应用程序内进行了身份验证。 / p>
模块/库如何知道用户是否已通过身份验证?
我正在尝试以下代码: / p>
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;
列出< FirebaseApp> firebaseApps = FirebaseApp.getApps(getActivity()); (FirebaseApp app:firebaseApps){
if(app.getName()。equals(FIREBASE_APP_NAME)){
hasBeenInitialized = true;
mFirebaseApp = app; $!
$ if(!hasBeenInitialized){
mFirebaseApp = FirebaseApp.initializeApp(getActivity(),options,FIREBASE_APP_NAME); (FirebaseAuth.getInstance(mFirebaseApp).getCurrentUser()== null){
Toast.makeText(getActivity(),需要登录!,Toast。 LENGTH_SHORT).show();
return;
您需要添加一个侦听器/ Observer获取每个auth状态变化的用户标识。
FirebaseAuth.getInstance()。addAuthStateListener(firebaseAuth - > {
if(firebaseAuth.getCurrentUser()== null){
your code goes here
}
});
https://firebase.google.com/docs/auth/android/manage-users
另一种方法wkile登录获取用户ID并存储在LocalStorage中,而注销清除localstorage,这样你也可以保持在离线状态。
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;
}
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.
这篇关于如何在Android模块中使用FirebaseAuth的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!