Home > Blockchain >  Firebase Realtime Database Log Out Google Account
Firebase Realtime Database Log Out Google Account

Time:10-31

I have implemented Sign In the method using Google Accounts, my problem is the logout function doesn't behave what I want. I don't receive any error, it's just the code doesn't work properly.

Can someone tell me what's wrong with this code?

Options Fragment:

        Preference logoutPreference = findPreference(getString(R.string.pref_key_logout));
        logoutPreference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
            public boolean onPreferenceClick(Preference preference) {
                FirebaseAuth.getInstance().signOut();
                Intent broadcastIntent = new Intent();
                broadcastIntent.setAction("com.example.budgetapp.ACTION_LOGOUT");
                getActivity().sendBroadcast(broadcastIntent);
                getActivity().startActivity(new Intent(getActivity(), SignInActivity.class));
                getActivity().finish();
                return true;
            }
        });

Main Activity:

    @Override
    protected void onResume() {
        super.onResume();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("com.example.budgetapp.ACTION_LOGOUT");
        receiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                finish();
            }
        };
        registerReceiver(receiver, intentFilter);
    }

Why is the code not working? I'm using Firebase Realtime Database

Whenever I click Logout the app redirects me to Sign In Activity (which is good) but when I try to Sign In, it automatically signs in to the previous Google Account.

CodePudding user response:

When you are using the following line of code:

FirebaseAuth.getInstance().signOut();

It means that you are signing out only from Firebase.

Whenever I click Logout the app redirects me to Sign In Activity (which is good) but when I try to Sign In, it automatically signs in to the previous Google Account.

As I understand, you are using the Google Provider for authentication. Signing out from Firebase doesn't mean that you are automatically signed out from Google. To sign out from Google you have to explicitly add a call to GoogleSignInClient#signOut() method:

googleSignInClient.signOut();

Don't also forget that the sign-out operation is asynchronous, meaning that you have to wait until the operation completes. Since this method returns an object of type Task<Void>, you can use addOnCompleteListener(OnCompleteListener listener) method, to know when you are completely signed out.

  • Related