You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Adding credentials: 'same-origin' to fetch requests does not utilize OkHttp3 CookieJar implementation. I am not able to EASILY persist cookies between Android Activities and React Native Activities. A solution would be to create a wrapper class where both activities talk to the same persistent cookies store but i would think this is already built in.
Example Scenario:
Build an OkHttpClient with the builder and set the cookie jar
OkHttpClient mOkHttpClient = new OkHttpClient.Builder().cookieJar(MyCookieManager()).build();
Example impl of the CookieJar
public class MyCookieManager implements CookieJar
{
private List<Cookie> mCookies;
@Override
public void saveFromResponse(HttpUrl url, List<Cookie> cookies) {
this.mCookies = cookies;
}
@Override
public List<Cookie> loadForRequest(HttpUrl url) {
if (mCookies != null) {
return mCookies;
}
return new ArrayList<>();
}
}
Make an auth request to the server in Java Android Activity
OkHTTP client automatically calls MyCookieJar.saveFromResponse() to save the set-cookies the server gave us
Make a GET request to the server for a resource
OkHTTP client automatically calls MyCookieJar.loadForRequest(url) to get the set-cookies that were previously saved and adds them as headers
THIS PART DOES NOT WORK
Switch to a REACT activity and make the same GET request using fetch
Cookies should automatically be added to this request
Solution is to simply use the OkHTTP client that is created by React Native NetworkingModule. This client persists through the application and has its own CookieJar. Using fetch in javascript uses this client and is able to get and set cookies. If we simply use this same client instance to make requests in Native Java code everything works perfectly.
mHttpClient = new MyAuthClient(OkHttpClientProvider.getOkHttpClient(), this);
Issue Description
Adding
credentials: 'same-origin'
to fetch requests does not utilize OkHttp3 CookieJar implementation. I am not able to EASILY persist cookies between Android Activities and React Native Activities. A solution would be to create a wrapper class where both activities talk to the same persistent cookies store but i would think this is already built in.Example Scenario:
Build an OkHttpClient with the builder and set the cookie jar
OkHttpClient mOkHttpClient = new OkHttpClient.Builder().cookieJar(MyCookieManager()).build();
Example impl of the CookieJar
MyCookieJar.saveFromResponse()
to save the set-cookies the server gave usMyCookieJar.loadForRequest(url)
to get the set-cookies that were previously saved and adds them as headersSwitch to a REACT activity and make the same GET request using fetch
Cookies should automatically be added to this request
Steps to Reproduce / Code Snippets
See my stack overflow post
http://stackoverflow.com/questions/39861466/share-cookies-between-react-native-activity-and-native-android-activity-using-co
Expected Results
That fetch somehow is able to talk to the same OkHTTP3 client that is used to authenticate inside of an Android Java Activity
Additional Information
The text was updated successfully, but these errors were encountered: