I have a method to write data to the Firebase database, but when I write it in the FirebaseAuthWithGoogle method, every time I click on the login button or register with Google, my account information is overwritten, that is, if a person registers in the application through Google, and then goes tries to log in, all its data is canceled and written again. How to make so that new users are recorded in the database and already registered, just logged into your account without overwriting the data? Just in case, I will explain again with an example, a new user clicks the "Login via Google" button, his account is registered in the Firebase database, authentication and all the data I need you can see in WriteNewUser are entered into the database even after the user has logged in. he chose his hero, and this is also written to the database, but as soon as he comes out and logs in to his account, WriteNewUser will work and everything he received before he is reset. If I log out and log in to my account, all this data will be overwritten https://i.stack.imgur.com/8wZrd.png

OnCreate:

protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViewById(R.id.create).setOnClickListener(this); findViewById(R.id.signinwithEmail).setOnClickListener(this); findViewById(R.id.signInGoogle).setOnClickListener(this); mAuth = FirebaseAuth.getInstance(); mDatabase = FirebaseDatabase.getInstance().getReference(); GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) .requestIdToken(getString(R.string.default_web_client_id)) .requestEmail() .build(); mGoogleSignInClient = GoogleSignIn.getClient(this, gso); mAuth = FirebaseAuth.getInstance(); 

OnActivityResult:

  public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == RC_SIGN_IN) { Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data); try { GoogleSignInAccount account = task.getResult(ApiException.class); assert account != null; firebaseAuthWithGoogle(account); } catch (ApiException e) { Log.w(TAG, "Ошибка входа", e); updateUI(null); } } } 

FirebaseAuthWithGoogle: Here and there is an overwrite or record

  private void firebaseAuthWithGoogle(GoogleSignInAccount acct) { Log.d(TAG, "Аутентификация" + acct.getId()); String account = acct.getEmail(); showProgressDialog(); AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null); mAuth.signInWithCredential(credential) .addOnCompleteListener(this, task -> { if (task.isSuccessful()) { Log.d(TAG, "Успешно"); FirebaseUser user = mAuth.getCurrentUser(); updateUI(user); writeNewUser(account,getUid()); //Here I call the write or overwrite method 

Google user:

 public class MainActivity extends BaseActivity implements View.OnClickListener { private static final String TAG = "Registration"; private static final int RC_SIGN_IN = 9001; private DatabaseReference mDatabase; private FirebaseAuth mAuth; private GoogleSignInClient mGoogleSignInClient; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViewById(R.id.create).setOnClickListener(this); findViewById(R.id.signinwithEmail).setOnClickListener(this); findViewById(R.id.signInGoogle).setOnClickListener(this); mAuth = FirebaseAuth.getInstance(); mDatabase = FirebaseDatabase.getInstance().getReference(); GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) .requestIdToken(getString(R.string.default_web_client_id)) .requestEmail() .build(); mGoogleSignInClient = GoogleSignIn.getClient(this, gso); mAuth = FirebaseAuth.getInstance(); } @Override public void onStart() { super.onStart(); FirebaseUser user = mAuth.getCurrentUser(); updateUI(user); } private void updateUI(FirebaseUser user) { if (user != null) { Intent intent = new Intent(MainActivity.this, MenuActivity.class); startActivity(intent); finish(); } } @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == RC_SIGN_IN) { Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data); try { GoogleSignInAccount account = task.getResult(ApiException.class); assert account != null; firebaseAuthWithGoogle(account); } catch (ApiException e) { Log.w(TAG, "Ошибка входа", e); updateUI(null); } } } private void firebaseAuthWithGoogle(GoogleSignInAccount acct) { Log.d(TAG, "Аутентификация" + acct.getId()); String account = acct.getEmail(); showProgressDialog(); AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null); mAuth.signInWithCredential(credential) .addOnCompleteListener(this, task -> { if (task.isSuccessful()) { Log.d(TAG, "Успешно"); FirebaseUser user = mAuth.getCurrentUser(); updateUI(user); writeNewUser(account,getUid()); //Here I call the write or overwrite method } else { Log.w(TAG, "Ошибка", task.getException()); updateUI(null); } hideProgressDialog(); }); } private void signIn() { Intent signInIntent = mGoogleSignInClient.getSignInIntent(); startActivityForResult(signInIntent, RC_SIGN_IN); } private void writeNewUser(String userId, String account) { GoogleUser user = new GoogleUser(account, 0); mDatabase.child("users").child(userId).setValue(user); mDatabase.child("users").child(userId).child("coupons").child("petiarochka").child("petiarochka100").setValue(0); mDatabase.child("users").child(userId).child("coupons").child("petiarochka").child("petiarochka300").setValue(0); mDatabase.child("users").child(userId).child("coupons").child("petiarochka").child("petiarochka500").setValue(0); mDatabase.child("users").child(userId).child("coupons").child("lenta").child("lenta100").setValue(0); mDatabase.child("users").child(userId).child("coupons").child("lenta").child("lenta300").setValue(0); mDatabase.child("users").child(userId).child("coupons").child("lenta").child("lenta500").setValue(0); } 

    1 answer 1

    It's simple. The FirebaseUser object has an ID . When a user comes in via a social network network, make a query in the database for the presence of a record for this user. If it is - then the user is not the first time logged; otherwise, for the first time. In this case, write it to your database. So next time he will be recognized as previously authorized.

    Perhaps this link will be useful to you: https://firebase.google.com/docs/auth/android/manage-users

    • Okay. I understood your answer, but I don’t understand how to make a request for availability, that is, I understand what to do if (getUid == null (in the database) but I don’t understand what method is needed to search the database - georgiy110802
    • I hope clearly explained - georgiy110802
    • @ georgiy110802, I just created an entry in the database during registration. ID was taken from the user. Created if there is no such record. How to get it ... I do not remember the code, but this is the simplest operation from the database - you need to look in the documentation. - Yuriy SPb
    • Okay, thanks a lot - georgiy110802