react-native icon indicating copy to clipboard operation
react-native copied to clipboard

Automatic cookies

Open cloud8421 opened this issue 11 years ago • 24 comments

Please feel free to correct me.

I've tried to use the fetch api to login against a service which sets a cookie in case of success.

The session cookie gets correctly set and subsequent requests to the same service pass authentication without me configuring anything at the code level, which makes me assume that cookies are automatically managed by the JavascriptCore runtime.

I can't find a way to manipulate them though, is that by design?

As a side note, does this mean that I'm just better off using a different authentication flow?

Thanks for your work on this!

cloud8421 avatar May 14 '15 11:05 cloud8421

They're automatically managed by the Objective-C network stack, which is used internally by fetch. We haven't exposed any explicit support for them though. This would need to be implemented as a native "RCTCookieJar" module.

nicklockwood avatar May 15 '15 02:05 nicklockwood

Thanks for the clarification, I'll keep it in mind.

cloud8421 avatar May 15 '15 15:05 cloud8421

How to get the cookie in client?

bakso avatar May 19 '15 17:05 bakso

@iostalk I've created a basic cookie manager that allows you to get/set cookies from react native: https://github.com/joeferraro/react-native-cookies

joeferraro avatar May 28 '15 04:05 joeferraro

Awesome @joeferraro

brentvatne avatar May 31 '15 22:05 brentvatne

thank you @joeferraro

dev-rkoshlyak avatar Jun 17 '15 18:06 dev-rkoshlyak

@joeferraro I'm trying to use your cookie manager, everything is in place, I require it successfully, but when I try to call any method on the object CookieManager it throws this error: Error: Cannot read property 'getAll' of undefined

This is my code: var React = require('react-native'); var CookieManager = require('react-native-cookies'); console.log(CookieManager); CookieManager.getAll((err, res) => { console.log('cookies!'); console.log(err); console.log(res); }); Is weird cause CookieManager is defined, since it gets logged properly in the console, with all its methods. Any idea where I went wrong?

xavibonell avatar Jun 26 '15 14:06 xavibonell

@xavibonell did you include the native RNCookieManagerIOS.h/m files in your Xcode project? It's not sufficient just to require the js.

nicklockwood avatar Jun 26 '15 16:06 nicklockwood

@joeferraro this is likely to be a common source of confusion. It might be a good idea to include an invariant inside your index.js to the effect of:

var invariant = require('invariant');
invariant(
   NativeModules.RNCookieManagerIOS,
   'Add RNCookieManagerIOS.h and RNCookieManagerIOS.m to your Xcode project.'
);

nicklockwood avatar Jun 26 '15 16:06 nicklockwood

@joeferraro I am faced with the same issue as @xavibonell Any clue anyone?

eyaleizenberg avatar Jul 09 '15 06:07 eyaleizenberg

For anyone curious about React Native cookie handling I made an example project that shows how to use cookie authentication. It makes use of the library from @joeferraro https://github.com/ryanmcdermott/react-native-login

ryanmcdermott avatar Sep 03 '15 16:09 ryanmcdermott

Thanks for sharing, @ryanmcdermott! Added it to the newsletter for this week

brentvatne avatar Sep 03 '15 18:09 brentvatne

@brentvatne Thank you I appreciate it!

ryanmcdermott avatar Sep 03 '15 18:09 ryanmcdermott

hi there,

I just wanted to check if my best case is to use https://github.com/joeferraro/react-native-cookies or if there is another way to simply clear / override the Cookie header in Fetch.

I'm using Fetch like this:

fetch(ENDPOINT, { method: 'GET', headers: { 'Accept': 'application/json', 'Content-Type': 'application/json', 'Cookie': user.session_name + '=' + user.sessid, } })

Which only works after the first visit, the first one seems to send double Cookie's because fetch has grabbed one from an initial login request and saved it. When it does the request it sends two sets of cookies which confuses the endpoint server and doesn't accept the connection. But after that because the automatic cookie is no longer there it then works. So basically I think if I can just either clear the automatic cookie before I run this call, or make sure in my call where I set cookie above that it is the ONLY one being set.

Do you think installing https://github.com/joeferraro/react-native-cookies and running clear cookies before this call will be my best bet?

Hope this makes sense.

Vince

vince-rowe avatar Jun 10 '16 11:06 vince-rowe

I am wondering the same thing as @vince-rowe. I don't use cookies, but fetch is using them in the background and I would just like to delete or override them in fetch without installing a third party library.

kristojorg avatar Jul 05 '16 21:07 kristojorg

I also want to find an easy solution to disable cookies at all.

ms88privat avatar Jul 06 '16 12:07 ms88privat

@ms88privat @kristojorg by default, fetch() does not send cookies.

If you're still having issues, you may want to try

fetch( ... {
...
credentials: 'omit'
})

hbd avatar Sep 06 '16 03:09 hbd

@hbd I don't need it anymore, but I beleave, it wasn't the case. I also tried the credential property and so on. Like they said, it is managed by objective-c and not the fetch API

ms88privat avatar Sep 06 '16 19:09 ms88privat

two sets of cookie troubles me... wish a solution.

chengmu avatar Sep 14 '16 03:09 chengmu

@ms88privat Have you found the solution to disable cookies. I have the same problem."credentials: 'omit'" dosen't work

haiyangjiajian avatar Oct 20 '16 11:10 haiyangjiajian