Search code examples
javaandroidfirebase-authenticationverification

Firebase: User has to be verificated every time, it runs the application


I am working on an android application, where currently I am trying to implement an authentication function with the usage of Firebase.

The functionalities work as they should, the user can log in both ways I expect, but every time it opens the program, has to go through an authentication procedure. Also, after clicking on the log off button, the system also drops up the google-verification immediately. (after avoiding it, we finally get to the menu with the login stuff again, but it is very annoying this way)

public class FragmentMain extends Fragment {

private static final int MY_REQUEST_CODE = 7117;
List<AuthUI.IdpConfig> providers;
Button logout;

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.main_fragment, container, false);

    logout = v.findViewById(R.id.logout);
    logout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            AuthUI.getInstance()
                    .signOut(getActivity())
                    .addOnCompleteListener(new OnCompleteListener<Void>() {
                        @Override
                        public void onComplete(@NonNull Task<Void> task) {
                            logout.setEnabled(false);
                            showSignOptions();
                        }
                    }).addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    Toast.makeText(getActivity(), "" + e.getMessage(), Toast.LENGTH_SHORT).show();
                }
            });
        }
    });

    providers = Arrays.asList(
        new AuthUI.IdpConfig.EmailBuilder().build(),
        new AuthUI.IdpConfig.GoogleBuilder().build()    
    );

    showSignOptions(); 
    return v;
}

private void showSignOptions() {
    startActivityForResult(
            AuthUI.getInstance().createSignInIntentBuilder()
                    .setAvailableProviders(providers)
                    .setLogo(R.drawable.loginlogo)
                    .setTheme(R.style.LogInTheme)
                    .build(), MY_REQUEST_CODE
    );
}

@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if(requestCode == MY_REQUEST_CODE)
    {
        IdpResponse response = IdpResponse.fromResultIntent(data);
        if(resultCode == RESULT_OK)
        {
            FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();

            Toast.makeText(getActivity(), "" + user.getEmail(), Toast.LENGTH_SHORT).show();
            logout.setEnabled(true);
        }else{
            Toast.makeText(getActivity(), "" + response.getError().getMessage(), Toast.LENGTH_SHORT).show();
        }
    }
}
}

In my opinion the FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser() should be the line which causes the problem, but not sure how to solve it (I mean of course, if it is the problem...)

Is there any way to block the system for asking for verification if the user once logged in, as long as it logs out? - also when it logs out, get back to the login intent?


Solution

  • you can try that; in onStart() method in your launcher activity, you can check whether the user is logged in or not. you can do something like that

    FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
    if (user != null) {
        // User is signed in, go to home activity/fragment.
    } else {
        // No user is signed in, go to signin acitvity/fragment.
    }