Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug: cookies not being persisted #3012

Closed
1 of 4 tasks
IlCallo opened this issue May 29, 2020 · 18 comments
Closed
1 of 4 tasks

bug: cookies not being persisted #3012

IlCallo opened this issue May 29, 2020 · 18 comments
Labels
needs investigation needs further investigation before deciding

Comments

@IlCallo
Copy link

IlCallo commented May 29, 2020

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)

  • 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.

#2347 seems to report the same problem
#2831 seems to be related, but not sure

@IlCallo
Copy link
Author

IlCallo commented May 29, 2020

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();
  }

  // ...
}

@laurentgoudet
Copy link
Contributor

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.

@imhoffd imhoffd added the needs investigation needs further investigation before deciding label Feb 2, 2021
@leo-petrucci
Copy link

leo-petrucci commented Mar 25, 2021

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.

@malte94
Copy link

malte94 commented May 22, 2022

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.

@Aarbel
Copy link

Aarbel commented Jun 13, 2022

@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
Copy link

Aarbel commented Jun 13, 2022

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)

@leo-petrucci
Copy link

@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.

@malte94
Copy link

malte94 commented Jun 13, 2022

@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'
      }
  );

@AndriiSherman
Copy link

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

@jeromebon
Copy link

jeromebon commented Nov 7, 2022

We experience the same bug
#3012 (comment) solved it

@muuvmuuv
Copy link

muuvmuuv commented Oct 4, 2023

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 })

@markemer markemer closed this as not planned Won't fix, can't repro, duplicate, stale Jan 18, 2024
@Aarbel
Copy link

Aarbel commented Jan 22, 2024

@markemer was it solved ?

@jjang16
Copy link

jjang16 commented Jan 24, 2024

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.

#3012 (comment)

@Aarbel
Copy link

Aarbel commented Jan 24, 2024

Maybe because it's here now

image

@Aarbel
Copy link

Aarbel commented Jan 24, 2024

@markemer will you fix this problem ?

@mariusbolik
Copy link

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

@Aarbel
Copy link

Aarbel commented Jan 24, 2024

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

Copy link

ionitron-bot bot commented Feb 23, 2024

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 ionitron-bot bot locked and limited conversation to collaborators Feb 23, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
needs investigation needs further investigation before deciding
Projects
Development

No branches or pull requests