0

I'm facing an issue where I can't save the localStorage across redirects. Here is my code:

// Call newSitelogin(). Set response(credentials) to localStorage. Redirect to new url.
newSitelogin({ uuid, password })
  .then(() => {
    window.open(process.env.NEW_SITE_URL); // This line clears localStorage.
  })
  .catch(() => replaceRouteWith('/'));

Endpoint to call the api and set the response in localStorage

newSitelogin(payload) {
  return yield put.resolve({
      [RSAA]: {
        endpoint: `/auth/hubv3-login`,
        method: 'POST',
        body: JSON.stringify(payload),
        types: [
          ACTION.LOGIN_REQUEST_PERFORM,
          {
            type: ACTION.LOGIN_REQUEST_SUCCESS,
            payload: (action, state, res) =>
              getJSON(res).then(json => localStorage.setItem('credentials', JSON.stringify(json))), // setting response to localStorage.
          },
          ACTION.LOGIN_REQUEST_FAILURE,
        ],
      },
  });
};

I can see that the credentials are rightly stored in the localStorage of my application: enter image description here

But they are not preserved when i redirect to a new url using window.open(process.env.NEW_SITE_URL).

enter image description here

How do i make sure that the credentials object is carried over when redirecting to a new url? I cannot use sessions and cookies due to the logic written in the app that i'm redirecting to. It only looks for credentials in localStorage.

saran3h
  • 7,930
  • 2
  • 23
  • 33

1 Answers1

1

I have faced this issue in the past. localStorage is only preserved per website. What i did was i attached the user credentials in cookies.