android - 如何在 Android 中使用 Firebase 存储用户数据?

标签 android firebase firebase-realtime-database firebase-authentication

我需要为登录应用程序的每个用户创建并使用包含所有详细信息(uid、名称等)的数据库条目

我在存储用户数据以便在 Android 中使用 Firebase 使用和检索用户个人资料信息时遇到问题。我找到了这个documentation在旧版本的 Firebase 上,但这似乎不再有效。

有谁知道如何重现上述代码,使其适用于新版本的 Firebase?非常感谢。

编辑 - 下面的代码:

final Firebase ref = new Firebase("https://<YOUR-FIREBASE-APP>.firebaseio.com");
ref.authWithPassword("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e68c8388889fa6839e878b968a83c885898b" rel="noreferrer noopener nofollow">[email protected]</a>", "correcthorsebatterystaple",
new Firebase.AuthResultHandler() {
@Override
public void onAuthenticated(AuthData authData) {
    // Authentication just completed successfully :)
    Map<String, String> map = new HashMap<String, String>();
    map.put("provider", authData.getProvider());
    if(authData.getProviderData().containsKey("displayName")) {
        map.put("displayName", authData.getProviderData().get("displayName").toString());
    }
    ref.child("users").child(authData.getUid()).setValue(map);
}
@Override
public void onAuthenticationError(FirebaseError error) {
    // Something went wrong :(
}
});

最佳答案

要在firebase数据库(新版本)中创建用户,您需要进行以下更改..

      private FirebaseAuth mAuth;
        String mUserEmail = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6d07080303142d08150c001d0108430e0200" rel="noreferrer noopener nofollow">[email protected]</a>";
        String mPassword = "correcthorsebatterystaple"

      mAuth.createUserWithEmailAndPassword(mUserEmail, mPassword)
     .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {

                    Log.d(LOG_TAG, getString(R.string.log_message_auth_successful) + " createUserWithEmail:onComplete:" + task.isSuccessful());

                    // if task is not successful show error
                    if (!task.isSuccessful()) {

                        try {
                            throw task.getException();
                        } catch (FirebaseAuthUserCollisionException e) {
                            // log error here                            

                      } catch (FirebaseNetworkException e) {
                            // log error here  
                        } catch (Exception e) {
                         // log error here        
                         }

                        } else {

                  // successfully user account created
                 // now the AuthStateListener runs the onAuthStateChanged callback

                    }
                }

            });
         }

现在在 onCreate() 中添加以下方法。

   final DatabaseReference ref = FirebaseDatabase.getInstance().
     getReferenceFromUrl(https://<YOUR-FIREBASE-APP>.firebaseio.com");
     mAuth = FirebaseAuth.getInstance();
     mAuthListener = new FirebaseAuth.AuthStateListener() {
        @Override
        public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
            FirebaseUser user = firebaseAuth.getCurrentUser();

            if (user != null) {
                // User is signed in
             Map<String, String> map = new HashMap<String, String>();
             map.put("provider", user.getProvider());
             if(user.getProviderData().containsKey("displayName")) {
             map.put("displayName",  
             user.getProviderData().get("displayName").toString());
            } 
            ref.child("users").child(user.getUid()).setValue(map);
            } else {
                // User is signed out
                Log.d(LOG_TAG, "onAuthStateChanged:signed_out");
            }
        }
    };

   @Override
public void onStart() {
    super.onStart();
    mAuth.addAuthStateListener(mAuthListener);
}

@Override
public void onStop() {
    super.onStop();
    if (mAuthListener != null) {
        mAuth.removeAuthStateListener(mAuthListener);
    }

}

关于android - 如何在 Android 中使用 Firebase 存储用户数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42682633/

相关文章:

android - 获取的联系电话显示重复号码(Whatsapp 和双人号码)

java - 编译时,Android Manifest 出现问题导致无法运行?

IOS - 未从 firebase 控制台收到 FCM 推送通知

node.js - 仅允许对 Cloud Functions for Firebase 进行写访问

android - 需要始终删除 Firebase 监听器吗?

android - android jetpack compose中的 "remember"和 "mutableState"有什么区别?

html - 在没有 WebView 的情况下在 Android 中提交 HTML 表单

firebase - 如何在 Firebase 托管上的多个发行版本之间进行流量分配

swift - 从 firebase 检索数据返回 nil

android - java.util.HashMap 无法转换为 com.google.android.gms.maps.model.LatLng