Svein Fidjestøl
Svein Fidjestøl

Reputation: 3206

React Native fetch() losing cookie

I am using React Native fetch() in order to authenticate against a REST API which uses session cookies.

The session cookie received upon sign-in is automatically sent back with every request, and this works fine.

However, if I sign in and then quit the app, upon starting the app again the session cookie seems to have been lost.

How can I persist the sign-in cookie so it's stil there after an app unload?

Upvotes: 4

Views: 3444

Answers (2)

Pollizzio
Pollizzio

Reputation: 1042

Did you try?

fetch(url, {
  credentials: 'include'
})

Source

Upvotes: 0

Chris Geirman
Chris Geirman

Reputation: 9684

There are several cookie-based react-native components out there. One of these might be helpful to you.

I've heard of some solutions which use the webview to persist cookie-based authentication as well. But if you can, I suggest looking into a stateless method of auth persistence, something like JWT for instance. You can then cache the key in local storage.

In the end, it might be easiest if you just cache the cookie's session variable into local storage yourself, then manually inject the cookie header into your fetch() requests yourself on every request.

Upvotes: 2

Related Questions