this is my first time to ask in StackOverflow. So that, I would like to share a problem in this forum. I've had registered an account to firebase authentication and record that on Firebase Realtime-Database. You can see on these pictures
https://i.ibb.co/TcqDhF7/image.png
https://i.ibb.co/Zc1YnLY/image.png
The problem is on Login. When I logged in, I expected that Firebase can match data between authentication and database but that was opposite. the question is Can Firebase match user data between authentication and the data record on Database?
I've tried to modify my Login Activity class like this
LoginActivity.java
package id.co.dolansemarang.loginfirebaseds;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
public class LoginActivity extends AppCompatActivity {
Button btnLogin;
EditText edtEmailLogin, edtPasswordLogin;
FirebaseAuth firebaseAuthLogin;
DatabaseReference userRefLogin;
FirebaseUser curUser;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
btnLogin = findViewById(R.id.btn_login);
edtEmailLogin = findViewById(R.id.edt_email_login);
edtPasswordLogin = findViewById(R.id.edt_password_login);
firebaseAuthLogin = FirebaseAuth.getInstance();
curUser = FirebaseAuth.getInstance().getCurrentUser();
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String email = edtEmailLogin.getText().toString();
final String password = edtPasswordLogin.getText().toString();
if (email.isEmpty()) {
Toast.makeText(LoginActivity.this, "Masukkan email", Toast.LENGTH_SHORT).show();
} else {
if (password.isEmpty()) {
Toast.makeText(LoginActivity.this, "Masukkan password", Toast.LENGTH_SHORT).show();
} else {
firebaseAuthLogin.signInWithEmailAndPassword(email, password).addOnCompleteListener(LoginActivity.this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
userRefLogin = FirebaseDatabase.getInstance().getReference().child("Users").child(curUser.getUid());
userRefLogin.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
//ambil data password
String passwordFromFirebase = dataSnapshot.child("password").getValue().toString();
//validasi password dengan password di edit text
if (password.equals(passwordFromFirebase)) {
// menyimpan data ke local storage (handphone)
// SharedPreferences sharedPreferences = getSharedPreferences(USERNAME_KEY, MODE_PRIVATE);
// SharedPreferences.Editor editor = sharedPreferences.edit();
// editor.putString(username_key, etUsername.getText().toString());
// editor.apply();
startActivity(new Intent(LoginActivity.this, MainActivity.class));
} else {
Toast.makeText(LoginActivity.this, "Pasword tidak cocok", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(LoginActivity.this, "Username tidak terdaftar", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Toast.makeText(LoginActivity.this, "Pasword tidak cocok", Toast.LENGTH_SHORT).show();
}
});
} else {
Toast.makeText(LoginActivity.this, "Pasword tidak cocok", Toast.LENGTH_SHORT).show();
Log.w("Pesan", "Gagal Masul");
}
}
});
}
}
}
});
}
}
I expect firebase can check matchable password and email data on both of them so that when I login app, Firebase check email and password on Firebase auth and database . I need help :(