-
Notifications
You must be signed in to change notification settings - Fork 24.3k
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
[Android] Cookies are not persisted across network requests #2792
Comments
Hey @h87kg, can you clarify "tell me to login"? Where are you sending a GET request? Can you share your code? |
|
I found the second request didn't sent the cookie info, such as:
|
Thanks for the detailed report! I think @andreicoman11 might know about cookies. |
Maybe and this related: https://github.com/github/fetch#sending-cookies |
This can be easily tested by requesting The |
I'm having this same issue. Apparently the network stack on iOS automatically retains the cookies, but Android doesn't. This is a pretty big showstopper because you can't keep any sessions with remote APIs. |
+1 |
Looks like The way the cookies are handled should probably respect the same origin policy? What does the spec say? Might be just not implemented yet. Will check with @andreicoman11. Would be awesome if you can point to the Java file where we are not persisting the cookies :) |
Yeah I agree with @arsham-f, this is a pretty big showstopper as I am working on a project right now with a requirement to use a session-based API. I looked into it a bit and, likely naively, attempted to add cookie support in CookieManager cookieManager = new CookieManager();
cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
sClient.setCookieHandler(cookieManager); Didn't work, but maybe I was on the right track and someone can run with that. |
Thanks for trying @dizlexik. I agree this is quite high-pri, hopefully we can find bandwidth to fix it soon or someone can send a PR. |
I just landed 274c5c7 which should match iOS behavior (when we have WebView) |
Summary: This adds a persistent cookie store that shares cookies with WebView. Add a `ForwardingCookieHandler` to OkHttp that uses the underlying Android webkit `CookieManager`. Use a `LazyCookieHandler` to defer initialization of `CookieManager` as this will in turn trigger initialization of the Chromium stack in KitKat+ which takes some time. This was we will incur this cost on a background network thread instead of during startup. Also add a `clearCookies()` method to the network module. Add a cookies example to the XHR example. This example should also work for iOS (except for the clear cookies part). They are for now just scoped to Android. Closes #2792. public Reviewed By: andreicoman11 Differential Revision: D2615550 fb-gh-sync-id: ff726a35f0fc3c7124d2f755448fe24c9d1caf21 Conflicts: ReactAndroid/src/main/java/com/facebook/react/modules/network/NetworkingModule.java
Hello, is this available in v0.15 or 16 ? |
It will be available in 0.17. |
It made the 0.16 cut and I even cherry-picked it to 0.15, give it a try :) See the history for the 0.15 branch: |
Oops didn't realize 0.16 was cut so recently. |
I just tested it. It's working as expected. Thanks 👍 The bug was really a showstopper. |
Should this solve the issue where the SESSION cookies aren't being sent back to the server? If so, it doesn't seem to be working for us... Or maybe we don't understand the build process; is there some other step to rebuild for Android after upgrading to 0.15 (other than react-native run-android) ? (a wireshark grab does show the JSESSIONID coming to the client but not getting returned on a subsequent GET) (also, we verified that the new version of NetworkingModule.java is present) |
I had the same issue at first when I tried to upgrade my project. But it worked after starting from a new project. |
Mine started working with session cookies straight away after upgrading. Did you also run react-native upgrade after npm installing the new version? Also make sure you are passing the option "credentials" to "same-origin" on all requests through fetch |
Summary: This adds a persistent cookie store that shares cookies with WebView. Add a `ForwardingCookieHandler` to OkHttp that uses the underlying Android webkit `CookieManager`. Use a `LazyCookieHandler` to defer initialization of `CookieManager` as this will in turn trigger initialization of the Chromium stack in KitKat+ which takes some time. This was we will incur this cost on a background network thread instead of during startup. Also add a `clearCookies()` method to the network module. Add a cookies example to the XHR example. This example should also work for iOS (except for the clear cookies part). They are for now just scoped to Android. Closes facebook#2792. public Reviewed By: andreicoman11 Differential Revision: D2615550 fb-gh-sync-id: ff726a35f0fc3c7124d2f755448fe24c9d1caf21
Thought I already replied but I don't see it so posting this FYI: Thanks for the fix! |
Summary: This adds a persistent cookie store that shares cookies with WebView. Add a `ForwardingCookieHandler` to OkHttp that uses the underlying Android webkit `CookieManager`. Use a `LazyCookieHandler` to defer initialization of `CookieManager` as this will in turn trigger initialization of the Chromium stack in KitKat+ which takes some time. This was we will incur this cost on a background network thread instead of during startup. Also add a `clearCookies()` method to the network module. Add a cookies example to the XHR example. This example should also work for iOS (except for the clear cookies part). They are for now just scoped to Android. Closes facebook#2792. public Reviewed By: andreicoman11 Differential Revision: D2615550 fb-gh-sync-id: ff726a35f0fc3c7124d2f755448fe24c9d1caf21
@johnckendall which version of react-native are you upgrade? |
Summary: This adds a persistent cookie store that shares cookies with WebView. Add a `ForwardingCookieHandler` to OkHttp that uses the underlying Android webkit `CookieManager`. Use a `LazyCookieHandler` to defer initialization of `CookieManager` as this will in turn trigger initialization of the Chromium stack in KitKat+ which takes some time. This was we will incur this cost on a background network thread instead of during startup. Also add a `clearCookies()` method to the network module. Add a cookies example to the XHR example. This example should also work for iOS (except for the clear cookies part). They are for now just scoped to Android. Closes #2792. public Reviewed By: andreicoman11 Differential Revision: D2615550 fb-gh-sync-id: ff726a35f0fc3c7124d2f755448fe24c9d1caf21
@andreicoman11 @lexs I would like to use this feature but I do requests in both Java Android code and JS React Code. Currently I'm able to use the CookieJar class with OkHTTP3 in Java to easily get and set cookies for me in Native Android code. i.e. Auth request gives me set cookies (automatically saved) and get request automatically adds the cookies as headers for me. Is it possible that in JS code I can access that same CookieJar (using fetch API) without having to add a bridging wrapper class. It seems like there is no way to seamlessly persist cookies between JS and Java when using React Native. A friend of mine was able to easily do this on iOS Here is a stack overflow post I made with more details I opened a new issue |
Hi,
When send a GET request, tell me to login, but it's really logged in. The issue only on Android.
The text was updated successfully, but these errors were encountered: