Firebase Auth Error: The custom token format is incorrect. Please check the documentation

Viewed 40

I'm trying to re-authorize a user after changing the password. But I am getting this error

The custom token format is incorrect. Please check the documentation

What could be wrong?

I don't have a server. I use Firebase Database I do every step in order:

  1. Change the password first
  2. I update the token
  3. I use a token for re-authorization

still getting this error.

Sometimes I get an error, something like

You need to log in to change your password

but I'm already logged in. Then I logout and login, and try to change the password again, but I get the error again The custom token format is incorrect.

Here is the code for re-authorization, what is wrong?

private fun updatePassword(newPassword: String): Single<Boolean> {
    return Single.create { emitter ->
        Firebase.auth.currentUser?.updatePassword(newPassword)
            ?.addOnCompleteListener { task ->
                if (task.isSuccessful) {
                    emitter.onSuccess(true)
                } else {
                    emitter.onSuccess(false)
                }
            }
    }
}

private fun getNewToken(): Single<String> {
    return Single.create { emitter ->
        Firebase.auth.getAccessToken(true)
            .addOnCompleteListener { task ->
                if (task.isSuccessful) {
                    val token = task.result.token
                    emitter.onSuccess(token!!)
                } else {
                    emitter.onError(Throwable("Error when refresh Token"))
                }
            }
    }
}

private fun reAuthenticate(tokenId: String): Completable {
    return Completable.create { emitter ->
        Firebase.auth.signInWithCustomToken(tokenId)
            .addOnCompleteListener {
                if (it.isSuccessful) {
                    emitter.onComplete()
                } else {
                    emitter.onError(Throwable("Error"))
                }
            }
    }
}

private fun checkReAuthenticate() {
    val newPassword = "pass12345"

    updatePassword(newPassword)
        .flatMap {
            getNewToken()
        }
        .flatMapCompletable { token ->
            reAuthenticate(token)
        }
        .subscribe(
            {
                print("Success ReAuthenticated")
            },
            {
                print("Error when ReAuthenticate")
            }
        )
}
0 Answers
Related