Android Studio: Firebase Sign In and User Authentication Part 2

In the first part, the Registration and the Sign In activity were created. Also, the app was linked to the Firebase console.

Once your app is connected to Firebase, select the app in your Firebase console. From there choose the Authentication tab on the left side of the screen.

So this is the tab you will use to control how users are authenticated in your app. In the top to the screen select Sign-In method . As you will see a list of the many Firebase Sign- In Methods but this tutorial will just be demonstrating Email and Password Sign In method.

Be Sure to select the Email/Password method and Enable the feature.

That’s all for the console portion of the tutorial for now. The next part will be coding the Java files in Android Studio.

Starting with the there are three main sections that need to be coded. The first would be setting the variables, calling OnClickMethod and lastly the Firbebase Auth.

The below portion of code creates and sets the variables and connects the app to Firebase. It also checks if the users already exist, if the user already has an account they will be Auto-Signed In.

public class Register extends AppCompatActivity {
///Creating the Variables
    private FirebaseAuth mAuth;
    private FirebaseAuth.AuthStateListener firebaseAuthStateListener;
    private Button mSiginUp;
    private EditText mEmail, xPassword, mPassword;
    private TextView Already;

    protected void onCreate(Bundle savedInstanceState) {
 /// Connecting Firebase to the app       
        mAuth = FirebaseAuth.getInstance();

  /// If User Already Exist, if so they Will be auto-Signed in
        firebaseAuthStateListener = new 
        FirebaseAuth.AuthStateListener() {
            public void onAuthStateChanged(@NonNull FirebaseAuth 
        firebaseAuth) {
                final FirebaseUser user = 
                if (user !=null){
                    Intent intent = new Intent(Register.this, 
///Setting the variables from the layout.
    mSiginUp = findViewById(;
    mEmail = findViewById(;
    xPassword = findViewById(;
    mPassword = findViewById(;

This below portion of code sets the OnClickMethod and creates a new account for the user when they click the button.

 /// OnClickMethod for Button    
mSiginUp.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {

            final String email = mEmail.getText().toString();
            final String xpassword = xPassword.getText().toString();
            final String password = mPassword.getText().toString();

 /// Checking if Passwords Match
            Toast.makeText(Register.this, "Must Match!", 
/// Authorizing users to Firebase 
            password).addOnCompleteListener(Register.this, new 
            OnCompleteListener<AuthResult>() {
                        public void onComplete(@NonNull 
            Task<AuthResult> task) {
              if (!task.isSuccessful()) {
                   Toast.makeText(Register.this, "sign up error", 
              } else {
  /// If user is created move to Main Activity           
            Intent intent = new Intent(Register.this, 
    protected void onStart() {
    protected void onStop() {
    public void already(View view) {
        Intent intent = new Intent(Register.this, SignIn.class);

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s