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

Webview android not saving cookies #2643

Closed
h3thal opened this issue Aug 17, 2022 · 21 comments
Closed

Webview android not saving cookies #2643

h3thal opened this issue Aug 17, 2022 · 21 comments

Comments

@h3thal
Copy link

h3thal commented Aug 17, 2022

y to log in from the webview on Android, after the next entry in the application, you must log in again. IOS doesn't have this problem.

Create a webview component specify a link to any site with authorization, for example github. specify parameters for the operation of cookies (sharedCookiesEnabled={true},thirdPartyCookiesEnabled={true}).
Then go to the application and complete the authorization. After authorization, restart the application and check authorization

It will be possible to continue working under an authorized user as on iOS

If applicable, add screenshots to help explain your problem.

Environment:

  • OS: Android
  • OS version: 11
  • react-native version: 0.68.2
  • react-native-webview version: 11.18.1
@allan-simon
Copy link

does it work when you switch to an other "real page" (i.e not just navigating inside a single page app)

we've seen on our side that on our multipage app we were able to reproduce this when we stayed on the same page

@KoalaSat
Copy link

KoalaSat commented Sep 18, 2022

Hello, I have the same issue on android, after running inside webview:

document.cookie = `${key}=${value}`;

Checking document.cookie again returns ""

I'm setting:

<WebView
        source={{
          uri: 'file:///android_asset/Web.bundle/index.html',
        }}
        sharedCookiesEnabled={true}
        thirdPartyCookiesEnabled={true}

@KoalaSat
Copy link

KoalaSat commented Sep 18, 2022

Testing it with https://developer.mozilla.org/en-US/docs/web/api/document/cookie actually works, so looks like it's related with using file:///

Injecting doesn't work neither:

webViewRef.current?.injectJavaScript(
      `
        (function() {
          document.cookie = "test1=Hello";
          console.log(document.cookie) // ---> ''
        })();
      `,
    );

@github-actions
Copy link

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 github-actions bot added the Stale label Nov 18, 2022
@allan-simon
Copy link

up

@KoalaSat
Copy link

KoalaSat commented Nov 23, 2022

For those with extra time or hurries, I solved it by using JS injection and EncryptedStorage.

Short description of the proposal RoboSats/robosats#258

And some key points on our implementation

  1. Send cookie data to native: https://github.com/Reckless-Satoshi/robosats/blob/main/frontend/src/services/System/SystemNativeClient/index.ts#L32
  2. Receiving, storing the data and injecting it back to JS: https://github.com/Reckless-Satoshi/robosats/blob/main/mobile/App.tsx#L107
  3. Receiving the data back and storing it on memory to avoid data inconsistencies and async read: https://github.com/Reckless-Satoshi/robosats/blob/main/frontend/src/services/Native/index.ts#L42

Extra hint: recovering stored data on start: https://github.com/Reckless-Satoshi/robosats/blob/main/mobile/App.tsx#L34

@github-actions github-actions bot removed the Stale label Nov 24, 2022
@github-actions
Copy link

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 github-actions bot added the Stale label Jan 24, 2023
@allan-simon
Copy link

up

@github-actions github-actions bot removed the Stale label Jan 25, 2023
@github-actions
Copy link

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 github-actions bot added the Stale label Mar 26, 2023
@allan-simon
Copy link

up

@github-actions github-actions bot removed the Stale label Mar 27, 2023
@rohanbasukala8848
Copy link

UP!!!!!

@kj-ahn-n
Copy link

kj-ahn-n commented Jun 1, 2023

In my case, the issue was resolved with the following code modification.

https://github.com/react-native-webview/react-native-webview/pull/2975/files

@IsaacConDosA
Copy link

any solution?

@github-actions
Copy link

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 github-actions bot added the Stale label Sep 11, 2023
@rohanbasukala8848
Copy link

searching for solutions!
UP

@github-actions github-actions bot removed the Stale label Sep 12, 2023
@aminta
Copy link

aminta commented Sep 14, 2023

up!!!

@manueltg89
Copy link

up!

@laogui
Copy link

laogui commented Nov 28, 2023

up! v13.6.3 still not work on android.

@Mokdasoo
Copy link

Mokdasoo commented Dec 27, 2023

I am sharing the temporary solution that solved my problem.

Environment Overview:

The environment of my app is an app that provides the entire service by displaying one URL through a single react native webview.

Encountered Problem:

  1. Implement login logic with cookies - Login (save cookies), logout (remove cookies), automatic login (determine the presence of cookies when running the app)
  2. Works well on web and iOS.
  3. If you close the app immediately after logging in or logging out on Android and then run the app again, cookies remain or are not saved, causing a problem where you remain logged in even after logging out or are unable to automatically log in even after logging in.
  4. If you log in or log out and leave the app open for 5 minutes, close it and run it again, the cookies will be reflected well.

Identified Cause:

The reason is that in web views of certain versions of Android or higher, cookies are automatically synchronized every 5 minutes, but when less than 5 minutes have passed, the synchronization was not done and the cookies were not reflected.

Temporary Solution:

The solution was to manually synchronize cookies in the Android Webview when cookie-related logic was executed on my web.
My easiest solution is to use the CookieManager.flush() method from the @react-native-cookies/cookies library.
When cookie logic is executed on the web, you can set it to send communication to the Webview, receive an event from Webview onMessage, and execute CookieManager.flush(). You can then synchronize manually.

Cookies are applied well after synchronization.

Copy link

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

@DSp4wN
Copy link

DSp4wN commented May 23, 2024

UP

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests