0

I have a class that observes a user account. It starts observing it when the app starts. When the account changes, it sets the cookie in a cookie manager with setCookie.

When I start the app and the account is already there, it properly injects the cookie - when I open the web view I see it there.

When I start the app and the account is not there, nothing happens (that's expected), then I log in (this creates the account) and the code for injecting cookies is called (the callback of the setCookie says that the cookie was correctly injected). But when I open the web view, I do not see the cookie there. If I restart the app everything works again.

I tried many different configurations of web views and cookie manager. I tried call flush on a cookie manager. I tried setting acceptCookies, acceptThirdPartyCookies. On my web view I need to have javascript enabled and dom storage enabled.

This is how I set cookies:

fun injectCookie(account: Account) {
    val email = account.accountEmail ?: return
    val accessCode = account.accessCode ?: return
    val cookie = buildCookie(email, accessCode, DOMAIN)

    val manager = CookieManager.getInstance()
//    manager.setAcceptCookie(true)
    manager.setCookie(DOMAIN, cookie) { Log.d("COOKIE - auth", it.toString()) } // this prints true!
//    manager.flush() // I tried with and without flushing. I expect it to work without flush. 
}

I expected my cookies to be visible in web views immediately after the setCookie prints true but that's not happening in the cases when the user opens the app and the account is not there. Once the user logs in and the setCookie code is called it still doesn't work. But after restarting the app, everything works.

Almost like something at the beginning of the app prevents the app to set cookies. It works when I do it very early in the app lifecycle but not later.

Any ideas what could be the problem?

4

0 回答 0