From 2f830abd81b17d915ed537e03926cee193e24471 Mon Sep 17 00:00:00 2001 From: sanjeevz3009 Date: Wed, 7 Aug 2024 15:14:42 +0100 Subject: [PATCH 1/7] Added session timeout --- eq-author/src/App/MeContext.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/eq-author/src/App/MeContext.js b/eq-author/src/App/MeContext.js index 38a7c38e4f..c7a3c34256 100644 --- a/eq-author/src/App/MeContext.js +++ b/eq-author/src/App/MeContext.js @@ -93,6 +93,24 @@ const ContextProvider = ({ history, client, children }) => { auth.onAuthStateChanged((user) => { setFirebaseUser(user); setAwaitingFirebase(false); + auth.onAuthStateChanged((user) => { + let sessionTimeout = null; + if (user === null || user === undefined) { + sessionTimeout && clearTimeout(sessionTimeout); + sessionTimeout = null; + } else { + user.getIdTokenResult().then((idTokenResult) => { + const authTime = idTokenResult.claims.auth_time * 1000; + const sessionDuration = 10000; // 10 seconds + const millisecondsUntilExpiration = + sessionDuration - (Date.now() - authTime); + sessionTimeout = setTimeout( + () => auth.signOut(), + millisecondsUntilExpiration + ); + }); + } + }); }); }, []); From 4fc57b1f396d9a058b218fc7d1e3e5f2dbb2b9c4 Mon Sep 17 00:00:00 2001 From: sanjeevz3009 Date: Thu, 8 Aug 2024 12:28:22 +0100 Subject: [PATCH 2/7] Session duration of 7 days added --- eq-author/src/App/MeContext.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/eq-author/src/App/MeContext.js b/eq-author/src/App/MeContext.js index c7a3c34256..1571604849 100644 --- a/eq-author/src/App/MeContext.js +++ b/eq-author/src/App/MeContext.js @@ -101,9 +101,12 @@ const ContextProvider = ({ history, client, children }) => { } else { user.getIdTokenResult().then((idTokenResult) => { const authTime = idTokenResult.claims.auth_time * 1000; - const sessionDuration = 10000; // 10 seconds + // Seven days in milliseconds + // const sessionDuration = 1000 * 60 * 60 * 24 * 7; + const sessionDuration = 20000; // 10 seconds const millisecondsUntilExpiration = sessionDuration - (Date.now() - authTime); + console.log(millisecondsUntilExpiration); sessionTimeout = setTimeout( () => auth.signOut(), millisecondsUntilExpiration From bd5b3865ea06a3f572d163182e5fe6151a757af9 Mon Sep 17 00:00:00 2001 From: sanjeevz3009 Date: Thu, 8 Aug 2024 12:31:26 +0100 Subject: [PATCH 3/7] Session duration set to 7 days --- eq-author/src/App/MeContext.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/eq-author/src/App/MeContext.js b/eq-author/src/App/MeContext.js index 1571604849..ab393a7c57 100644 --- a/eq-author/src/App/MeContext.js +++ b/eq-author/src/App/MeContext.js @@ -102,11 +102,11 @@ const ContextProvider = ({ history, client, children }) => { user.getIdTokenResult().then((idTokenResult) => { const authTime = idTokenResult.claims.auth_time * 1000; // Seven days in milliseconds - // const sessionDuration = 1000 * 60 * 60 * 24 * 7; - const sessionDuration = 20000; // 10 seconds + const sessionDuration = 1000 * 60 * 60 * 24 * 7; + // const sessionDuration = 20000; // 10 seconds const millisecondsUntilExpiration = sessionDuration - (Date.now() - authTime); - console.log(millisecondsUntilExpiration); + // console.log(millisecondsUntilExpiration); sessionTimeout = setTimeout( () => auth.signOut(), millisecondsUntilExpiration From 9bb8528a7961bc37d5e5251bc73167ff15788818 Mon Sep 17 00:00:00 2001 From: sanjeevz3009 Date: Fri, 9 Aug 2024 13:20:25 +0100 Subject: [PATCH 4/7] Duplicate code line removed --- eq-author/src/App/MeContext.js | 40 ++++++++++++++++------------------ 1 file changed, 19 insertions(+), 21 deletions(-) diff --git a/eq-author/src/App/MeContext.js b/eq-author/src/App/MeContext.js index ab393a7c57..a3e151e911 100644 --- a/eq-author/src/App/MeContext.js +++ b/eq-author/src/App/MeContext.js @@ -93,27 +93,25 @@ const ContextProvider = ({ history, client, children }) => { auth.onAuthStateChanged((user) => { setFirebaseUser(user); setAwaitingFirebase(false); - auth.onAuthStateChanged((user) => { - let sessionTimeout = null; - if (user === null || user === undefined) { - sessionTimeout && clearTimeout(sessionTimeout); - sessionTimeout = null; - } else { - user.getIdTokenResult().then((idTokenResult) => { - const authTime = idTokenResult.claims.auth_time * 1000; - // Seven days in milliseconds - const sessionDuration = 1000 * 60 * 60 * 24 * 7; - // const sessionDuration = 20000; // 10 seconds - const millisecondsUntilExpiration = - sessionDuration - (Date.now() - authTime); - // console.log(millisecondsUntilExpiration); - sessionTimeout = setTimeout( - () => auth.signOut(), - millisecondsUntilExpiration - ); - }); - } - }); + let sessionTimeout = null; + if (user === null || user === undefined) { + sessionTimeout && clearTimeout(sessionTimeout); + sessionTimeout = null; + } else { + user.getIdTokenResult().then((idTokenResult) => { + const authTime = idTokenResult.claims.auth_time * 1000; + // Seven days in milliseconds + // const sessionDuration = 1000 * 60 * 60 * 24 * 7; + const sessionDuration = 10000; // 10 seconds + const millisecondsUntilExpiration = + sessionDuration - (Date.now() - authTime); + // console.log(millisecondsUntilExpiration); + sessionTimeout = setTimeout( + () => auth.signOut(), + millisecondsUntilExpiration + ); + }); + } }); }, []); From b641242d7b1a45d79109c22f38ec4ad4f74b6e42 Mon Sep 17 00:00:00 2001 From: sanjeevz3009 Date: Fri, 9 Aug 2024 13:35:35 +0100 Subject: [PATCH 5/7] Code cleaned up and comments added --- eq-author/src/App/MeContext.js | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/eq-author/src/App/MeContext.js b/eq-author/src/App/MeContext.js index a3e151e911..5a52878d58 100644 --- a/eq-author/src/App/MeContext.js +++ b/eq-author/src/App/MeContext.js @@ -89,23 +89,28 @@ const ContextProvider = ({ history, client, children }) => { useEffect(() => { // be aware that the return from auth.onAuthStateChanged will change on firebase ver 4.0 - // https://firebase.google.com/docs/reference/js/v8/firebase.auth.Auth#onauthstatechanged + // https://firebase.google.com/docs/reference/js/v8/firebase.auth.Auth#onauthstatechanged] + // This useEffect hook is responsible for handling the authentication state changes in Firebase. + // It listens for changes in the authentication state using the onAuthStateChanged method. + // When the authentication state changes, it updates the firebaseUser state and sets awaitingFirebase to false. auth.onAuthStateChanged((user) => { setFirebaseUser(user); setAwaitingFirebase(false); + // It also sets up a session timeout for the user if they are authenticated. + // If the user is not authenticated, the session timeout is cleared using clearTimeout. let sessionTimeout = null; if (user === null || user === undefined) { sessionTimeout && clearTimeout(sessionTimeout); sessionTimeout = null; } else { + // If the user is authenticated, it retrieves the ID token result and calculates the session duration. user.getIdTokenResult().then((idTokenResult) => { const authTime = idTokenResult.claims.auth_time * 1000; - // Seven days in milliseconds - // const sessionDuration = 1000 * 60 * 60 * 24 * 7; - const sessionDuration = 10000; // 10 seconds + // The session duration is set to 7 days in this example. + const sessionDuration = 1000 * 60 * 60 * 24 * 7; const millisecondsUntilExpiration = sessionDuration - (Date.now() - authTime); - // console.log(millisecondsUntilExpiration); + // It then sets up a session timeout using setTimeout, which will automatically sign out the user after the session duration expires. sessionTimeout = setTimeout( () => auth.signOut(), millisecondsUntilExpiration From 8a2990ea81841daca57b112244b60e02863ff74f Mon Sep 17 00:00:00 2001 From: sanjeevz3009 Date: Mon, 12 Aug 2024 16:48:57 +0100 Subject: [PATCH 6/7] Rewritten the sessionDuration calculation for 7 days in milliseconds (it now multiplies from days, hours, minutes, seconds and then milliseconds) --- eq-author/src/App/MeContext.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/eq-author/src/App/MeContext.js b/eq-author/src/App/MeContext.js index 5a52878d58..508c6019ac 100644 --- a/eq-author/src/App/MeContext.js +++ b/eq-author/src/App/MeContext.js @@ -107,7 +107,7 @@ const ContextProvider = ({ history, client, children }) => { user.getIdTokenResult().then((idTokenResult) => { const authTime = idTokenResult.claims.auth_time * 1000; // The session duration is set to 7 days in this example. - const sessionDuration = 1000 * 60 * 60 * 24 * 7; + const sessionDuration = 7 * 24 * 60 * 60 * 1000; const millisecondsUntilExpiration = sessionDuration - (Date.now() - authTime); // It then sets up a session timeout using setTimeout, which will automatically sign out the user after the session duration expires. From 20315ac5bd3eaec9dc8e6e90ff47535408e985b1 Mon Sep 17 00:00:00 2001 From: sanjeevz3009 Date: Wed, 14 Aug 2024 14:45:13 +0100 Subject: [PATCH 7/7] Comments updated and the session duration calculation format is rearranged --- eq-author/src/App/MeContext.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/eq-author/src/App/MeContext.js b/eq-author/src/App/MeContext.js index 508c6019ac..58855fc070 100644 --- a/eq-author/src/App/MeContext.js +++ b/eq-author/src/App/MeContext.js @@ -89,7 +89,7 @@ const ContextProvider = ({ history, client, children }) => { useEffect(() => { // be aware that the return from auth.onAuthStateChanged will change on firebase ver 4.0 - // https://firebase.google.com/docs/reference/js/v8/firebase.auth.Auth#onauthstatechanged] + // https://firebase.google.com/docs/reference/js/v8/firebase.auth.Auth#onauthstatechanged // This useEffect hook is responsible for handling the authentication state changes in Firebase. // It listens for changes in the authentication state using the onAuthStateChanged method. // When the authentication state changes, it updates the firebaseUser state and sets awaitingFirebase to false. @@ -106,8 +106,9 @@ const ContextProvider = ({ history, client, children }) => { // If the user is authenticated, it retrieves the ID token result and calculates the session duration. user.getIdTokenResult().then((idTokenResult) => { const authTime = idTokenResult.claims.auth_time * 1000; - // The session duration is set to 7 days in this example. - const sessionDuration = 7 * 24 * 60 * 60 * 1000; + // The session duration is set to 7 days. + // The format of the session duration calculation is in milliseconds/seconds/minutes/hours/days. + const sessionDuration = 1000 * 60 * 60 * 24 * 7; // 604,800,000 milliseconds const millisecondsUntilExpiration = sessionDuration - (Date.now() - authTime); // It then sets up a session timeout using setTimeout, which will automatically sign out the user after the session duration expires.