我需要为登录应用程序的每个用户创建并使用包含所有详细信息(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/