Advertisements

MainActivity for Firebase Auth

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;

public class MainActivity extends AppCompatActivity {

  private FirebaseAuth mAuth;
  private TextView mTvEmail, mTvLogout;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mAuth = FirebaseAuth.getInstance();


    mTvLogout = findViewById(R.id.txtBtnLogout);
    mTvEmail = findViewById(R.id.tvEmail);


    FirebaseUser currentUser = mAuth.getCurrentUser();
    if (currentUser == null) {
//No one signed in
      startActivity(new Intent(this, SignInActivity.class));
      this.finish();
    }else{
//User logged in
      mTvEmail.setText(currentUser.getEmail());
    }



    mTvLogout.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View view) {
        mAuth.signOut();
        startActivity(new Intent(MainActivity.this, SignInActivity.class));
        MainActivity.this.finish();

      }
    });


  }
}