From 4c28f5127b727461e72c1c834da8facd931fa55c Mon Sep 17 00:00:00 2001 From: gazillion101 Date: Sat, 8 Apr 2023 08:35:57 -0400 Subject: [PATCH] [#230] 230, 231 --- web/js/packages/admin/src/Auth.tsx | 85 ++++++++++--------- .../packages/portal/src/App/DatascopeForm.tsx | 2 +- .../portal/src/App/EditDatascopes.tsx | 11 +-- web/js/packages/portal/src/App/Menu.tsx | 73 ++++++++-------- web/js/packages/portal/src/Auth.tsx | 5 +- .../__snapshots__/AddDatascope.test.js.snap | 20 ++--- 6 files changed, 101 insertions(+), 95 deletions(-) diff --git a/web/js/packages/admin/src/Auth.tsx b/web/js/packages/admin/src/Auth.tsx index 4e49ede2..17e3ff2f 100644 --- a/web/js/packages/admin/src/Auth.tsx +++ b/web/js/packages/admin/src/Auth.tsx @@ -1,7 +1,7 @@ import React, { useState, useEffect, useRef } from 'react'; import Modal from 'react-bootstrap/Modal' import Button from 'react-bootstrap/Button' -import {useInitialize} from './Utils/CustomHooks' +import { useInitialize } from './Utils/CustomHooks' const sessionTimeoutMin = 20 @@ -9,50 +9,51 @@ const sessionTimeoutMin = 20 export default function Auth() { let active = useRef(false) let [show, setShow] = useState(false) - let lastSessionUpdate = useRef( Date.now()/1000 ) + let lastSessionUpdate = useRef(Date.now() / 1000) let showtimer = useRef(0) - let timeoutId = useRef(0) + let timeoutId = useRef(0) let redirectPublicWebsite = () => { return "/app/logout" } let revalidate = () => { - if(!active.current) - return + let token = window.sessionStorage.getItem("Session"); - lastSessionUpdate.current = Date.now()/1000 + lastSessionUpdate.current = Date.now() / 1000 if (token === null) { console.log("not authenticated"); window.location.href = redirectPublicWebsite(); return; } + if (!active.current) + return fetch(window.location.origin + "/auth/refresh", { cache: 'no-cache', method: 'POST', - body: "", + body: "", headers: { Authorization: "Bearer " + token, - Cache: "no-cache" + Cache: "no-cache" }, }).then(response => { let d = new Date() - + if (!response.ok) { console.log("Auth fetch error, at: " + d.toLocaleString()) sessionStorage.removeItem("Session") - setShow( true ) + setShow(true) } else { response.json().then(js => { if (js.status !== "OK") { console.log("Status not OK, " + JSON.stringify(js) + ", at: " + d.toLocaleString()) sessionStorage.removeItem("Session") console.log("Pop the message") - showtimer.current = window.setTimeout(t => { + showtimer.current = window.setTimeout(t => { console.log("Don't wait until popup shows, close the view") window.location.href = redirectPublicWebsite() }, 30000) - if(window.location.pathname !== '/') - setShow(true ) + if (window.location.pathname !== '/') + setShow(true) } else { sessionStorage.setItem("Session", js.token) } @@ -65,74 +66,74 @@ export default function Auth() { } let onTimeout = () => { let d = new Date() - console.log("in onTimeout, revalidate possible expiration, %s", d.toLocaleString() ) - console.log("Elapsed: " + (Date.now()/1000 - lastSessionUpdate.current)) + console.log("in onTimeout, revalidate possible expiration, %s", d.toLocaleString()) + console.log("Elapsed: " + (Date.now() / 1000 - lastSessionUpdate.current)) revalidate(); } let onActivity = (e) => { //console.log("in onActivity") - let now = Date.now()/1000 + let now = Date.now() / 1000 if (now - lastSessionUpdate.current >= 30) { - - lastSessionUpdate.current =now + + lastSessionUpdate.current = now revalidate(); } - + clearTimeout(timeoutId.current) - timeoutId.current = window.setTimeout(onTimeout, 1000*(sessionTimeoutMin*60 + 5)) + timeoutId.current = window.setTimeout(onTimeout, 1000 * (sessionTimeoutMin * 60 + 5)) } - let onReauthenticate = (e) =>{ + let onReauthenticate = (e) => { console.log("Reauthentication requested on failed fetch!") revalidate() - } - useInitialize( () => { + } + useInitialize(() => { lastSessionUpdate.current = 0 showtimer.current = 0 - window.addEventListener("mousemove", onActivity, {passive: true} ) - window.addEventListener("keydown", onActivity, {passive: true} ) - window.addEventListener("wheel", onActivity, {passive: true} ) - window.addEventListener("DOMMouseScroll", onActivity, {passive: true} ) - window.addEventListener("mouseWheel", onActivity, {passive: true} ) - window.addEventListener("mousedown", onActivity, {passive: true} ) - window.addEventListener("touchstart", onActivity, {passive: true} ) - window.addEventListener("touchmove", onActivity, {passive: true} ) - window.addEventListener("MSPointerDown", onActivity, {passive: true} ) - window.addEventListener("MSPointerMove", onActivity, {passive: true} ) - window.addEventListener("visibilitychange", onActivity, {passive: true} ) + window.addEventListener("mousemove", onActivity, { passive: true }) + window.addEventListener("keydown", onActivity, { passive: true }) + window.addEventListener("wheel", onActivity, { passive: true }) + window.addEventListener("DOMMouseScroll", onActivity, { passive: true }) + window.addEventListener("mouseWheel", onActivity, { passive: true }) + window.addEventListener("mousedown", onActivity, { passive: true }) + window.addEventListener("touchstart", onActivity, { passive: true }) + window.addEventListener("touchmove", onActivity, { passive: true }) + window.addEventListener("MSPointerDown", onActivity, { passive: true }) + window.addEventListener("MSPointerMove", onActivity, { passive: true }) + window.addEventListener("visibilitychange", onActivity, { passive: true }) window.document.addEventListener('reauthenticate', onReauthenticate); }) useEffect(() => { - - if(timeoutId.current) + + if (timeoutId.current) clearTimeout(timeoutId.current) - timeoutId.current = window.setTimeout(onTimeout, 1000*(sessionTimeoutMin*60 + 5)) + timeoutId.current = window.setTimeout(onTimeout, 1000 * (sessionTimeoutMin * 60 + 5)) revalidate(); active.current = true handleShow() return () => { active.current = false - if(timeoutId.current) + if (timeoutId.current) clearTimeout(timeoutId.current) - timeoutId.current = 0 + timeoutId.current = 0 } }, []) let onShow = () => { - if(showtimer.current) + if (showtimer.current) clearTimeout(showtimer.current) showtimer.current = 0 - window.setTimeout(t => { + window.setTimeout(t => { console.log("kill the popup from popup") window.location.href = redirectPublicWebsite() }, 5000) } - let handleClose = () => { setShow(true ); window.location.href = redirectPublicWebsite(); } + let handleClose = () => { setShow(true); window.location.href = redirectPublicWebsite(); } let handleShow = () => { setShow(false); } return (
diff --git a/web/js/packages/portal/src/App/DatascopeForm.tsx b/web/js/packages/portal/src/App/DatascopeForm.tsx index d9973ac1..0277cb25 100644 --- a/web/js/packages/portal/src/App/DatascopeForm.tsx +++ b/web/js/packages/portal/src/App/DatascopeForm.tsx @@ -358,7 +358,7 @@ const DatascopeForm: React.FC = (props) => { Database Name { diff --git a/web/js/packages/portal/src/App/EditDatascopes.tsx b/web/js/packages/portal/src/App/EditDatascopes.tsx index 01952409..74a95359 100644 --- a/web/js/packages/portal/src/App/EditDatascopes.tsx +++ b/web/js/packages/portal/src/App/EditDatascopes.tsx @@ -395,19 +395,20 @@ export default function EditDatascopes() { if (js.status === "OK") { setAlert( setAlert(<>)} dismissible> - Ghost Database {dbname} updated successfully! + Ghost Database {dbname} deleted successfully! ) capi.getConnections(setSpinner, setConns, setAlert, remap, () => { com.getDatascopes(setSpinner, setAlert, setDatascopes, () => { - setSelectedDatascope(t) + if(slatedToDelete !== t) + setSelectedDatascope(t) }) }) } else { setAlert( setAlert(<>)} dismissible> - Error updating {dbname}: {js.errormessage} + Error deleting {dbname}: {js.errormessage} ) } @@ -420,7 +421,7 @@ export default function EditDatascopes() { resp != null && resp.text().then(t => setAlert( setAlert(<>)} dismissible> - Error updating {dbname}: {t} + Error deleting {dbname}: {t} ) ) @@ -541,7 +542,7 @@ export default function EditDatascopes() { - {(selectedDatascope !== "" && selectedDatascope !== "xxx") && + {(selectedDatascope !== "") &&
{selectedDatascopeDetails?.name}
diff --git a/web/js/packages/portal/src/App/Menu.tsx b/web/js/packages/portal/src/App/Menu.tsx index cac5f89e..ee6c8ca8 100644 --- a/web/js/packages/portal/src/App/Menu.tsx +++ b/web/js/packages/portal/src/App/Menu.tsx @@ -12,10 +12,8 @@ import * as hex from '../Utils/Hex' function Menu() { let [avatar, setAvatar] = useState('/avatar.png') // eslint-disable-line no-unused-vars - let [show, setShow] = useState(false) + let [show, setShow] = useState(false) - let p = hex.HexStringToByteArray(com.getTokenProperty("picture")) - let picture = hex.StringFromUTF8Array(p) let onAva = e => { setShow(true) } @@ -24,16 +22,21 @@ function Menu() { } let getGroups = () => { let gr = com.getTokenProperty("groups") - if(gr === null || gr === undefined) + if (gr === null || gr === undefined) return "" return gr.join(", ") } - if(undefined !== picture && avatar != picture) { + let pic = com.getTokenProperty("picture") + + let p = pic && hex.HexStringToByteArray(pic) + let picture = p && hex.StringFromUTF8Array(p) + + if (undefined !== picture && avatar != picture) { setAvatar(picture) } - return ( -
- + return ( +
+ Current user: @@ -50,37 +53,37 @@ function Menu() { - - - + + + + + + logo + + + + + logo + + - - logo - - - - - logo - - + - + + + avatar + + + -
- ) +
+ ) }; export default Menu \ No newline at end of file diff --git a/web/js/packages/portal/src/Auth.tsx b/web/js/packages/portal/src/Auth.tsx index 4e49ede2..8689c880 100644 --- a/web/js/packages/portal/src/Auth.tsx +++ b/web/js/packages/portal/src/Auth.tsx @@ -17,8 +17,7 @@ export default function Auth() { return "/app/logout" } let revalidate = () => { - if(!active.current) - return + let token = window.sessionStorage.getItem("Session"); lastSessionUpdate.current = Date.now()/1000 if (token === null) { @@ -26,6 +25,8 @@ export default function Auth() { window.location.href = redirectPublicWebsite(); return; } + if(!active.current) + return fetch(window.location.origin + "/auth/refresh", { cache: 'no-cache', method: 'POST', diff --git a/web/js/packages/portal/src/tests/__snapshots__/AddDatascope.test.js.snap b/web/js/packages/portal/src/tests/__snapshots__/AddDatascope.test.js.snap index c0d10673..4daa7ade 100644 --- a/web/js/packages/portal/src/tests/__snapshots__/AddDatascope.test.js.snap +++ b/web/js/packages/portal/src/tests/__snapshots__/AddDatascope.test.js.snap @@ -156,7 +156,7 @@ Object {