@backstage/core-components 0.18.11-next.0 → 0.18.11-next.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # @backstage/core-components
2
2
 
3
+ ## 0.18.11-next.1
4
+
5
+ ### Patch Changes
6
+
7
+ - dbe93a7: Fix autologout not working correctly when closing all tabs
8
+ - Updated dependencies
9
+ - @backstage/core-plugin-api@1.12.7-next.0
10
+
3
11
  ## 0.18.11-next.0
4
12
 
5
13
  ### Patch Changes
@@ -1,6 +1,6 @@
1
1
  import { jsx, Fragment } from 'react/jsx-runtime';
2
2
  import { useApi, identityApiRef, configApiRef } from '@backstage/core-plugin-api';
3
- import { useState, useEffect, useMemo } from 'react';
3
+ import { useState, useMemo, useEffect } from 'react';
4
4
  import { useIdleTimer, workerTimers } from 'react-idle-timer';
5
5
  import { LAST_SEEN_ONLINE_STORAGE_KEY, useLogoutDisconnectedUserEffect } from './disconnectedUsers.esm.js';
6
6
  import { StillTherePrompt } from './StillTherePrompt.esm.js';
@@ -28,6 +28,7 @@ const ConditionalAutoLogout = ({
28
28
  const onIdle = () => {
29
29
  setPromptOpen(false);
30
30
  setRemainingTimeCountdown(0);
31
+ lastSeenOnlineStore.delete();
31
32
  identityApi.signOut();
32
33
  };
33
34
  const onActive = () => {
@@ -94,17 +95,34 @@ const parseConfig = (configApi, props) => {
94
95
  const AutoLogout = (props) => {
95
96
  const identityApi = useApi(identityApiRef);
96
97
  const configApi = useApi(configApiRef);
97
- const [isLogged, setIsLogged] = useState(false);
98
+ const [isLogged, setIsLogged] = useState(null);
99
+ const lastSeenOnlineStore = useMemo(
100
+ () => new DefaultTimestampStore(LAST_SEEN_ONLINE_STORAGE_KEY),
101
+ []
102
+ );
98
103
  useEffect(() => {
99
- async function isLoggedIn(identity) {
100
- if ((await identity.getCredentials()).token) {
101
- setIsLogged(true);
102
- } else {
104
+ let cancelled = false;
105
+ async function checkLogin(identity) {
106
+ try {
107
+ const creds = await identity.getCredentials();
108
+ if (cancelled) return;
109
+ if (creds?.token) {
110
+ setIsLogged(true);
111
+ } else {
112
+ setIsLogged(false);
113
+ lastSeenOnlineStore.delete();
114
+ }
115
+ } catch (err) {
116
+ if (cancelled) return;
103
117
  setIsLogged(false);
118
+ lastSeenOnlineStore.delete();
104
119
  }
105
120
  }
106
- isLoggedIn(identityApi);
107
- }, [identityApi]);
121
+ checkLogin(identityApi);
122
+ return () => {
123
+ cancelled = true;
124
+ };
125
+ }, [lastSeenOnlineStore, identityApi]);
108
126
  const {
109
127
  enabled,
110
128
  idleTimeoutMinutes,
@@ -131,15 +149,12 @@ const AutoLogout = (props) => {
131
149
  );
132
150
  }
133
151
  }, [idleTimeoutMinutes, promptBeforeIdleSeconds]);
134
- const lastSeenOnlineStore = useMemo(
135
- () => new DefaultTimestampStore(LAST_SEEN_ONLINE_STORAGE_KEY),
136
- []
137
- );
138
152
  const [promptOpen, setPromptOpen] = useState(false);
139
153
  const [remainingTimeCountdown, setRemainingTimeCountdown] = useState(0);
140
154
  useLogoutDisconnectedUserEffect({
141
155
  enableEffect: logoutIfDisconnected,
142
- autologoutIsEnabled: enabled && isLogged,
156
+ autologoutIsEnabled: enabled,
157
+ isLoggedIn: isLogged,
143
158
  idleTimeoutSeconds: idleTimeoutMinutes * 60,
144
159
  lastSeenOnlineStore,
145
160
  identityApi
@@ -1 +1 @@
1
- {"version":3,"file":"AutoLogout.esm.js","sources":["../../../src/components/AutoLogout/AutoLogout.tsx"],"sourcesContent":["/*\n * Copyright 2023 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport {\n ConfigApi,\n configApiRef,\n IdentityApi,\n identityApiRef,\n useApi,\n} from '@backstage/core-plugin-api';\nimport { useEffect, useMemo, useState } from 'react';\nimport {\n EventsType,\n IIdleTimer,\n workerTimers,\n useIdleTimer,\n} from 'react-idle-timer';\n\nimport {\n LAST_SEEN_ONLINE_STORAGE_KEY,\n useLogoutDisconnectedUserEffect,\n} from './disconnectedUsers';\nimport { StillTherePrompt } from './StillTherePrompt';\nimport { DefaultTimestampStore, TimestampStore } from './timestampStore';\n\ntype AutoLogoutTrackableEvent = EventsType;\n\n/** @public */\nexport type AutoLogoutProps = {\n /**\n * Enable/disable the AutoLogoutMechanism.\n * defaults to true.\n */\n enabled?: boolean;\n /**\n * The amount of time (in minutes) of inactivity\n * after which the user is automatically logged out.\n * defaults to 60 minutes.\n */\n idleTimeoutMinutes?: number;\n /**\n * The number of seconds before the idleTimeout expires,\n * at which the user will be alerted by a Dialog that\n * they are about to be logged out.\n * defaults to 10 seconds\n */\n promptBeforeIdleSeconds?: number;\n /**\n * Enable/disable the usage of Node's worker thread timers instead of main thread timers.\n * This is helpful if you notice that the your browser is killing inactive tab's timers, like the one used by AutoLogout.\n * If you experience some browser incompatibility, you may try to set this to false.\n * defaults to true.\n */\n useWorkerTimers?: boolean;\n /**\n * Enable/disable the autologout for disconnected users.\n * disconnected users are the ones that are logged in but have no Backstage tab open in their browsers.\n * If enabled, disconnected users will be automatically logged out after `idleTimeoutMinutes`\n * defaults to true\n */\n logoutIfDisconnected?: boolean;\n};\n\ntype AutoLogoutInternalProps = Omit<Required<AutoLogoutProps>, 'enabled'> & {\n events: AutoLogoutTrackableEvent[];\n promptOpen: boolean;\n setPromptOpen: (value: boolean) => void;\n remainingTimeCountdown: number;\n setRemainingTimeCountdown: (amount: number) => void;\n identityApi: IdentityApi;\n lastSeenOnlineStore: TimestampStore;\n};\n\nconst ConditionalAutoLogout = ({\n idleTimeoutMinutes,\n events,\n useWorkerTimers,\n logoutIfDisconnected,\n promptBeforeIdleSeconds,\n promptOpen,\n setPromptOpen,\n remainingTimeCountdown,\n setRemainingTimeCountdown,\n identityApi,\n lastSeenOnlineStore,\n}: AutoLogoutInternalProps): JSX.Element => {\n const promptBeforeIdleMillis = promptBeforeIdleSeconds * 1000;\n const promptBeforeIdle = promptBeforeIdleMillis > 0 ? true : false;\n\n const onPrompt = async () => {\n // onPrompt will be called `promptBeforeIdle` milliseconds before `timeout`.\n // All events are disabled while the prompt is active.\n // If the user wishes to stay active, call the `activate()` method.\n // You can get the remaining prompt time with the `getRemainingTime()` method,\n setPromptOpen(true);\n setRemainingTimeCountdown(promptBeforeIdleMillis);\n };\n\n const onIdle = () => {\n // onIdle will be called after the timeout is reached.\n // Events will be rebound as long as `stopOnMount` is not set.\n setPromptOpen(false);\n setRemainingTimeCountdown(0);\n identityApi.signOut();\n };\n\n const onActive = () => {\n // onActive will only be called if `activate()` is called while `isPrompted()`\n // is true. Here you will also want to close your modal and perform\n // any active actions.\n setPromptOpen(false);\n setRemainingTimeCountdown(0);\n };\n\n const onAction = (\n _event?: Event | undefined,\n _idleTimer?: IIdleTimer | null,\n ) => {\n // onAction will be called if any user event is detected. The list of events that triggers a user event detection is the list of configured events\n // If any user event is detected we update the Last seen online in storage\n lastSeenOnlineStore.save(new Date());\n };\n\n const timer = useIdleTimer({\n timeout: idleTimeoutMinutes * 60 * 1000,\n events: events,\n crossTab: true,\n name: 'autologout-timer',\n timers: useWorkerTimers ? workerTimers : undefined,\n onIdle: onIdle,\n onActive: promptBeforeIdle ? onActive : undefined,\n onAction: logoutIfDisconnected ? onAction : undefined,\n onPrompt: promptBeforeIdle ? onPrompt : undefined,\n promptBeforeIdle: promptBeforeIdle ? promptBeforeIdleMillis : undefined,\n syncTimers: 1000,\n });\n\n return (\n <>\n {promptBeforeIdle && (\n <StillTherePrompt\n idleTimer={timer}\n open={promptOpen}\n setOpen={setPromptOpen}\n remainingTime={remainingTimeCountdown}\n setRemainingTime={setRemainingTimeCountdown}\n promptTimeoutMillis={promptBeforeIdleMillis}\n />\n )}\n </>\n );\n};\n\nconst defaultConfig: Required<AutoLogoutProps> = {\n enabled: true,\n idleTimeoutMinutes: 0.5,\n promptBeforeIdleSeconds: 10,\n useWorkerTimers: true,\n logoutIfDisconnected: true,\n};\n\n/**\n * A list of DOM events that the activity tracker will use to determine if the user is active or not.\n */\nconst defaultTrackedEvents: AutoLogoutTrackableEvent[] = [\n 'mousemove',\n 'keydown',\n 'wheel',\n 'DOMMouseScroll',\n 'mousewheel',\n 'mousedown',\n 'touchstart',\n 'touchmove',\n 'MSPointerDown',\n 'MSPointerMove',\n 'visibilitychange',\n];\n\n/**\n * Parses configuration for the AutoLogout. Properties configured in `app-config` take precedence over the props passed to the React component.\n * If neither props nor config properties are found, a default value will be set accordingly.\n */\nconst parseConfig = (\n configApi: ConfigApi,\n props: AutoLogoutProps,\n): Required<AutoLogoutProps> => {\n return {\n enabled:\n configApi.getOptionalBoolean('auth.autologout.enabled') ??\n props.enabled ??\n defaultConfig.enabled,\n idleTimeoutMinutes:\n configApi.getOptionalNumber('auth.autologout.idleTimeoutMinutes') ??\n props.idleTimeoutMinutes ??\n defaultConfig.idleTimeoutMinutes,\n promptBeforeIdleSeconds:\n configApi.getOptionalNumber('auth.autologout.promptBeforeIdleSeconds') ??\n props.promptBeforeIdleSeconds ??\n defaultConfig.promptBeforeIdleSeconds,\n useWorkerTimers:\n configApi.getOptionalBoolean('auth.autologout.useWorkerTimers') ??\n props.useWorkerTimers ??\n defaultConfig.useWorkerTimers,\n logoutIfDisconnected:\n configApi.getOptionalBoolean('auth.autologout.logoutIfDisconnected') ??\n props.logoutIfDisconnected ??\n defaultConfig.logoutIfDisconnected,\n };\n};\n\n/**\n * The Autologout feature enables platform engineers to add a mechanism to log out users after a configurable amount of time of inactivity.\n * When enabled, the mechanism will track user actions (mouse movement, mouse click, key pressing, taps, etc.) in order to determine if they are active or not.\n * After a certain amount of inactivity/idle time, the user session is invalidated and they are required to sign in again.\n *\n * @public\n */\nexport const AutoLogout = (props: AutoLogoutProps): JSX.Element | null => {\n const identityApi = useApi(identityApiRef);\n const configApi = useApi(configApiRef);\n const [isLogged, setIsLogged] = useState(false);\n useEffect(() => {\n // if the user is not logged in, the autologout feature won't affect the app even if enabled\n async function isLoggedIn(identity: IdentityApi) {\n if ((await identity.getCredentials()).token) {\n setIsLogged(true);\n } else {\n setIsLogged(false);\n }\n }\n isLoggedIn(identityApi);\n }, [identityApi]);\n\n const {\n enabled,\n idleTimeoutMinutes,\n promptBeforeIdleSeconds,\n logoutIfDisconnected,\n useWorkerTimers,\n }: AutoLogoutProps = useMemo(() => {\n return parseConfig(configApi, props);\n }, [configApi, props]);\n\n useEffect(() => {\n if (idleTimeoutMinutes < 0.5) {\n throw new Error(\n '❌ idleTimeoutMinutes property should be >= 0.5 minutes (30 seconds).',\n );\n }\n\n if (promptBeforeIdleSeconds < 0) {\n throw new Error(\n '❌ promptBeforeIdleSeconds property should be >= 0 seconds. Set to 0 to disable the prompt.',\n );\n }\n\n if (idleTimeoutMinutes * 60 <= promptBeforeIdleSeconds) {\n throw new Error(\n `❌ promptBeforeIdleSeconds should be smaller than idleTimeoutMinutes`,\n );\n }\n }, [idleTimeoutMinutes, promptBeforeIdleSeconds]);\n\n const lastSeenOnlineStore: TimestampStore = useMemo(\n () => new DefaultTimestampStore(LAST_SEEN_ONLINE_STORAGE_KEY),\n [],\n );\n const [promptOpen, setPromptOpen] = useState<boolean>(false);\n\n const [remainingTimeCountdown, setRemainingTimeCountdown] =\n useState<number>(0);\n\n useLogoutDisconnectedUserEffect({\n enableEffect: logoutIfDisconnected,\n autologoutIsEnabled: enabled && isLogged,\n idleTimeoutSeconds: idleTimeoutMinutes * 60,\n lastSeenOnlineStore,\n identityApi,\n });\n\n if (!enabled || !isLogged) {\n return null;\n }\n\n return (\n <ConditionalAutoLogout\n idleTimeoutMinutes={idleTimeoutMinutes}\n promptBeforeIdleSeconds={promptBeforeIdleSeconds}\n useWorkerTimers={useWorkerTimers}\n events={defaultTrackedEvents}\n logoutIfDisconnected={logoutIfDisconnected}\n promptOpen={promptOpen}\n setPromptOpen={setPromptOpen}\n remainingTimeCountdown={remainingTimeCountdown}\n setRemainingTimeCountdown={setRemainingTimeCountdown}\n identityApi={identityApi}\n lastSeenOnlineStore={lastSeenOnlineStore}\n />\n );\n};\n"],"names":[],"mappings":";;;;;;;;AAsFA,MAAM,wBAAwB,CAAC;AAAA,EAC7B,kBAAA;AAAA,EACA,MAAA;AAAA,EACA,eAAA;AAAA,EACA,oBAAA;AAAA,EACA,uBAAA;AAAA,EACA,UAAA;AAAA,EACA,aAAA;AAAA,EACA,sBAAA;AAAA,EACA,yBAAA;AAAA,EACA,WAAA;AAAA,EACA;AACF,CAAA,KAA4C;AAC1C,EAAA,MAAM,yBAAyB,uBAAA,GAA0B,GAAA;AACzD,EAAA,MAAM,gBAAA,GAAmB,sBAAA,GAAyB,CAAA,GAAI,IAAA,GAAO,KAAA;AAE7D,EAAA,MAAM,WAAW,YAAY;AAK3B,IAAA,aAAA,CAAc,IAAI,CAAA;AAClB,IAAA,yBAAA,CAA0B,sBAAsB,CAAA;AAAA,EAClD,CAAA;AAEA,EAAA,MAAM,SAAS,MAAM;AAGnB,IAAA,aAAA,CAAc,KAAK,CAAA;AACnB,IAAA,yBAAA,CAA0B,CAAC,CAAA;AAC3B,IAAA,WAAA,CAAY,OAAA,EAAQ;AAAA,EACtB,CAAA;AAEA,EAAA,MAAM,WAAW,MAAM;AAIrB,IAAA,aAAA,CAAc,KAAK,CAAA;AACnB,IAAA,yBAAA,CAA0B,CAAC,CAAA;AAAA,EAC7B,CAAA;AAEA,EAAA,MAAM,QAAA,GAAW,CACf,MAAA,EACA,UAAA,KACG;AAGH,IAAA,mBAAA,CAAoB,IAAA,iBAAK,IAAI,IAAA,EAAM,CAAA;AAAA,EACrC,CAAA;AAEA,EAAA,MAAM,QAAQ,YAAA,CAAa;AAAA,IACzB,OAAA,EAAS,qBAA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AAAA,MACA,yBAAA;AAAA,MACA,WAAA;AAAA,MACA;AAAA;AAAA,GACF;AAEJ;;;;"}
1
+ {"version":3,"file":"AutoLogout.esm.js","sources":["../../../src/components/AutoLogout/AutoLogout.tsx"],"sourcesContent":["/*\n * Copyright 2023 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport {\n ConfigApi,\n configApiRef,\n IdentityApi,\n identityApiRef,\n useApi,\n} from '@backstage/core-plugin-api';\nimport { useEffect, useMemo, useState } from 'react';\nimport {\n EventsType,\n IIdleTimer,\n workerTimers,\n useIdleTimer,\n} from 'react-idle-timer';\n\nimport {\n LAST_SEEN_ONLINE_STORAGE_KEY,\n useLogoutDisconnectedUserEffect,\n} from './disconnectedUsers';\nimport { StillTherePrompt } from './StillTherePrompt';\nimport { DefaultTimestampStore, TimestampStore } from './timestampStore';\n\ntype AutoLogoutTrackableEvent = EventsType;\n\n/** @public */\nexport type AutoLogoutProps = {\n /**\n * Enable/disable the AutoLogoutMechanism.\n * defaults to true.\n */\n enabled?: boolean;\n /**\n * The amount of time (in minutes) of inactivity\n * after which the user is automatically logged out.\n * defaults to 60 minutes.\n */\n idleTimeoutMinutes?: number;\n /**\n * The number of seconds before the idleTimeout expires,\n * at which the user will be alerted by a Dialog that\n * they are about to be logged out.\n * defaults to 10 seconds\n */\n promptBeforeIdleSeconds?: number;\n /**\n * Enable/disable the usage of Node's worker thread timers instead of main thread timers.\n * This is helpful if you notice that the your browser is killing inactive tab's timers, like the one used by AutoLogout.\n * If you experience some browser incompatibility, you may try to set this to false.\n * defaults to true.\n */\n useWorkerTimers?: boolean;\n /**\n * Enable/disable the autologout for disconnected users.\n * disconnected users are the ones that are logged in but have no Backstage tab open in their browsers.\n * If enabled, disconnected users will be automatically logged out after `idleTimeoutMinutes`\n * defaults to true\n */\n logoutIfDisconnected?: boolean;\n};\n\ntype AutoLogoutInternalProps = Omit<Required<AutoLogoutProps>, 'enabled'> & {\n events: AutoLogoutTrackableEvent[];\n promptOpen: boolean;\n setPromptOpen: (value: boolean) => void;\n remainingTimeCountdown: number;\n setRemainingTimeCountdown: (amount: number) => void;\n identityApi: IdentityApi;\n lastSeenOnlineStore: TimestampStore;\n};\n\nconst ConditionalAutoLogout = ({\n idleTimeoutMinutes,\n events,\n useWorkerTimers,\n logoutIfDisconnected,\n promptBeforeIdleSeconds,\n promptOpen,\n setPromptOpen,\n remainingTimeCountdown,\n setRemainingTimeCountdown,\n identityApi,\n lastSeenOnlineStore,\n}: AutoLogoutInternalProps): JSX.Element => {\n const promptBeforeIdleMillis = promptBeforeIdleSeconds * 1000;\n const promptBeforeIdle = promptBeforeIdleMillis > 0 ? true : false;\n\n const onPrompt = async () => {\n // onPrompt will be called `promptBeforeIdle` milliseconds before `timeout`.\n // All events are disabled while the prompt is active.\n // If the user wishes to stay active, call the `activate()` method.\n // You can get the remaining prompt time with the `getRemainingTime()` method,\n setPromptOpen(true);\n setRemainingTimeCountdown(promptBeforeIdleMillis);\n };\n\n const onIdle = () => {\n // onIdle will be called after the timeout is reached.\n // Events will be rebound as long as `stopOnMount` is not set.\n setPromptOpen(false);\n setRemainingTimeCountdown(0);\n lastSeenOnlineStore.delete();\n identityApi.signOut();\n };\n\n const onActive = () => {\n // onActive will only be called if `activate()` is called while `isPrompted()`\n // is true. Here you will also want to close your modal and perform\n // any active actions.\n setPromptOpen(false);\n setRemainingTimeCountdown(0);\n };\n\n const onAction = (\n _event?: Event | undefined,\n _idleTimer?: IIdleTimer | null,\n ) => {\n // onAction will be called if any user event is detected. The list of events that triggers a user event detection is the list of configured events\n // If any user event is detected we update the Last seen online in storage\n lastSeenOnlineStore.save(new Date());\n };\n\n const timer = useIdleTimer({\n timeout: idleTimeoutMinutes * 60 * 1000,\n events: events,\n crossTab: true,\n name: 'autologout-timer',\n timers: useWorkerTimers ? workerTimers : undefined,\n onIdle: onIdle,\n onActive: promptBeforeIdle ? onActive : undefined,\n onAction: logoutIfDisconnected ? onAction : undefined,\n onPrompt: promptBeforeIdle ? onPrompt : undefined,\n promptBeforeIdle: promptBeforeIdle ? promptBeforeIdleMillis : undefined,\n syncTimers: 1000,\n });\n\n return (\n <>\n {promptBeforeIdle && (\n <StillTherePrompt\n idleTimer={timer}\n open={promptOpen}\n setOpen={setPromptOpen}\n remainingTime={remainingTimeCountdown}\n setRemainingTime={setRemainingTimeCountdown}\n promptTimeoutMillis={promptBeforeIdleMillis}\n />\n )}\n </>\n );\n};\n\nconst defaultConfig: Required<AutoLogoutProps> = {\n enabled: true,\n idleTimeoutMinutes: 0.5,\n promptBeforeIdleSeconds: 10,\n useWorkerTimers: true,\n logoutIfDisconnected: true,\n};\n\n/**\n * A list of DOM events that the activity tracker will use to determine if the user is active or not.\n */\nconst defaultTrackedEvents: AutoLogoutTrackableEvent[] = [\n 'mousemove',\n 'keydown',\n 'wheel',\n 'DOMMouseScroll',\n 'mousewheel',\n 'mousedown',\n 'touchstart',\n 'touchmove',\n 'MSPointerDown',\n 'MSPointerMove',\n 'visibilitychange',\n];\n\n/**\n * Parses configuration for the AutoLogout. Properties configured in `app-config` take precedence over the props passed to the React component.\n * If neither props nor config properties are found, a default value will be set accordingly.\n */\nconst parseConfig = (\n configApi: ConfigApi,\n props: AutoLogoutProps,\n): Required<AutoLogoutProps> => {\n return {\n enabled:\n configApi.getOptionalBoolean('auth.autologout.enabled') ??\n props.enabled ??\n defaultConfig.enabled,\n idleTimeoutMinutes:\n configApi.getOptionalNumber('auth.autologout.idleTimeoutMinutes') ??\n props.idleTimeoutMinutes ??\n defaultConfig.idleTimeoutMinutes,\n promptBeforeIdleSeconds:\n configApi.getOptionalNumber('auth.autologout.promptBeforeIdleSeconds') ??\n props.promptBeforeIdleSeconds ??\n defaultConfig.promptBeforeIdleSeconds,\n useWorkerTimers:\n configApi.getOptionalBoolean('auth.autologout.useWorkerTimers') ??\n props.useWorkerTimers ??\n defaultConfig.useWorkerTimers,\n logoutIfDisconnected:\n configApi.getOptionalBoolean('auth.autologout.logoutIfDisconnected') ??\n props.logoutIfDisconnected ??\n defaultConfig.logoutIfDisconnected,\n };\n};\n\n/**\n * The Autologout feature enables platform engineers to add a mechanism to log out users after a configurable amount of time of inactivity.\n * When enabled, the mechanism will track user actions (mouse movement, mouse click, key pressing, taps, etc.) in order to determine if they are active or not.\n * After a certain amount of inactivity/idle time, the user session is invalidated and they are required to sign in again.\n *\n * @public\n */\nexport const AutoLogout = (props: AutoLogoutProps): JSX.Element | null => {\n const identityApi = useApi(identityApiRef);\n const configApi = useApi(configApiRef);\n const [isLogged, setIsLogged] = useState<boolean | null>(null);\n const lastSeenOnlineStore: TimestampStore = useMemo(\n () => new DefaultTimestampStore(LAST_SEEN_ONLINE_STORAGE_KEY),\n [],\n );\n\n useEffect(() => {\n let cancelled = false;\n\n async function checkLogin(identity: IdentityApi) {\n try {\n const creds = await identity.getCredentials();\n if (cancelled) return;\n if (creds?.token) {\n setIsLogged(true);\n } else {\n setIsLogged(false);\n lastSeenOnlineStore.delete();\n }\n } catch (err) {\n if (cancelled) return;\n setIsLogged(false);\n lastSeenOnlineStore.delete();\n }\n }\n checkLogin(identityApi);\n\n return () => {\n cancelled = true;\n };\n }, [lastSeenOnlineStore, identityApi]);\n\n const {\n enabled,\n idleTimeoutMinutes,\n promptBeforeIdleSeconds,\n logoutIfDisconnected,\n useWorkerTimers,\n }: AutoLogoutProps = useMemo(() => {\n return parseConfig(configApi, props);\n }, [configApi, props]);\n\n useEffect(() => {\n if (idleTimeoutMinutes < 0.5) {\n throw new Error(\n '❌ idleTimeoutMinutes property should be >= 0.5 minutes (30 seconds).',\n );\n }\n\n if (promptBeforeIdleSeconds < 0) {\n throw new Error(\n '❌ promptBeforeIdleSeconds property should be >= 0 seconds. Set to 0 to disable the prompt.',\n );\n }\n\n if (idleTimeoutMinutes * 60 <= promptBeforeIdleSeconds) {\n throw new Error(\n `❌ promptBeforeIdleSeconds should be smaller than idleTimeoutMinutes`,\n );\n }\n }, [idleTimeoutMinutes, promptBeforeIdleSeconds]);\n\n const [promptOpen, setPromptOpen] = useState<boolean>(false);\n\n const [remainingTimeCountdown, setRemainingTimeCountdown] =\n useState<number>(0);\n\n useLogoutDisconnectedUserEffect({\n enableEffect: logoutIfDisconnected,\n autologoutIsEnabled: enabled,\n isLoggedIn: isLogged,\n idleTimeoutSeconds: idleTimeoutMinutes * 60,\n lastSeenOnlineStore,\n identityApi,\n });\n\n if (!enabled || !isLogged) {\n return null;\n }\n\n return (\n <ConditionalAutoLogout\n idleTimeoutMinutes={idleTimeoutMinutes}\n promptBeforeIdleSeconds={promptBeforeIdleSeconds}\n useWorkerTimers={useWorkerTimers}\n events={defaultTrackedEvents}\n logoutIfDisconnected={logoutIfDisconnected}\n promptOpen={promptOpen}\n setPromptOpen={setPromptOpen}\n remainingTimeCountdown={remainingTimeCountdown}\n setRemainingTimeCountdown={setRemainingTimeCountdown}\n identityApi={identityApi}\n lastSeenOnlineStore={lastSeenOnlineStore}\n />\n );\n};\n"],"names":[],"mappings":";;;;;;;;AAsFA,MAAM,wBAAwB,CAAC;AAAA,EAC7B,kBAAA;AAAA,EACA,MAAA;AAAA,EACA,eAAA;AAAA,EACA,oBAAA;AAAA,EACA,uBAAA;AAAA,EACA,UAAA;AAAA,EACA,aAAA;AAAA,EACA,sBAAA;AAAA,EACA,yBAAA;AAAA,EACA,WAAA;AAAA,EACA;AACF,CAAA,KAA4C;AAC1C,EAAA,MAAM,yBAAyB,uBAAA,GAA0B,GAAA;AACzD,EAAA,MAAM,gBAAA,GAAmB,sBAAA,GAAyB,CAAA,GAAI,IAAA,GAAO,KAAA;AAE7D,EAAA,MAAM,WAAW,YAAY;AAK3B,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sEnabled,
7
+ isLoggedIn,
7
8
  idleTimeoutSeconds,
8
9
  lastSeenOnlineStore,
9
10
  identityApi
10
11
  }) => {
11
12
  useEffect(() => {
12
- if (autologoutIsEnabled && enableEffect) {
13
- const lastSeenOnline = lastSeenOnlineStore.get();
14
- if (lastSeenOnline) {
15
- const now = /* @__PURE__ */ new Date();
16
- const nowSeconds = Math.ceil(now.getTime() / 1e3);
17
- const lastSeenOnlineSeconds = Math.ceil(
18
- lastSeenOnline.getTime() / 1e3
19
- );
20
- if (nowSeconds - lastSeenOnlineSeconds > idleTimeoutSeconds) {
21
- identityApi.signOut();
22
- }
23
- }
24
- lastSeenOnlineStore.save(/* @__PURE__ */ new Date());
25
- } else {
13
+ const shouldCheckDisconnectedUser = autologoutIsEnabled && enableEffect;
14
+ if (isLoggedIn === null) {
15
+ return;
16
+ }
17
+ if (!shouldCheckDisconnectedUser || !isLoggedIn) {
26
18
  lastSeenOnlineStore.delete();
19
+ return;
20
+ }
21
+ const lastSeenOnline = lastSeenOnlineStore.get();
22
+ if (lastSeenOnline) {
23
+ const now = /* @__PURE__ */ new Date();
24
+ const nowSeconds = Math.ceil(now.getTime() / 1e3);
25
+ const lastSeenOnlineSeconds = Math.ceil(lastSeenOnline.getTime() / 1e3);
26
+ if (nowSeconds - lastSeenOnlineSeconds > idleTimeoutSeconds) {
27
+ lastSeenOnlineStore.delete();
28
+ identityApi.signOut();
29
+ return;
30
+ }
27
31
  }
32
+ lastSeenOnlineStore.save(/* @__PURE__ */ new Date());
28
33
  }, [
29
34
  autologoutIsEnabled,
30
35
  enableEffect,
36
+ isLoggedIn,
31
37
  identityApi,
32
38
  idleTimeoutSeconds,
33
39
  lastSeenOnlineStore
@@ -1 +1 @@
1
- {"version":3,"file":"disconnectedUsers.esm.js","sources":["../../../src/components/AutoLogout/disconnectedUsers.ts"],"sourcesContent":["/*\n * Copyright 2023 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\nimport { IdentityApi } from '@backstage/core-plugin-api';\nimport { useEffect } from 'react';\n\nimport { TimestampStore } from './timestampStore';\n\nexport const LAST_SEEN_ONLINE_STORAGE_KEY =\n '@backstage/autologout:lastSeenOnline';\n\nexport type UseLogoutDisconnectedUserEffectProps = {\n enableEffect: boolean;\n autologoutIsEnabled: boolean;\n idleTimeoutSeconds: number;\n lastSeenOnlineStore: TimestampStore;\n identityApi: IdentityApi;\n};\n\nexport const useLogoutDisconnectedUserEffect = ({\n enableEffect,\n autologoutIsEnabled,\n idleTimeoutSeconds,\n lastSeenOnlineStore,\n identityApi,\n}: UseLogoutDisconnectedUserEffectProps) => {\n useEffect(() => {\n /**\n * Considers disconnected users as inactive users.\n * If all Backstage tabs are closed and idleTimeoutMinutes are passed then logout the user anyway.\n */\n if (autologoutIsEnabled && enableEffect) {\n const lastSeenOnline = lastSeenOnlineStore.get();\n if (lastSeenOnline) {\n const now = new Date();\n const nowSeconds = Math.ceil(now.getTime() / 1000);\n const lastSeenOnlineSeconds = Math.ceil(\n lastSeenOnline.getTime() / 1000,\n );\n if (nowSeconds - lastSeenOnlineSeconds > idleTimeoutSeconds) {\n identityApi.signOut();\n }\n }\n /**\n * save for the first time when app is loaded, so that\n * if user logs in and does nothing we still have a\n * lastSeenOnline value in store\n */\n lastSeenOnlineStore.save(new Date());\n } else {\n lastSeenOnlineStore.delete();\n }\n }, [\n autologoutIsEnabled,\n enableEffect,\n identityApi,\n idleTimeoutSeconds,\n lastSeenOnlineStore,\n ]);\n};\n"],"names":[],"mappings":";;AAoBO,MAAM,4BAAA,GACX;AAUK,MAAM,kCAAkC,CAAC;AAAA,EAC9C,YAAA;AAAA,EACA,mBAAA;AAAA,EACA,kBAAA;AAAA,EACA,mBAAA;AAAA,EACA;AACF,CAAA,KAA4C;AAC1C,EAAA,SAAA,CAAU,MAAM;AAKd,IAAA,IAAI,uBAAuB,YAAA,EAAc;AACvC,MAAA,MAAM,cAAA,GAAiB,oBAAoB,GAAA,EAAI;AAC/C,MAAA,IAAI,cAAA,EAAgB;AAClB,QAAA,MAAM,GAAA,uBAAU,IAAA,EAAK;AACrB,QAAA,MAAM,aAAa,IAAA,CAAK,IAAA,CAAK,GAAA,CAAI,OAAA,KAAY,GAAI,CAAA;AACjD,QAAA,MAAM,wBAAwB,IAAA,CAAK,IAAA;AAAA,UACjC,cAAA,CAAe,SAAQ,GAAI;AAAA,SAC7B;AACA,QAAA,IAAI,UAAA,GAAa,wBAAwB,kBAAA,EAAoB;AAC3D,UAAA,WAAA,CAAY,OAAA,EAAQ;AAAA,QACtB;AAAA,MACF;AAMA,MAAA,mBAAA,CAAoB,IAAA,iBAAK,IAAI,IAAA,EAAM,CAAA;AAAA,IACrC,CAAA,MAAO;AACL,MAAA,mBAAA,CAAoB,MAAA,EAAO;AAAA,IAC7B;AAAA,EACF,CAAA,EAAG;AAAA,IACD,mBAAA;AAAA,IACA,YAAA;AAAA,IACA,WAAA;AAAA,IACA,kBAAA;AAAA,IACA;AAAA,GACD,CAAA;AACH;;;;"}
1
+ {"version":3,"file":"disconnectedUsers.esm.js","sources":["../../../src/components/AutoLogout/disconnectedUsers.ts"],"sourcesContent":["/*\n * Copyright 2023 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\nimport { IdentityApi } from '@backstage/core-plugin-api';\nimport { useEffect } from 'react';\n\nimport { TimestampStore } from './timestampStore';\n\nexport const LAST_SEEN_ONLINE_STORAGE_KEY =\n '@backstage/autologout:lastSeenOnline';\n\nexport type UseLogoutDisconnectedUserEffectProps = {\n enableEffect: boolean;\n autologoutIsEnabled: boolean;\n isLoggedIn: boolean | null;\n idleTimeoutSeconds: number;\n lastSeenOnlineStore: TimestampStore;\n identityApi: IdentityApi;\n};\n\nexport const useLogoutDisconnectedUserEffect = ({\n enableEffect,\n autologoutIsEnabled,\n isLoggedIn,\n idleTimeoutSeconds,\n lastSeenOnlineStore,\n identityApi,\n}: UseLogoutDisconnectedUserEffectProps) => {\n useEffect(() => {\n /**\n * Considers disconnected users as inactive users.\n * If all Backstage tabs are closed and idleTimeoutMinutes are passed then logout the user anyway.\n */\n const shouldCheckDisconnectedUser = autologoutIsEnabled && enableEffect;\n\n // Prevent lastSeen getting deleted before logged state is checked\n if (isLoggedIn === null) {\n return;\n }\n\n if (!shouldCheckDisconnectedUser || !isLoggedIn) {\n lastSeenOnlineStore.delete();\n return;\n }\n\n const lastSeenOnline = lastSeenOnlineStore.get();\n if (lastSeenOnline) {\n const now = new Date();\n const nowSeconds = Math.ceil(now.getTime() / 1000);\n const lastSeenOnlineSeconds = Math.ceil(lastSeenOnline.getTime() / 1000);\n if (nowSeconds - lastSeenOnlineSeconds > idleTimeoutSeconds) {\n lastSeenOnlineStore.delete();\n identityApi.signOut();\n return;\n }\n }\n lastSeenOnlineStore.save(new Date());\n }, [\n autologoutIsEnabled,\n enableEffect,\n isLoggedIn,\n identityApi,\n idleTimeoutSeconds,\n lastSeenOnlineStore,\n ]);\n};\n"],"names":[],"mappings":";;AAoBO,MAAM,4BAAA,GACX;AAWK,MAAM,kCAAkC,CAAC;AAAA,EAC9C,YAAA;AAAA,EACA,mBAAA;AAAA,EACA,UAAA;AAAA,EACA,kBAAA;AAAA,EACA,mBAAA;AAAA,EACA;AACF,CAAA,KAA4C;AAC1C,EAAA,SAAA,CAAU,MAAM;AAKd,IAAA,MAAM,8BAA8B,mBAAA,IAAuB,YAAA;AAG3D,IAAA,IAAI,eAAe,IAAA,EAAM;AACvB,MAAA;AAAA,IACF;AAEA,IAAA,IAAI,CAAC,2BAAA,IAA+B,CAAC,UAAA,EAAY;AAC/C,MAAA,mBAAA,CAAoB,MAAA,EAAO;AAC3B,MAAA;AAAA,IACF;AAEA,IAAA,MAAM,cAAA,GAAiB,oBAAoB,GAAA,EAAI;AAC/C,IAAA,IAAI,cAAA,EAAgB;AAClB,MAAA,MAAM,GAAA,uBAAU,IAAA,EAAK;AACrB,MAAA,MAAM,aAAa,IAAA,CAAK,IAAA,CAAK,GAAA,CAAI,OAAA,KAAY,GAAI,CAAA;AACjD,MAAA,MAAM,wBAAwB,IAAA,CAAK,IAAA,CAAK,cAAA,CAAe,OAAA,KAAY,GAAI,CAAA;AACvE,MAAA,IAAI,UAAA,GAAa,wBAAwB,kBAAA,EAAoB;AAC3D,QAAA,mBAAA,CAAoB,MAAA,EAAO;AAC3B,QAAA,WAAA,CAAY,OAAA,EAAQ;AACpB,QAAA;AAAA,MACF;AAAA,IACF;AACA,IAAA,mBAAA,CAAoB,IAAA,iBAAK,IAAI,IAAA,EAAM,CAAA;AAAA,EACrC,CAAA,EAAG;AAAA,IACD,mBAAA;AAAA,IACA,YAAA;AAAA,IACA,UAAA;AAAA,IACA,WAAA;AAAA,IACA,kBAAA;AAAA,IACA;AAAA,GACD,CAAA;AACH;;;;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@backstage/core-components",
3
- "version": "0.18.11-next.0",
3
+ "version": "0.18.11-next.1",
4
4
  "description": "Core components used by Backstage plugins and apps",
5
5
  "backstage": {
6
6
  "role": "web-library"
@@ -67,7 +67,7 @@
67
67
  },
68
68
  "dependencies": {
69
69
  "@backstage/config": "1.3.8",
70
- "@backstage/core-plugin-api": "1.12.6",
70
+ "@backstage/core-plugin-api": "1.12.7-next.0",
71
71
  "@backstage/errors": "1.3.1",
72
72
  "@backstage/theme": "0.7.3",
73
73
  "@backstage/version-bridge": "1.0.12",
@@ -110,10 +110,10 @@
110
110
  "zod": "^3.25.76 || ^4.0.0"
111
111
  },
112
112
  "devDependencies": {
113
- "@backstage/app-defaults": "1.7.9-next.0",
114
- "@backstage/cli": "0.36.3-next.0",
115
- "@backstage/core-app-api": "1.20.1",
116
- "@backstage/test-utils": "1.7.18",
113
+ "@backstage/app-defaults": "1.7.9-next.1",
114
+ "@backstage/cli": "0.36.3-next.1",
115
+ "@backstage/core-app-api": "1.20.2-next.0",
116
+ "@backstage/test-utils": "1.7.19-next.0",
117
117
  "@testing-library/dom": "^10.0.0",
118
118
  "@testing-library/jest-dom": "^6.0.0",
119
119
  "@testing-library/user-event": "^14.0.0",