Back

java.lang.IllegalStateException: User cancelled login

  • 1
  • Android
  • Accounts
  • Cloud
Bane
21 Jan, 2024, 19:40

I've implemented the oAuth Login with Google successfully and everything is working fine. Now when I tested all the error possibilities, I go something weird.

When the user starts the oAuth, it takes the user to the oAuth webpage. If the user presses back or gets back without completing the whole processes, then the app throws a java.lang.IllegalStateException with message User cancelled login . It occurs whenever user didn't complete the whole step. But when user completes all steps, the app works fine and there is no error.

I tried to catch the Exception, but couldn't. This triggers a Fatal Exception and crashes the app.

Here is the code I'm using -

TypeScript
    private suspend fun loginWithGoogle() {

        val account = Account(userModelRepository.client)

        withContext(Dispatchers.IO) {

            try {
                val createSessionDeferred = async {
                    val result = account.createOAuth2Session(
                        activity = this@LoginHome,
                        provider = "google"
                    )

                }

                // Wait for createOAuth2Session to complete
                createSessionDeferred.await()

                // check session method here
                userDetailsViewModel.getSession()

            } catch (e: AppwriteException) {
                e.printStackTrace()
            } catch (e: Exception) {
                e.printStackTrace()
            } catch (e: IllegalStateException) {
                e.printStackTrace()
            }

        }


    }
TL;DR
The developer is encountering a `java.lang.IllegalStateException` with the message "User cancelled login" when the user cancels the OAuth login process. The app crashes when this exception occurs and the developer is unable to catch it. To solve this issue, the developer can add a try-catch block specifically for the `java.lang.IllegalStateException`. This can be done by adding `catch (e: IllegalStateException)` in the existing try-catch block. Here's the updated code: ``` private suspend fun loginWithGoogle() { val account = Account(userModelRepository.client) withContext(Dispatchers.IO)
Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more