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 Register.java 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;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
 /// Connecting Firebase to the app       
        FirebaseApp.initializeApp(this);
        mAuth = FirebaseAuth.getInstance();

  /// If User Already Exist, if so they Will be auto-Signed in
        firebaseAuthStateListener = new 
        FirebaseAuth.AuthStateListener() {
            @Override
            public void onAuthStateChanged(@NonNull FirebaseAuth 
        firebaseAuth) {
                final FirebaseUser user = 
        FirebaseAuth.getInstance().getCurrentUser();
                if (user !=null){
                    Intent intent = new Intent(Register.this, 
        MainActivity.class);
                    startActivity(intent);
                    finish();
                    return;
                }
            }
        };
///Setting the variables from the layout.
    mSiginUp = findViewById(R.id.button_create);
    mEmail = findViewById(R.id.editText_email);
    xPassword = findViewById(R.id.editText_password);
    mPassword = findViewById(R.id.editText_passconfirm);

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() {
        @Override
        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
            if 
 (!xPassword.getText().toString().equals(mPassword.getText().toString() 
   ))
            {
            Toast.makeText(Register.this, "Must Match!", 
            Toast.LENGTH_SHORT).show();
            }
/// Authorizing users to Firebase 
            mAuth.createUserWithEmailAndPassword(email, 
            password).addOnCompleteListener(Register.this, new 
            OnCompleteListener<AuthResult>() {
                        @Override
                        public void onComplete(@NonNull 
            Task<AuthResult> task) {
              if (!task.isSuccessful()) {
                   Toast.makeText(Register.this, "sign up error", 
            Toast.LENGTH_SHORT).show();
              } else {
  /// If user is created move to Main Activity           
            Intent intent = new Intent(Register.this, 
            MainActivity.class);
                    startActivity(intent);
                    finish();
                    }
                        }
                    });
              }
        });
    }
    @Override
    protected void onStart() {
        super.onStart();
        mAuth.addAuthStateListener(firebaseAuthStateListener);
    }
    @Override
    protected void onStop() {
        super.onStop();
        mAuth.removeAuthStateListener(firebaseAuthStateListener);
    }
    public void already(View view) {
        Intent intent = new Intent(Register.this, SignIn.class);
        startActivity(intent);
        finish();
    }
}

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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