Android Firebase使用relogin删除所有用户数据

时间:2017-12-10 12:07:26

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

我正在尝试在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);

                    }


                }
            });



}

所以,一旦我运行代码,它第一次完全正常工作并说我编辑并添加其他用户信息但是一旦用户注销并重新进入,一切都被清除,节点再次被创建。 / p>

2 个答案:

答案 0 :(得分:1)

您在此处将数据保存在数据库中:

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 {

答案 1 :(得分:0)

这是源代码的工作版本。您也可以使用相同的代码进行Facebook登录。此代码只是防止用户在再次登录时被覆盖。

  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(SignInActivity.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");

                        //getting current users account
                        FirebaseUser user=mAuth.getCurrentUser();

                        //getting the display name of the current user to store them in our real time database

                        final String databaseUserName=user.getDisplayName();


                        //creating a child called users
                        final DatabaseReference myRootRef = FirebaseDatabase.getInstance().getReference().child("Users");



                        //here we make a control such that, if logged in user is exist in the realtime database
                        //if not exists, then we save them , if exists we continue with the else statement and break it.
                        myRootRef.addListenerForSingleValueEvent(new ValueEventListener() {
                            @Override
                            public void onDataChange(DataSnapshot dataSnapshot) {
                                if(!dataSnapshot.hasChild(databaseUserName)){

                                    DatabaseReference userNameRef =  myRootRef.child(databaseUserName);
                                    //value is also set to user display name however it doenst have to be so
                                    userNameRef.setValue(databaseUserName);



                                } else{


                                }
                            }

                            @Override
                            public void onCancelled(DatabaseError databaseError) {

                            }
                        });




                        //after that user is redirected to the main account activity.
                        Intent accountIntent = new Intent(SignInActivity.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(SignInActivity.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(SignInActivity.this, "Failed Registration: "+e.getMessage(), Toast.LENGTH_SHORT).show();

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

                    }


                }
            });



}