0

In this app, I have a logout button that's signed out the user if logged in with email & password or with a google account, the problem happens after signing out and re-launch the app again in the SplashActivity, I do check for the currentUser if is null it should go to the login page, else it should go to the home page, but It seems the app remember the current user even it's logged out before, I tried this solution but it doesn't work

Gif describes the problem

the dagger hilt module

@Module
@InstallIn(SingletonComponent::class)
object AppModule {

    @Provides
    @Singleton
    fun provideFirebaseAuth() = FirebaseAuth.getInstance()

    @Provides
    @Singleton
    fun provideFirebaseFirestore() = FirebaseFirestore.getInstance()


}

SplashActivity code

private const val TAG = "SplashActivity"
@AndroidEntryPoint
class SplashActivity  : AppCompatActivity() {



    private lateinit var binding: ActivitySplashBinding
    @Inject
    lateinit var firebaseAuth: FirebaseAuth

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivitySplashBinding.inflate(layoutInflater)
        setContentView(binding.root)

        val typeface = ResourcesCompat.getFont(this, R.font.blacklist)

        val animation = AnimationUtils.loadAnimation(this, R.anim.my_anim)

        binding.apply {
            splashTextView.typeface = typeface
            splashTextView.animation = animation
        }

        Log.d(TAG, "onCreate: ${firebaseAuth.currentUser?.providerData?.get(0)?.email}")



    }

    override fun onStart() {
        super.onStart()

        Handler(Looper.getMainLooper()).postDelayed({ /* Create an Intent that will start the Menu-Activity. */

            if(firebaseAuth.currentUser == null) {
                val intent = Intent(this@SplashActivity, MainActivity::class.java)
                startActivity(intent)
                this@SplashActivity.startActivity(intent)
                this@SplashActivity.finish()
            }else{
                val intent = Intent(this@SplashActivity, HomeActivity::class.java)
                startActivity(intent)
                this@SplashActivity.startActivity(intent)
                this@SplashActivity.finish()
            }
        }, 3000)
    }
}

ProfileFragment "logout button code"


private const val TAG = "ProfileFragment"

@AndroidEntryPoint
class ProfileFragment : Fragment() {

    private var _binding: FragmentProfileBinding? = null
    private val binding get() = _binding!!

    @Inject
    lateinit var firebaseAuth: FirebaseAuth

    private lateinit var googleSignInClient: GoogleSignInClient

    private val viewModel by viewModels<CredentialsViewModel>()


    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        // Inflate the layout for this fragment
        _binding = FragmentProfileBinding.inflate(inflater, container, false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        binding.logoutButton.setOnClickListener {


            val currentUser = firebaseAuth.currentUser ?: return@setOnClickListener

            for (i in 0 until currentUser.providerData.size) {
                when (currentUser.providerData[i].providerId) {
                    "google.com" -> {
                        //User signed in with a custom account
                        Log.d(
                            TAG,
                            "onViewCreated: provider is ${currentUser.providerData[i].providerId}"
                        )
                        googleSignInClient = GoogleSignIn.getClient(
                            requireActivity(),
                            viewModel.getGoogleSignInOptions()
                        )

                        googleSignInClient.signOut().addOnCompleteListener {

                            if (it.isSuccessful) {
                                redirectToMainActivity()
                            }else {
                                return@addOnCompleteListener
                            }
                        }

                    }
                    "password" -> {
                        Log.d(TAG, "onViewCreated: ${currentUser.providerData[i].providerId}")
                        firebaseAuth.signOut()
                        redirectToMainActivity()
                    }
                    else -> {
                        Log.d(TAG, "onViewCreated: ${currentUser.providerData[i].providerId}")
                        redirectToMainActivity()
                        return@setOnClickListener
                    }
                }
            }

        }
    }

    private fun redirectToMainActivity() {
        val intent = Intent(requireContext(), MainActivity::class.java)
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK)
        startActivity(intent)
        requireActivity().finish()
    }

    override fun onDestroy() {
        super.onDestroy()
        _binding = null
    }
}
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
Dr Mido
  • 2,414
  • 4
  • 32
  • 72

1 Answers1

0

When a user is authenticated with email and password, in order to sign the user out, you only need to call:

firebaseAuth.signOut()

On the other hand, when you need to sign the user out from Google using:

googleSignInClient.signOut().addOnCompleteListener { /* ... /* }

You're only singing out the user from Google. This operation doesn't sign out the user from Firebase automatically. So to sign the user out completely, you have to use:

firebaseAuth.signOut() //Sign out from Firebase
googleSignInClient.signOut().addOnCompleteListener { /* ... /* } //Sign out from Google
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • but I already added code for signing out using "email & password" the code is in when the block, please check it `"password" -> { Log.d(TAG, "onViewCreated: ${currentUser.providerData[i].providerId}") firebaseAuth.signOut() redirectToMainActivity() }` – Dr Mido Feb 15 '23 at 12:12
  • Yes, but you should use `firebaseAuth.signOut()` also when you sign out from Google. Give it a try and tel me if it works. – Alex Mamo Feb 15 '23 at 12:21
  • I added `firebaseAuth.signOut()` before google signing out code and the problem is still persist – Dr Mido Feb 15 '23 at 15:36
  • Does `googleSignInClient.signOut()` complete without no errors? – Alex Mamo Feb 15 '23 at 15:53
  • currently I tested signing out with (email & password) only – Dr Mido Feb 15 '23 at 16:01
  • Try to use them separately and isolate the code so you can be sure that the signOut functions are triggered. – Alex Mamo Feb 15 '23 at 16:35
  • Hey Dr Mido. Can I help you with other information? – Alex Mamo Feb 16 '23 at 07:01
  • unfortunately, I tried to sign out with both methods like you said but the problem still persists, May I share the sample app on GitHub to check it out with more details? – Dr Mido Feb 16 '23 at 07:11
  • When you have a lot of code, something may go wrong. That's why I suggested running those parts of the code in isolation. Besides that, check [this](https://stackoverflow.com/questions/48862359/user-authentication-persisted-after-having-cancelled-the-user-from-console-fireb) out, if did that. – Alex Mamo Feb 16 '23 at 09:17