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

Cookie Persistence between Native Android and React JS Code #10254

Closed
Brian-Kaplan opened this issue Oct 5, 2016 · 1 comment
Closed

Cookie Persistence between Native Android and React JS Code #10254

Brian-Kaplan opened this issue Oct 5, 2016 · 1 comment
Labels
Resolution: Locked This issue was locked by the bot.

Comments

@Brian-Kaplan
Copy link

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

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<>();
    }
}
  1. 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
  2. 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
  3. 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

    fetch(REPORT_URL, {
      credentials: 'include',
      method: 'GET',
      headers: {
        'Accept': 'application/json',
        'Accept-Language': 'en_US',
      }
    }).then(response => {
          console.log(response);
          this.setReportResponse(response);
        }
      ).catch(function(err) {
        console.log('Fetch Error', err);
      });
  }

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

  • React Native version: 0.34.1
  • Platform(s) (iOS, Android, or both?): Android
  • Operating System (macOS, Linux, or Windows?): OSX Sierra
@Brian-Kaplan
Copy link
Author

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

@facebook facebook locked as resolved and limited conversation to collaborators May 24, 2018
@react-native-bot react-native-bot added the Resolution: Locked This issue was locked by the bot. label Jul 19, 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

2 participants