1

这是一个 React 应用程序。在身份验证中,我将 cookie(第一方)作为要进行身份验证的凭据之一,它适用于 Chrome,但不适用于 Firefox。Chrome 已成功获取并存储 cookie,但 Firefox 没有获取该 cookie。

我正在使用js-cookie来处理 Cookie 管理

const isLogined = () => {
    var xhr = new XMLHttpRequest();
    const data = { Name, Password };
    enableLoading();
    setTimeout(() => {
      Http.post(`thisistheapiurl`, data, (xhr.withCredentials = true))
        .then(res => {
          disableLoading();
          if (res.status === 200) {
            if (res.data.result !== "failed") {
              var expired = new Date(new Date().getTime() + 3600 * 1000);
              Cookies.set("header", `${res.headers["x-header"]}`, {
                expires: expired
              });
              Cookies.set("signature", `${res.headers["x-signature"]}`, {
                expires: expired
              });
              Cookies.set("refreshToken", `${res.headers["x-refreshtoken"]}`, {
                expires: expired
              });
              // alert("succes");
              window.location.href = "/dashboard";
            } else {
              setStatus(
                intl.formatMessage({
                  id: "AUTH.VALIDATION.INVALID_LOGIN"
                })
              );
            }
          }
        })
        .catch(err => {
          console.log(err);
          disableLoading();
          setStatus(
            intl.formatMessage({
              id: "AUTH.VALIDATION.INVALID_LOGIN"
            })
          );
        });
    }, 1000);
  };
4

1 回答 1

0

只需在 onSubmit 表单的 isLogined 函数之前添加 event.preventDefault() 。如果您已经使用按钮类型 =“提交”,则您不需要在按钮上调用 isLogined 函数

于 2020-03-19T09:15:12.987 回答