Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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);
                }

            }
        };
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
159 views
Welcome To Ask or Share your Answers For Others

1 Answer

Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...