joeferraro / react-native-cookies

Cookie manager for React Native
MIT License
788 stars 315 forks source link

Sometimes my cookies are not used in one request but used in the next one. #103

Open Alanz2223 opened 6 years ago

Alanz2223 commented 6 years ago

Hello, I am experiencing a hard to debug issue with RN cookies, I have a standard redux app and at app startup I have a redux-saga check whether the user is authenticated or transfer them into the next screen or have them log in. I am using axios to make all my requests. My checkAuth saga looks like this

` export function* checkAuth() { try { let cookie = yield call(CookieManager.get, AC.IS_USER_URL);

    if(cookie && cookie.hasOwnProperty("this.sid"))
    {
        //this means the user has an existing cookie
        //lets check if its still valid
        var res = yield call(API.checkIfUser, cookie);

      //check the response to see if user is authenticated

      }
    }
   catch(e)
    {
        //console.log error
    }
}`

Its a simple saga however I've noticed that there are times where the cookie variable is not retrieved correctly, and it simply returns an empty object even though the cookies were set from setFromResponse in previous login events. After some research I found an article where a group is also encountering a similar issue where their cookies are not persisted and they claim that it has something to do with the native implementations of the cookie managers. The article could be found here

but an excerpt of it

A few weeks ago, we ran into a particularly tricky bug: some users told us that our mobile app was logging them out every time they closed the app.

We couldn’t figure out why the invalid cookies were getting sent until we stumbled across the core issue: the cookie storage provided to React Native by the underlying native frameworks isn’t 100% reliable.

fetch on React Native is implemented on top of the native networking stacks (Objective-C on iOS, and Java on Android). Cookies are automatically managed by these native stacks rather than by React Native itself [GitHub][StackOverflow].

This would explain why I've also tried to workaround this by manually managing the cookies with AsyncStorage and passing them to axios and fetch by either setting the cookies manually every time I make a request, but it sends different cookies. I am unsure whether there may be a workaround for this or whether this may be a core issue of the react native cookie managers and thus by extension, this library.

ps-sorry for the code formatting, the editor is not being kind with me.

safaiyeh commented 4 years ago

I have forked the repo here: https://github.com/safaiyeh/react-native-cookie-store We can create issues and PRs there to continue the development of the project.