In my app a User can sign up and than sign in, after successfully signing in he's taken to an activity with events and stuff according to his preferences. I am able to sign up and sign in. But after signing when i can't get the current user from firebase auth. and am i signing in properly. I am new to android. All help will be appreciated. The first code is loginactivity and the second one is where i am trying to get the uid.
public class LoginActivity extends AppCompatActivity {
FirebaseDatabase database = FirebaseDatabase.getInstance();
private DatabaseReference mdatabase;
private DatabaseReference fdatabase;
FirebaseAuth auth = FirebaseAuth.getInstance();
FirebaseUser user = auth.getCurrentUser();
private EditText txtEmailLogin;
private EditText txtPassLogin;
private FirebaseAuth firebaseAuth;
private FirebaseAuth.AuthStateListener mAuthlistener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
txtEmailLogin = findViewById(R.id.EmailEditText);
txtPassLogin = findViewById(R.id.PassEditText);
firebaseAuth = FirebaseAuth.getInstance();
//controleert als er ingelogd moet worden of niet.
/* if (firebaseAuth.getCurrentUser() != null)
{
Intent i = new Intent(LoginActivity.this, Home.class);
i.putExtra("Email", firebaseAuth.getCurrentUser().getEmail());
startActivity(i);
}*/
}
public void btnregister_Click(View v) {
Intent i = new Intent(LoginActivity.this,RegisterActivity.class);
startActivity(i);
}
public void btnLogin_Click(View v){
ProgressDialog progressDialog = ProgressDialog.show(LoginActivity.this,"Please Wait...", "Processing...", true);
(firebaseAuth.signInWithEmailAndPassword(txtEmailLogin.getText().toString(), txtPassLogin.getText().toString()))
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if(task.isSuccessful()) {
Toast.makeText(LoginActivity.this ,"Login Succesfull" ,Toast.LENGTH_SHORT).show();
Intent i = new Intent(LoginActivity.this, Home.class);
startActivity(i);
}
else
{
Log.e("Error", task.getException().toString());
Toast.makeText(LoginActivity.this, task.getException().getMessage(),Toast.LENGTH_LONG).show();
}
}
});
}
}
public class Home extends AppCompatActivity {
FirebaseAuth mAuth;
FirebaseAuth.AuthStateListener mAuthListener;
TextView txtUserNaam;
String uid,UserName;
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference mdatabase;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
mAuth=FirebaseAuth.getInstance();
mAuthListener=new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
if (mAuth.getCurrentUser().getUid() == null) {
//Your action here
Intent i = new Intent(Home.this, LoginActivity.class);
startActivity(i);
} else {
txtUserNaam = findViewById(R.id.txtusernaam);
mdatabase.child("USERS").child(firebaseAuth.getCurrentUser().getUid()).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
UserName = dataSnapshot.child("UserName").getValue(String.class);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
txtUserNaam.setText(UserName);
}
}
};
}
}
AuthStateListener is used to observe changes to the user's sign-in state. So you should have it in your LoginActivity
mAuthListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
if (firebaseAuth.getCurrentUser() != null) {
// intent call to second activity
}
}
}
You can bind you Listener to Auth in onStart() method like below
@Override
protected void onStart() {
super.onStart();
mAuth.addAuthStateListener(mAuthListener);
}
For getting the current logged in user details you can use
FirebaseAuth.getInstance().getCurrentUser().getUid();
this will return uid as a String.
Instead of declaring
FirebaseAuth auth = FirebaseAuth.getInstance();
FirebaseUser user = auth.getCurrentUser();
Add this into your OnCreate
FirebaseUser mCurrentUser = FirebaseAuth.getInstance().getCurrentUser();
if (mCurrentUser != null) {
uid = mCurrentUser.getUid();
Then if you want you can declare your uid as public static String uid
so that you would not have to rewrite the code and could just use the variable
uid
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.