Skip to content Skip to sidebar Skip to footer

How To Prevent Users From Logging In An Android App, If They Don't Click On The Verification Link Sent By Firebase Authentication First?

I am making an android chat application. It opens up with a StartActivity page that leads to either of the Login or Register pages. I am using Firebase authentication system, i.e.

Solution 1:

The NullPointerException is caused by this code:

public class StartActivity extends AppCompatActivity {

    MaterialButton goToLogin, goToRegister;
    ImageView banner;

    FirebaseUser firebaseUser;
    FirebaseAuth auth;

    @Override
    protected void onStart() {
        super.onStart();

        firebaseUser = FirebaseAuth.getInstance().getCurrentUser();

        //redirect if user is not null
        if (Objects.requireNonNull(auth.getCurrentUser()).isEmailVerified()) {

You never initialize auth before the call to auth.getCurrentUser(), which is why you get a NullPointerException. The simple fix is to initialize auth:

protected void onStart() {
    super.onStart();

    auth = FirebaseAuth.getInstance();

    firebaseUser = auth.getCurrentUser();

    //redirect if user is not null
    if (Objects.requireNonNull(auth.getCurrentUser()).isEmailVerified()) {
        ...

But I also highly recommend reading What is a NullPointerException, and how do I fix it?, as this type of error is quite common and an easily be fixed by following the approach outlined there.


Post a Comment for "How To Prevent Users From Logging In An Android App, If They Don't Click On The Verification Link Sent By Firebase Authentication First?"