当重定向到新的外部URL时如何保留localStorage [重复]

问题描述 投票:0回答:1

我面临一个无法跨重定向保存localStorage的问题。这是我的代码:

// 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('/'));

端点调用api并在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,
        ],
      },
  });
};

我可以看到凭据正确存储在应用程序的localStorage中:enter image description here

但是当我使用window.open(process.env.NEW_SITE_URL)重定向到新的URL时,它们不会保留。

enter image description here

[当重定向到新网址时,如何确保credentials对象被继承?由于重定向到应用程序中的逻辑,我无法使用会话和cookie。它仅在localStorage中查找credentials

javascript reactjs google-chrome local-storage redux-saga
1个回答
0
投票

我过去曾遇到过这个问题。每个网站的localStorage是唯一的。我所做的是我在Cookie中附加了他的用户凭据。

© www.soinside.com 2019 - 2024. All rights reserved.