capacitor icon indicating copy to clipboard operation
capacitor copied to clipboard

bug: cookies not being persisted

Open IlCallo opened this issue 4 years ago • 9 comments

Bug Report

Capacitor Version

npx cap doctor output: @capacitor/ios not installed @capacitor/cli 1.5.2 @capacitor/core 1.5.2 @capacitor/android 2.1.0

Affected Platform(s)

  • [x] Android
  • [ ] iOS
  • [ ] Electron
  • [ ] Web

Current Behavior

Android native Cookies management persists cookies only after a given amout of time. This causes problems when an auth session cookie or remember me cookie are used and the application is destroyed shortly after the login (or any other action creating a cookie) is performed. When re-started, we won't have that cookie available.

A similar problem is present on iOS too, but haven't yet tried it out (link at the end of the issue).

Expected Behavior

Cookies shall be forcefully persisted when onPause or onStop is called, to be sure to avoid data loss in case the app is later destroyed (by the user or by the system).

Sample Code or Sample Application Repo


Reproduction Steps

  • perform a request to a server which you know will set a cookie
  • hard-close the app right after
  • re-open the app
  • check via chrome devtools that the cookie isn't present

Other Technical Details

yarn --version output: 1.22

node --version output: 13.x

Other Information

This article explain the problem pretty well.

https://github.com/ionic-team/capacitor/issues/2347 seems to report the same problem https://github.com/ionic-team/capacitor/issues/2831 seems to be related, but not sure

IlCallo avatar May 29 '20 14:05 IlCallo

Simply adding the following to my MainActivity.java fixed the problem. Could probably be onStop instead, but I'm not sure if Android persist cookies even when the app is paused

import android.webkit.CookieManager;

public class MainActivity extends BridgeActivity {
  // ...

  @Override
  public void onPause() {
    super.onPause();

    CookieManager.getInstance().flush();
  }

  // ...
}

IlCallo avatar May 29 '20 14:05 IlCallo

I can confirm that @IlCallo solution indeed solves the problem, but shouldn't this be achieved by Capacitor itself? Or at least be part of Android template for new projects? As the current behavior (cookie state not being immediately persisted on Android) is definitely unexpected.

laurentgoudet avatar Feb 02 '21 09:02 laurentgoudet

Yep, just had this happen to me. I released another Capacitor App last year and the Android Cookies were being persisted correctly, but were reset on iOS. That was fixed somewhere server-side.

Unfortunately something must've changed with Android and now the cookies are being reset on closing the app. Adding the code in the OP seems to fix it for now.

leo-petrucci avatar Mar 25 '21 11:03 leo-petrucci

Simply adding the following to my MainActivity.java fixed the problem. Could probably be onStop instead, but I'm not sure if Android persist cookies even when the app is paused

import android.webkit.CookieManager;

public class MainActivity extends BridgeActivity {
  // ...

  @Override
  public void onPause() {
    super.onPause();

    CookieManager.getInstance().flush();
  }

  // ...
}

Thank you. This should be implemented by default definitely.

malte94 avatar May 22 '22 14:05 malte94

@malte94 @creativiii

did you fully solved the problem on both iOS and Android ?

What do you mean by OP ? Can you give some examples ?

Thanks a lot for your help

Aarbel avatar Jun 13 '22 09:06 Aarbel

Having the problem with Auth0 cookies, our users disconnect after 2 days on iOS and Android (seems like they are not persisted after this time)

Aarbel avatar Jun 13 '22 09:06 Aarbel

@malte94 @creativiii

did you fully solved the problem on both iOS and Android ?

What do you mean by OP ? Can you give some examples ?

Thanks a lot for your help

As mentioned, I added the code posted by @IlCallo to my Android project and it fixed the issue. I haven't dared removing it since, so I have no idea if the problem is fixed or not.

leo-petrucci avatar Jun 13 '22 10:06 leo-petrucci

@Aarbel

Since the user disconnects after 2 days, this does not seem to be related to having no persistency at all, like discussed here.

If you use a session cookie, set an expiration date.

  const token = jwt.sign({ id: u._id }, process.env.TOKEN,
      {
          expiresIn: '120d'
      }
  );

malte94 avatar Jun 13 '22 10:06 malte94

@IlCallo solution worked perfectly for me! Really don't understand, why capacitor is not handling it

AndriiSherman avatar Aug 30 '22 12:08 AndriiSherman

We experience the same bug https://github.com/ionic-team/capacitor/issues/3012#issuecomment-636017770 solved it

jeromebon avatar Nov 07 '22 16:11 jeromebon

For us it was fixed by setting a correct expires date, we always just had "session"

const expires = dayjs().add(7, "days").toDate().toUTCString()
CapacitorCookies.setCookie({ key: "identifier", value: "...", expires })

muuvmuuv avatar Oct 04 '23 14:10 muuvmuuv

@markemer was it solved ?

Aarbel avatar Jan 22 '24 10:01 Aarbel

We were having a same issue in Capacitor 5 Android. The solution above solved it. Surprising it's 2024 and the issue is still there.

https://github.com/ionic-team/capacitor/issues/3012#issuecomment-636017770

jjang16 avatar Jan 24 '24 04:01 jjang16

Maybe because it's here now

image

Aarbel avatar Jan 24 '24 07:01 Aarbel

@markemer will you fix this problem ?

Aarbel avatar Jan 24 '24 07:01 Aarbel

The team's official explanation is that Capacitor cookies should not be used because they are only intended for the web: https://forum.ionicframework.com/t/cookies-vs-preferences-what-is-more-persistent/236820/4?u=mariusbolik

mariusbolik avatar Jan 24 '24 12:01 mariusbolik

@mariusbolik thanks a lot, tools like auth0 use the browser cookies for example, so how to fix the problem ?

Aarbel avatar Jan 24 '24 12:01 Aarbel

Thanks for the issue! This issue is being locked to prevent comments that are not relevant to the original issue. If this is still an issue with the latest version of Capacitor, please create a new issue and ensure the template is fully filled out.

ionitron-bot[bot] avatar Feb 23 '24 13:02 ionitron-bot[bot]