Initializing Firebase Authentication

Let's get our hands dirty by instantiating the required classes and methods. In the onCreate(..) method of the Activity, initialize the FirebaseAuth class.  Check whether the getCurrentUser method is returning null values. If it returns, ask the user to log in again or else take the user to the next activity:

package com.packt.firebase.mastering.authentication;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.google.firebase.auth.FirebaseAuth;

public class MainActivity extends AppCompatActivity {

private FirebaseAuth mAuth;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAuth = FirebaseAuth.getInstance();
if (mAuth.getCurrentUser() != null) {
startActivity(new Intent(this, DashBoardActivity.class));
finish();
} else {
Authenticate();
}
}
}
}

We have written the preceding code so that if an unauthorized user tries to log in, the application will prompt the user to enter their credentials. If the user is authorized, we will take them to the DashboardActivity.