首页 > 解决方案 > TypeError:注销期间未定义历史记录

问题描述

我已经实现了一个简单的注销功能,它可以清除所有 cookie 并删除本地存储。登录时,它可以完美运行,并且还会创建相应的令牌和本地存储。我在下面附上了图片这里 令牌为客户端登录创建了 token1。并为员工登录创建了令牌

一旦我注销,它就会清除令牌。但是,它会抛出以下错误,说明历史未定义。我也在我的登录页面中创建了一个变量历史记录。我无法弄清楚错误。我在下面附上了代码:; 错误

带有注销按钮的顶部导航栏:

export const ClientTopNavigationBar = ({ history }) => {
  const handleClientLogout = (evt) => {
    clientLogout(() => {
      history.push("/ClientLogin");
    });
  };

  const ShowClientTopNavigationBar = () => (
    <Styles>
      <Navbar expand="lg">
        <img src="/images/logo1.png" height="50" alt="Logo" />
        {isClientAuthenticated() && (
        <Fragment>
          <button
            className="btn btn-link text-decoration-none logout-btn"
             onClick={handleClientLogout}
          >
            logout
          </button>
        </Fragment>
         )} 
      </Navbar>
    </Styles>
  );

  //render
  return <header id="header">{ShowClientTopNavigationBar()}</header>;
};

export default withRouter(ClientTopNavigationBar);

auth.js 中的客户端身份验证

export const setClientAuthentication = (token1, client) => {
  setCookie("token1", token1);
  setLocalStorage("client", client);
};


export const isClientAuthenticated = () => {
  if (getCookie("token1") && getLocalStorage("client")) {
    return getLocalStorage("client");
  } else {
    return false;
  }
};

export const clientLogout = (next) => {
  deleteCookie("token1");
  deleteLocalStorage("client");

  next();
};

我还在这里附加了前端 UI: 前端

标签: reactjscookieslocal-storagelogouthistory

解决方案


推荐阅读