gpt4 book ai didi

java - Android Firebase通过重新登录删除所有用户数据

转载 作者:行者123 更新时间:2023-12-02 11:53:31 29 4
gpt4 key购买 nike

我正在尝试在 Firebase 实时数据库中创建用户表。但是,每次用户重新登录时,他之前输入的数据都会被删除或覆盖。无法理解应该如何更改它。

  private void firebaseAuthWithGoogle(GoogleSignInAccount account) {





AuthCredential credential = GoogleAuthProvider.getCredential(account.getIdToken(), null);
mAuth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {



Toast.makeText(RegisterActivity.this,"Registration Is Succesfull",Toast.LENGTH_LONG).show();
// Sign in success, update UI with the signed-in user's information
Log.d(TAG, "signInWithCredential:success");

FirebaseUser user=mAuth.getCurrentUser();

final String databaseUserName=user.getDisplayName();


String name=mAuth.getCurrentUser().getDisplayName();

DatabaseReference myRootRef = FirebaseDatabase.getInstance().getReference().child("Users");


DatabaseReference userNameRef = myRootRef.child(databaseUserName);


//after that user is redirected to the main account activity.
Intent accountIntent = new Intent(RegisterActivity.this,UserAccountActivity.class);
accountIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(accountIntent);


} else {
// If sign in fails, display a message to the user.
Log.w(TAG, "signInWithCredential:failure", task.getException());
Toast.makeText(RegisterActivity.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();

// if signing up task is unsuccesfull we do make a error indication pretty much.
FirebaseAuthException e = (FirebaseAuthException )task.getException();
Toast.makeText(RegisterActivity.this, "Failed Registration: "+e.getMessage(), Toast.LENGTH_SHORT).show();

Log.e("LoginActivity", "Failed Registration", e);

}


}
});



}

因此,一旦我运行代码,第一次它工作得很好,并说我编辑并添加了其他用户信息,但一旦用户注销并重新输入,所有内容都会被清除,并再次创建节点。

最佳答案

这里您将数据保存在数据库中:

DatabaseReference myRootRef = FirebaseDatabase.getInstance().getReference().child("Users");
DatabaseReference userNameRef = myRootRef.child(databaseUserName);

现在第二次,数据没有被删除或清除,它不能被神奇地删除,数据被覆盖。您需要做的就是添加一个 push() ,它将为每次登录创建一个随机 ID。

就像这样:

DatabaseReference myRootRef = FirebaseDatabase.getInstance().getReference().child("Users").push();
DatabaseReference userNameRef = myRootRef.child(databaseUserName)

编辑:

auth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(Activity_name_here.this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
Toast.makeText(getApplicationContext(), "createUserWithEmail:onComplete:" + task.isSuccessful(), Toast.LENGTH_SHORT).show();
// If sign in fails, display a message to the user. If sign in succeeds
// the auth state listener will be notified and logic to handle the
// signed in user can be handled in the listener.
if (!task.isSuccessful()) {
Toast.makeText(getApplicationContext(), "Authentication failed." + task.getException(),
Toast.LENGTH_SHORT).show();
} else {

关于java - Android Firebase通过重新登录删除所有用户数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47738678/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com