瀏覽代碼

Automatically log out from inactive user sessions

This patch adds the `IdleTimer` component to the main App component,
which will be configured to time out if the user is inactive on the
app pages. On timeout, the user gets automatically logged out.

This functionality is disabled by default, and can be configured using
the `inactiveSessionTimeout` configuration option.
Daniel Vincze 1 年之前
父節點
當前提交
5ad8a42d44
共有 4 個文件被更改,包括 17 次插入0 次删除
  1. 3 0
      config.ts
  2. 1 0
      package.json
  3. 1 0
      src/@types/Config.ts
  4. 12 0
      src/components/App.tsx

+ 3 - 0
config.ts

@@ -31,6 +31,9 @@ const conf: Config = {
   // The timeout between polling requests
   // The timeout between polling requests
   requestPollTimeout: 5000,
   requestPollTimeout: 5000,
 
 
+  // The timeout for an inactive user session (in ms). Set 0 to disable.
+  inactiveSessionTimeout: 0,
+
   // - Specifies the `limit` for each provider when listing all its VMs for pagination.
   // - Specifies the `limit` for each provider when listing all its VMs for pagination.
   // - If the provider is not in this list, the 'default' value will be used.
   // - If the provider is not in this list, the 'default' value will be used.
   // - If the `default` value is lower than the number of instances that
   // - If the `default` value is lower than the number of instances that

+ 1 - 0
package.json

@@ -93,6 +93,7 @@
     "react-datetime": "^3.1.1",
     "react-datetime": "^3.1.1",
     "react-dom": "^16.13.1",
     "react-dom": "^16.13.1",
     "react-hot-loader": "^4.12.17",
     "react-hot-loader": "^4.12.17",
+    "react-idle-timer": "^4.3.6",
     "react-modal": "^3.11.2",
     "react-modal": "^3.11.2",
     "react-motion": "^0.5.2",
     "react-motion": "^0.5.2",
     "react-notification-system": "^0.4.0",
     "react-notification-system": "^0.4.0",

+ 1 - 0
src/@types/Config.ts

@@ -43,4 +43,5 @@ export type Config = {
   servicesUrls: Services;
   servicesUrls: Services;
   maxMinionPoolEventsPerPage: number;
   maxMinionPoolEventsPerPage: number;
   bareMetalEndpointName: string;
   bareMetalEndpointName: string;
+  inactiveSessionTimeout: number;
 };
 };

+ 12 - 0
src/components/App.tsx

@@ -15,6 +15,7 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
 import { hot } from "react-hot-loader/root";
 import { hot } from "react-hot-loader/root";
 import React from "react";
 import React from "react";
 import { BrowserRouter as Router, Switch, Route } from "react-router-dom";
 import { BrowserRouter as Router, Switch, Route } from "react-router-dom";
+import IdleTimer from "react-idle-timer";
 import styled, { createGlobalStyle } from "styled-components";
 import styled, { createGlobalStyle } from "styled-components";
 import { observe } from "mobx";
 import { observe } from "mobx";
 
 
@@ -87,6 +88,10 @@ class App extends React.Component<Record<string, unknown>, State> {
     isConfigReady: false,
     isConfigReady: false,
   };
   };
 
 
+  onIdle() {
+    userStore.logout();
+  }
+
   async componentDidMount() {
   async componentDidMount() {
     observe(userStore, "loggedUser", () => {
     observe(userStore, "loggedUser", () => {
       this.setState({});
       this.setState({});
@@ -195,6 +200,13 @@ class App extends React.Component<Record<string, unknown>, State> {
     return (
     return (
       <Wrapper>
       <Wrapper>
         <GlobalStyle />
         <GlobalStyle />
+        {configLoader.config.inactiveSessionTimeout > 0 ? (
+          <IdleTimer
+            timeout={configLoader.config.inactiveSessionTimeout}
+            throttle={500}
+            onIdle={this.onIdle}
+          />
+        ) : null}
         <Router>
         <Router>
           <Switch>
           <Switch>
             {configLoader.isFirstLaunch ? (
             {configLoader.isFirstLaunch ? (