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

[Android] Cookies are not persisted across network requests #2792

Closed
h87kg opened this issue Sep 17, 2015 · 24 comments
Closed

[Android] Cookies are not persisted across network requests #2792

h87kg opened this issue Sep 17, 2015 · 24 comments
Assignees
Labels
Resolution: Locked This issue was locked by the bot.

Comments

@h87kg
Copy link

h87kg commented Sep 17, 2015

Hi,

When send a GET request, tell me to login, but it's really logged in. The issue only on Android.

@mkonicek
Copy link
Contributor

Hey @h87kg, can you clarify "tell me to login"? Where are you sending a GET request? Can you share your code?

@h87kg
Copy link
Author

h87kg commented Sep 17, 2015

  1. logged in successfully after invoke new AccountService.login()
  2. failed without authorization after invoke new FlightService().searchFlights()
class AccountService extends Service {
  login(username, password) {
    var formData = new FormData();
    formData.append('loginName', username);
    formData.append('pwd', password);
    return fetch(`${this.URL}/sme/employee/login.json`, {
      method: 'POST',
      body: formData,
    })
    .then(response => response.json());
  }
};
class FlightService extends Service {
  searchFlights(fromCity, toCity, depDate) {
    return fetch(`${this.URL}/sme/flight.json?depCityCode=${fromCity.flightCityCode}&arrCityCode=${toCity.flightCityCode}&depDate=${DateFilter.format(depDate, 'yyyy-MM-dd')}&forBusiness=Y&isCivilServants=N`)
    .then(response => response.json());
  }
}

@h87kg
Copy link
Author

h87kg commented Sep 17, 2015

I found the second request didn't sent the cookie info, such as:

Cookie:      PHPSESSID=jciun1hfmsqcvivvpvdbd2adp2
Cookie2:     $Version=1
POST /sme/employee/login.json

sent:

Content-Length:  32
Content-Type:    application/x-www-form-urlencoded
Host:            
Connection:      Keep-Alive

received:

Server:             nginx/1.0.15
Content-Type:       application/json;charset=UTF-8
Transfer-Encoding:  chunked
Connection:         keep-alive
Vary:               Accept-Encoding
X-Powered-By:       PHP/5.4.37
Set-Cookie:         PHPSESSID=gsdv1qgf3togdmql26pkuesiv7; path=/
Cache-Control:      no-cache
Date:               Thu, 17 Sep 2015 11:57:02 GMT
Content-Encoding:   gzip
GET /sme/flight.json?depCityCode=BJS&arrCityCode=CTU&depDate=2015-09-18&forBusiness=Y&isCivilServants=N

sent:

Host:            
Connection:       Keep-Alive
Accept-Encoding:  gzip
User-Agent:       okhttp/2.4.0

received:

Server:             nginx/1.0.15
Content-Type:       application/json;charset=UTF-8
Transfer-Encoding:  chunked
Connection:         keep-alive
Vary:               Accept-Encoding
X-Powered-By:       PHP/5.4.37
Set-Cookie:         PHPSESSID=bu8drc39vp7rkr90j3i5eji7j1; path=/
Cache-Control:      no-cache
Date:               Thu, 17 Sep 2015 11:57:06 GMT
X-Debug-Token:      22f42f
Content-Encoding:   gzip

@ide ide changed the title [Android] the network session could not be shared [Android] Cookies are not persisted across network requests Sep 17, 2015
@mkonicek
Copy link
Contributor

Thanks for the detailed report! I think @andreicoman11 might know about cookies.

@jhen0409
Copy link
Contributor

Maybe and this related: https://github.com/github/fetch#sending-cookies

@mbrock
Copy link
Contributor

mbrock commented Sep 24, 2015

credentials: 'same-origin' doesn't help on Android, but works fine on iOS.

This can be easily tested by requesting http://httpbin.org/cookies/set?foo=bar and then http://httpbin.org/cookies.

The Set-Cookie header is also hidden from the headers object so there seems to be no way of using cookies on Android.

@arsham-f
Copy link

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.

@CCoffie
Copy link

CCoffie commented Oct 14, 2015

+1

@mkonicek
Copy link
Contributor

Looks like credentials: 'same-origin' should indeed work: https://github.com/github/fetch#sending-cookies.

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

@dizlexik
Copy link
Contributor

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 OkHttpClientProvider.java by adding:

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.

@mkonicek
Copy link
Contributor

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.

@lexs
Copy link
Contributor

lexs commented Nov 23, 2015

I just landed 274c5c7 which should match iOS behavior (when we have WebView)

lexs added a commit that referenced this issue Nov 23, 2015
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
@andriniaina
Copy link

Hello, is this available in v0.15 or 16 ?

@ide
Copy link
Contributor

ide commented Nov 24, 2015

It will be available in 0.17.

@mkonicek
Copy link
Contributor

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:
https://github.com/facebook/react-native/compare/0.15-stable

@ide
Copy link
Contributor

ide commented Nov 24, 2015

Oops didn't realize 0.16 was cut so recently.

@andriniaina
Copy link

I just tested it. It's working as expected. Thanks 👍 The bug was really a showstopper.

@johnckendall
Copy link

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)

@andriniaina
Copy link

I had the same issue at first when I tried to upgrade my project. But it worked after starting from a new project.

@eddiefletchernz
Copy link

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

See: https://github.com/github/fetch#sending-cookies

sunnylqm pushed a commit to sunnylqm/react-native that referenced this issue Dec 2, 2015
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
@szq4119
Copy link

szq4119 commented Dec 11, 2015

@johnckendall
Copy link

Thought I already replied but I don't see it so posting this FYI:
Thanks guys, after fulling upgrading the project and resolving issues this now appears to be working as it should.

Thanks for the fix!

Crash-- pushed a commit to Crash--/react-native that referenced this issue Dec 24, 2015
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
@atom992
Copy link

atom992 commented May 1, 2016

@johnckendall which version of react-native are you upgrade?

rozele referenced this issue in microsoft/react-native-windows May 25, 2016
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
@Brian-Kaplan
Copy link

Brian-Kaplan commented Oct 5, 2016

@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
http://stackoverflow.com/questions/39861466/share-cookies-between-react-native-activity-and-native-android-activity-using-co

I opened a new issue
#10254

@facebook facebook locked as resolved and limited conversation to collaborators Jul 21, 2018
@react-native-bot react-native-bot added the Resolution: Locked This issue was locked by the bot. label Jul 21, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Resolution: Locked This issue was locked by the bot.
Projects
None yet
Development

No branches or pull requests