0

This is my Activity to log in my users. When a new user is created, I want to write his UUID + empty string team to Firebase. The way I have it now, however, is that a child of "users" gets overwritten at every login (I can't have that, I pass an empty string 'teams' to be put in Firebase, but the String doesn't stay empty - I need to keep the data across logins).

Thank you for your help

public class SigninActivity extends AppCompatActivity {


private static final String TAG = SigninActivity.class.getSimpleName();
private static final String PATH_TOS = "";
private Button loginButton;
private FirebaseAuth auth;
private static final int RC_SIGN_IN = 200;
private FirebaseDatabase db;
private DatabaseReference dbUsers;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate( savedInstanceState );
    auth = FirebaseAuth.getInstance();
    if(isUserLogin()){
        loginUser();}
    setContentView( R.layout.activity_signin );
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    db = FirebaseDatabase.getInstance();
    loginButton = findViewById(R.id.login_button);
    loginButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view){
            startActivityForResult(AuthUI.getInstance().createSignInIntentBuilder()
                    .setTosUrl (PATH_TOS)
                    .build(), RC_SIGN_IN);
        }
    });

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode == RC_SIGN_IN){
        if(resultCode == RESULT_OK){
            loginUser();
        }
        if(resultCode == RESULT_CANCELED){
            displayMessage(getString(R.string.signin_failed));
        }
        return;
    }
    displayMessage(getString(R.string.unknown_response));
}

private boolean isUserLogin(){
    if(auth.getCurrentUser() != null){
        return true;
    }
    return false;
}
private void loginUser(){
    writeNewUser(auth.getCurrentUser().getUid(),"");
    Intent loginIntent = new Intent(SigninActivity.this, MainActivity.class);
    startActivity(loginIntent);
    finish();
}

private void displayMessage(String message){
    Toast.makeText(this, message, Toast.LENGTH_LONG).show();
}
private void writeNewUser(String uuid,String team){
    DatabaseReference dbUsers = db.getReference("users");
    User user = new User(uuid,team);
    dbUsers.child(uuid).setValue(user);
}

}

ReyAnthonyRenacia
  • 17,219
  • 5
  • 37
  • 56
user3302053
  • 81
  • 2
  • 8
  • 1
    I'd suggest using `snapshot.exists()` to check if data exists at a specific path, in this case your `users/UID/team` path - if it does exist then don't overwrite? See [Test if a data exist in Firebase](https://stackoverflow.com/questions/24824732/test-if-a-data-exist-in-firebase) – Benjamin Scholtz Jun 03 '18 at 15:52
  • Possible duplicate of [Checking if a particular value exists in the firebase database](https://stackoverflow.com/questions/47893328/checking-if-a-particular-value-exists-in-the-firebase-database) – Alex Mamo Jun 04 '18 at 12:44

0 Answers0