javascript - react js : How to Get Automatically Logout After Session Expired?

标签 javascript reactjs session-cookies

我正在尝试在 session 过期后实现自动注销,但我无法做到这一点,我尝试实现超时以及 cookies 过期时间,但它不起作用,这是我的所有代码。这是登录 API https://digitalfleet.eu/api/1/oauth/token/, 我正在尝试在 session 过期后实现自动注销,但我无法做到这一点,我尝试实现超时以及 cookies 过期时间,但它不起作用,这是我的所有代码。这是登录 API https://digitalfleet.eu/api/1/oauth/token/,

componentDidMount() {
    this.redirect();
  }

handleSubmit(e) {
    e.preventDefault();
    let authToken = localStorage.getItem("Token");
    axios({
      // Define Method
      method: "post",

      // Set Access Token URL
      url: config.apiUrl.login,

      //Set Headers
      headers: {
        "Content-Type": "application/x-www-form-urlencoded",
        Authorization: "Bearer " + JSON.parse(authToken)
      },

      // Interpolate variables in the strings using Template Literals
      data: `grant_type=${GRANT_TYPE}&username=${this.state.username}&password=${this.state.password}&client_id=${CLIENT_ID}`
    })
      .then(response => {
        // console.log(response.data);

        // Save All Data in localstorage
        var ls = require("local-storage");
        this.cookies = new Cookies();
        ls.set("Token", response.data["access_token"]);
        this.cookies.set("Token", response.data["access_token"], {
          path: "/",
          expires: new Date(Date.now() + 2592000)
        });
        ls.set("RefreshToken", response.data["refresh_token"]);
        ls.set("Toke Type", response.data["token_type"]);
        ls.set("Token Scope", response.data["scope"]);
        ls.set("Expire in", response.data["expires_in"]);

        // Used swal alert
        Swal.fire({
          title: "Log in",
          type: "success",
          text: "You Successfully loggedin",
          showConfirmButton: false,
          timerProgressBar: true,
          timer: 3000
        });
        this.redirect();
      })
      .catch(response => {
        //handle error
        console.log(response);
        Swal.fire({
          title: "Login Error",
          type: "error",
          text: "Please Enter Correct Username and Password",
          timerProgressBar: true,
          timer: 3000
        });
      });
  }
  redirect() {
    if (ls.get("Token")) {
      this.props.history.push("/dashboard");
      window.location.reload();
    }
  }

这是注销代码

onLogout() {
    localStorage.clear();
    // window.localStorage.clear();
    this.props.history.push("/");
    window.location.reload();
  }

最佳答案

this.redirect() 中,您正在检查 LocalStorage 中的 token ,但您已在 cookie 中设置了 token 的过期时间(而不是在本地存储中)

此外,无法在本地存储中设置过期时间。直接使用cookie或通过lscache之类的服务控制本地存储数据

关于javascript - react js : How to Get Automatically Logout After Session Expired?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59412364/

相关文章:

javascript - 如何使用 Webpack 5 加载服务 worker ?

javascript - 获取 express 编码的 cookie 作为 header 值

javascript - 利用 dcm4che DICOM 工具包和 dcm4chee archive (PACS) 在 Node.js 中实现基于云的图像共享平台

javascript - 如何申请:hover on :after to certain div in CSS

javascript - 如何修复 "TypeError: _this is undefined"

android - 在 ubuntu 中运行 React Native 应用程序时出错

asp.net-mvc - Angular 2 和 Asp.Net 5 mvc 6 - 身份验证实践

javascript - PIXI : browser sends no cookies with Texture. fromImage()

javascript - 在 Protractor 中启用元素时单击

javascript - javascript 中的简单 Prolog 谓词