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

getStateFromCookies tries to parse cookie value as JSON #46

Closed
szimek opened this issue Nov 2, 2017 · 3 comments
Closed

getStateFromCookies tries to parse cookie value as JSON #46

szimek opened this issue Nov 2, 2017 · 3 comments

Comments

@szimek
Copy link

szimek commented Nov 2, 2017

First of all, thanks a lot for this middleware!

I have 2 issues trying to set it up, though:

  1. The first one is that getStateFromCookies tries to parse cookie value as JSON (https://github.com/grofers/redux-cookies-middleware/blob/master/src/getStateFromCookies.js#L48), but I'm storing plain strings (e.g. JWT tokens) in my cookies. Why does it try to parse it as a JSON?

  2. The second one is related to initial state. Assuming that I've got paths for this middleware set up as:

const paths = {
  'auth.id_token': { name: 'auth_id_token' },
};

and I don't have any initial state (after all, I'm trying to read it from cookies), so I simply pass an empty object as the initial state:

const initialState = getStateFromCookies({}, paths);

it will break, because in https://github.com/grofers/redux-cookies-middleware/blob/master/src/getStateFromCookies.js#L42 it will get undefined as stateTree and it will try to call stateTree[terminalKey] (assuming that the right side, i.e. JSON parsing, actually succeeds). Couldn't it simply create these keys if they don't exist yet? Otherwise I need to model my initial state like this:

let initialState = {
  auth: { id_token: null },
};
initialState = getStateFromCookies(initialState, paths);

to prevent this issue.

@nimishsinghal
Copy link
Contributor

@szimek Sorry for the delay in reply. We are glad you found the middleware useful. Regarding your issues.

  1. Cookie value can be both an object as well as a string. I think JSON.parse will work in both the cases.
  2. Thanks for pointing out the issue. Will raise a PR to fix this.

@ashbhir
Copy link
Contributor

ashbhir commented Mar 6, 2019

#51 handles the above mentioned issues

@ashbhir
Copy link
Contributor

ashbhir commented Mar 25, 2019

Issue fixed with PR #51

@ashbhir ashbhir closed this as completed Mar 25, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants