react-native-webview icon indicating copy to clipboard operation
react-native-webview copied to clipboard

Cookies are lost on iOS after application closing

Open alex-mironov opened this issue 7 years ago • 22 comments

Cookies seems to be lost after closing an application. Is it expected behaviour?

Environment:

  • OS: iOS
  • react-native version: 0.57.7
  • react-native-webview version: 5.2.4

alex-mironov avatar Apr 24 '19 19:04 alex-mironov

Having the same problem on Android.

Environment:

  • OS: Android
  • react-native version: 0.59.5
  • react-native-webview version: 5.7.0

gorvinsky avatar May 02 '19 03:05 gorvinsky

having the same problem on both android and ios

react native - .57.8 react-native-webview version - 5.3.0

GokulDharumar avatar May 15 '19 14:05 GokulDharumar

Same :/

wilderuncat avatar Jun 03 '19 03:06 wilderuncat

I have the same Issue, I guess. The Cookies are not lost directly after closing the App (swiping the App up in iOS) or after a restart of the Phone. But after some Time. The Cookie Lifetime is set to 10 Yrs, but the User gets logged out after Days/Weeks.

I wrote about the Problem here: https://stackoverflow.com/questions/56440239/react-native-webview-user-gets-logged-out-despite-10-year-login-cookie

nuriye avatar Jun 04 '19 08:06 nuriye

Seems like cookie used to be persisted. Maybe this bug appeared just recently? https://github.com/react-native-community/react-native-webview/issues/207#issuecomment-498321766

BettyJJ avatar Jun 13 '19 10:06 BettyJJ

Any updates regarding this, it appears to happen at random but sometimes even after just a few hours.

JustPassingThrough avatar Jul 24 '19 19:07 JustPassingThrough

Hello 👋, this issue has been opened for more than 2 months with no activity on it. If the issue is still here, please keep in mind that we need community support and help to fix it! Just comment something like still searching for solutions and if you found one, please open a pull request! You have 7 days until this gets closed automatically

github-actions[bot] avatar Sep 23 '19 00:09 github-actions[bot]

Any progress on this? There is no cookie issue in android. But in iOS, on closing the app, the cookie is getting automatically cleared it seems and the user will be landed on login screen on reopening the app, which is not acceptable.

anurag060 avatar Oct 31 '19 05:10 anurag060

You need to ensure that the cookies have an expiry date set on the server-side. Cookies without an expiry date are considered 'session only' and will get cleared when you restart the app.

That worked for me

texmex44 avatar Apr 14 '20 13:04 texmex44

We had a bug that sometime clearing PHPSESSID (which is a session only cookie) cleared the auth as well, resolving it fixed the issue indeed

vanyasem avatar May 26 '20 10:05 vanyasem

We also experience this problem

daanpape avatar May 27 '20 12:05 daanpape

i have resolved issue and tested with php. you can check my solution here: https://stackoverflow.com/a/62071590/8079868

numandev1 avatar May 31 '20 19:05 numandev1

This may sound silly but I was facing same issue just today. The session was persisting on Android but not on IOS so I easily believed it was an issue with IOS.

but after hours of debugging, I realized it was my own mistake. I forgot to declare session_start() in my php file. But I'm still confused how android managed to work fine without that declaration.

adesh1167 avatar Mar 12 '24 12:03 adesh1167

Is there any solution for case when I don't have the ability to fix that on the server?

Joedmin avatar May 22 '24 13:05 Joedmin

Firstly, try to explain what exactly is the behavior you want so we can know if using cookies is the best option in the first place. Cookies might function perfectly in some scenarios for a website but might not be the case for an app.

adesh1167 avatar May 22 '24 13:05 adesh1167

I am making a mobile application for a website, that does not have one and since I cannot reverse engineer websocket comminucation to use the API, all I can do is to just use WebView and rely on it and the website to handle login cookies. The thing is that currently, the WebView will delete all of the website's cookies so I have to login each time after restarting the application.

Joedmin avatar May 22 '24 15:05 Joedmin

The issue is still there on android it keeps logging me out fir certain amount of time on first install whenever I kill the app and after certain time it starts working properly

Packages: "expo": "51.0.8", "react": "18.2.0", "react-native": "0.74.1", "react-native-gesture-handler": "~2.16.1", "react-native-webview": "^13.8.6"

DSp4wN avatar May 23 '24 14:05 DSp4wN

I believe you already generate authentication token upon login. This authentication token is stored in the app's async storage. A copy of the token is also stored on the server along with the id of the account associated with it.

Whenever the app is opened again after being closed, the token is fetched from the async storage and a request is sent to the server with it. On the server, you'll query the database with the token to know if it exists and you'll fetch the user id associated with it. You'll then create a new session. The php file doing the checks and creating the session must have session_start() at the top. You'll initiate the session by setting $_SESSION['id'] to the user's id (or any other identifier you use)

With this, subsequent calls made from the app to the server will keep that same $_SESSION until the app is closed.

So in my case, the only mistake I made was omitting session_start() at the top of my php file. This made me logged in for first few API calls from the app then I noticed I wasn't logged in again on subsequent calls.

adesh1167 avatar May 23 '24 20:05 adesh1167

Why is this issue closed? I have the same problem. Cookies are gone from webview after app has been in background for a while!

smohadjer avatar Oct 17 '24 14:10 smohadjer

Same here.

  1. Cookies are gone from Webview not only when you've been on the background for a while.
  2. Cookies are gone if you shut down an app immediately after setting a cookie.

Point two can be resolved by just waiting for 20 seconds or more, I guess cookie is being preserved with some delay, like described here

duskload avatar Oct 30 '24 13:10 duskload