diff --git a/package-lock.json b/package-lock.json index 4e88dd4..4e5cfe6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "ucentral-client", - "version": "0.9.9", + "version": "0.9.10", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "ucentral-client", - "version": "0.9.9", + "version": "0.9.10", "dependencies": { "@coreui/coreui": "^3.4.0", "@coreui/icons": "^2.0.1", diff --git a/package.json b/package.json index 5362ec2..ac98d83 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ucentral-client", - "version": "0.9.9", + "version": "0.9.10", "private": true, "dependencies": { "@coreui/coreui": "^3.4.0", diff --git a/public/config.json b/public/config.json index c9d97c3..2939ef8 100644 --- a/public/config.json +++ b/public/config.json @@ -1,4 +1,4 @@ { - "DEFAULT_GATEWAY_URL": "https://ucentral.dpaas.arilia.com:16001", + "DEFAULT_UCENTRALSEC_URL": "https://ucentral.dpaas.arilia.com:16001", "ALLOW_GATEWAY_CHANGE": false } diff --git a/src/App.js b/src/App.js index 32f0cad..5bd0b1d 100644 --- a/src/App.js +++ b/src/App.js @@ -3,6 +3,7 @@ import { HashRouter, Switch } from 'react-router-dom'; import 'scss/style.scss'; import Router from 'router'; import { AuthProvider } from 'contexts/AuthProvider'; +import { checkIfJson } from 'utils/helper'; const loading = (
@@ -12,9 +13,10 @@ const loading = ( const App = () => { const storageToken = sessionStorage.getItem('access_token'); + const apiEndpoints = checkIfJson(sessionStorage.getItem('gateway_endpoints')) ? JSON.parse(sessionStorage.getItem('gateway_endpoints')) : {}; return ( - + diff --git a/src/components/BlinkModal/index.js b/src/components/BlinkModal/index.js index 3780f7a..6eae13d 100644 --- a/src/components/BlinkModal/index.js +++ b/src/components/BlinkModal/index.js @@ -29,7 +29,7 @@ import styles from './index.module.scss'; const BlinkModal = ({ show, toggleModal }) => { const { t } = useTranslation(); - const { currentToken } = useAuth(); + const { currentToken, endpoints } = useAuth(); const { deviceSerialNumber } = useDevice(); const [isNow, setIsNow] = useState(false); const [waiting, setWaiting] = useState(false); @@ -73,7 +73,7 @@ const BlinkModal = ({ show, toggleModal }) => { }; axiosInstance - .post(`/device/${encodeURIComponent(deviceSerialNumber)}/leds`, parameters, { headers }) + .post(`${endpoints.ucentralgw}/api/v1/device/${encodeURIComponent(deviceSerialNumber)}/leds`, parameters, { headers }) .then(() => { setResult('success'); }) diff --git a/src/components/CommandHistory/index.js b/src/components/CommandHistory/index.js index d4bd7d9..664f2d2 100644 --- a/src/components/CommandHistory/index.js +++ b/src/components/CommandHistory/index.js @@ -27,7 +27,7 @@ import styles from './index.module.scss'; const DeviceCommands = () => { const { t } = useTranslation(); - const { currentToken } = useAuth(); + const { currentToken, endpoints } = useAuth(); const { deviceSerialNumber } = useDevice(); // Wifiscan result related const [chosenWifiScan, setChosenWifiScan] = useState(null); @@ -110,7 +110,7 @@ const DeviceCommands = () => { axiosInstance .get( - `/commands?serialNumber=${encodeURIComponent(deviceSerialNumber)}${extraParams}`, + `${endpoints.ucentralgw}/api/v1/commands?serialNumber=${encodeURIComponent(deviceSerialNumber)}${extraParams}`, options, ) .then((response) => { @@ -133,7 +133,7 @@ const DeviceCommands = () => { }; axiosInstance - .get(`/file/${uuid}?serialNumber=${deviceSerialNumber}`, options) + .get(`${endpoints.ucentralgw}/api/v1/file/${uuid}?serialNumber=${deviceSerialNumber}`, options) .then((response) => { const blob = new Blob([response.data], { type: 'application/octet-stream' }); const link = document.createElement('a'); @@ -154,7 +154,7 @@ const DeviceCommands = () => { }, }; return axiosInstance - .delete(`/command/${uuidDelete}`, options) + .delete(`${endpoints.ucentralgw}/api/v1/command/${uuidDelete}`, options) .then(() => { deleteCommandFromList(uuidDelete); setUuidDelete(''); diff --git a/src/components/ConfigureModal/index.js b/src/components/ConfigureModal/index.js index e588d4d..456638a 100644 --- a/src/components/ConfigureModal/index.js +++ b/src/components/ConfigureModal/index.js @@ -27,7 +27,7 @@ import styles from './index.module.scss'; const ConfigureModal = ({ show, toggleModal }) => { const { t } = useTranslation(); - const { currentToken } = useAuth(); + const { currentToken, endpoints } = useAuth(); const { deviceSerialNumber } = useDevice(); const [hadSuccess, setHadSuccess] = useState(false); const [hadFailure, setHadFailure] = useState(false); @@ -84,7 +84,7 @@ const ConfigureModal = ({ show, toggleModal }) => { }; axiosInstance - .post(`/device/${encodeURIComponent(deviceSerialNumber)}/configure`, parameters, { headers }) + .post(`${endpoints.ucentralgw}/api/v1/device/${encodeURIComponent(deviceSerialNumber)}/configure`, parameters, { headers }) .then(() => { setHadSuccess(true); }) diff --git a/src/components/DeleteLogModal/index.js b/src/components/DeleteLogModal/index.js index 25bc76f..2096505 100644 --- a/src/components/DeleteLogModal/index.js +++ b/src/components/DeleteLogModal/index.js @@ -13,7 +13,7 @@ import styles from './index.module.scss'; const DeleteLogModal = ({ show, toggle, object }) => { const { t } = useTranslation(); - const { currentToken } = useAuth(); + const { currentToken, endpoints } = useAuth(); const { deviceSerialNumber } = useDevice(); const [loading, setLoading] = useState(false); const [maxDate, setMaxDate] = useState(new Date().toString()); @@ -37,7 +37,7 @@ const DeleteLogModal = ({ show, toggle, object }) => { }, }; return axiosInstance - .delete(`/device/${deviceSerialNumber}/${object}`, options) + .delete(`${endpoints.ucentralgw}/api/v1/device/${deviceSerialNumber}/${object}`, options) .then(() => {}) .catch(() => {}) .finally(() => { diff --git a/src/components/DeviceActionCard/index.js b/src/components/DeviceActionCard/index.js index f0b17f9..3c2713c 100644 --- a/src/components/DeviceActionCard/index.js +++ b/src/components/DeviceActionCard/index.js @@ -17,7 +17,7 @@ import styles from './index.module.scss'; const DeviceActions = () => { const { t } = useTranslation(); - const { currentToken } = useAuth(); + const { currentToken, endpoints } = useAuth(); const { deviceSerialNumber } = useDevice(); const [showRebootModal, setShowRebootModal] = useState(false); const [showBlinkModal, setShowBlinkModal] = useState(false); @@ -66,7 +66,7 @@ const DeviceActions = () => { }; axiosInstance - .get(`/device/${encodeURIComponent(deviceSerialNumber)}/rtty`, options) + .get(`${endpoints.ucentralgw}/api/v1/device/${encodeURIComponent(deviceSerialNumber)}/rtty`, options) .then((response) => { const url = `https://${response.data.server}:${response.data.viewport}/connect/${response.data.connectionId}`; const newWindow = window.open(url, '_blank', 'noopener,noreferrer'); diff --git a/src/components/DeviceConfiguration/index.js b/src/components/DeviceConfiguration/index.js index b448438..3644a3b 100644 --- a/src/components/DeviceConfiguration/index.js +++ b/src/components/DeviceConfiguration/index.js @@ -25,7 +25,7 @@ import styles from './index.module.scss'; const DeviceConfiguration = () => { const { t } = useTranslation(); - const { currentToken } = useAuth(); + const { currentToken, endpoints } = useAuth(); const { deviceSerialNumber } = useDevice(); const [collapse, setCollapse] = useState(false); const [showModal, setShowModal] = useState(false); @@ -49,7 +49,7 @@ const DeviceConfiguration = () => { }; axiosInstance - .get(`/device/${encodeURIComponent(deviceSerialNumber)}`, options) + .get(`${endpoints.ucentralgw}/api/v1/device/${encodeURIComponent(deviceSerialNumber)}`, options) .then((response) => { setDevice(response.data); }) diff --git a/src/components/DeviceHealth/index.js b/src/components/DeviceHealth/index.js index 2743191..ff4df40 100644 --- a/src/components/DeviceHealth/index.js +++ b/src/components/DeviceHealth/index.js @@ -26,7 +26,7 @@ import styles from './index.module.scss'; const DeviceHealth = () => { const { t } = useTranslation(); - const { currentToken } = useAuth(); + const { currentToken, endpoints } = useAuth(); const { deviceSerialNumber } = useDevice(); const [collapse, setCollapse] = useState(false); const [details, setDetails] = useState([]); @@ -87,7 +87,7 @@ const DeviceHealth = () => { } axiosInstance - .get(`/device/${encodeURIComponent(deviceSerialNumber)}/healthchecks${extraParams}`, options) + .get(`${endpoints.ucentralgw}/api/v1/device/${encodeURIComponent(deviceSerialNumber)}/healthchecks${extraParams}`, options) .then((response) => { setHealthChecks(response.data.values); }) diff --git a/src/components/DeviceListTable/index.js b/src/components/DeviceListTable/index.js index 4a74939..127d3ec 100644 --- a/src/components/DeviceListTable/index.js +++ b/src/components/DeviceListTable/index.js @@ -29,7 +29,7 @@ import styles from './index.module.scss'; const DeviceList = () => { const { t } = useTranslation(); - const { currentToken } = useAuth(); + const { currentToken, endpoints } = useAuth(); const [loadedSerials, setLoadedSerials] = useState(false); const [serialNumbers, setSerialNumbers] = useState([]); const [page, setPage] = useState(0); @@ -47,7 +47,7 @@ const DeviceList = () => { }; axiosInstance - .get('/devices?serialOnly=true', { + .get(`${endpoints.ucentralgw}/api/v1/devices?serialOnly=true`, { headers, }) .then((response) => { @@ -75,7 +75,7 @@ const DeviceList = () => { .join(','); axiosInstance - .get(`/devices?deviceWithStatus=true&select=${serialsToGet}`, { + .get(`${endpoints.ucentralgw}/api/v1/devices?deviceWithStatus=true&select=${serialsToGet}`, { headers, }) .then((response) => { @@ -96,7 +96,7 @@ const DeviceList = () => { }; axiosInstance - .get(`/devices?deviceWithStatus=true&select=${encodeURIComponent(serialNumber)}`, { + .get(`${endpoints.ucentralgw}/api/v1/devices?deviceWithStatus=true&select=${encodeURIComponent(serialNumber)}`, { headers, }) .then((response) => { diff --git a/src/components/DeviceLogs/index.js b/src/components/DeviceLogs/index.js index e41f71f..3af663c 100644 --- a/src/components/DeviceLogs/index.js +++ b/src/components/DeviceLogs/index.js @@ -25,7 +25,7 @@ import styles from './index.module.scss'; const DeviceLogs = () => { const { t } = useTranslation(); - const { currentToken } = useAuth(); + const { currentToken, endpoints } = useAuth(); const { deviceSerialNumber } = useDevice(); const [collapse, setCollapse] = useState(false); const [details, setDetails] = useState([]); @@ -84,7 +84,7 @@ const DeviceLogs = () => { } axiosInstance - .get(`/device/${encodeURIComponent(deviceSerialNumber)}/logs${extraParams}`, options) + .get(`${endpoints.ucentralgw}/api/v1/device/${encodeURIComponent(deviceSerialNumber)}/logs${extraParams}`, options) .then((response) => { setLogs(response.data.values); }) diff --git a/src/components/DeviceStatusCard/index.js b/src/components/DeviceStatusCard/index.js index 7e151d2..fbaa90d 100644 --- a/src/components/DeviceStatusCard/index.js +++ b/src/components/DeviceStatusCard/index.js @@ -25,7 +25,7 @@ import styles from './index.module.scss'; const DeviceStatusCard = () => { const { t } = useTranslation(); - const { currentToken } = useAuth(); + const { currentToken, endpoints } = useAuth(); const { deviceSerialNumber } = useDevice(); const [lastStats, setLastStats] = useState(null); const [status, setStatus] = useState(null); @@ -47,11 +47,11 @@ const DeviceStatusCard = () => { }; const lastStatsRequest = axiosInstance.get( - `/device/${encodeURIComponent(deviceSerialNumber)}/statistics?lastOnly=true`, + `${endpoints.ucentralgw}/api/v1/device/${encodeURIComponent(deviceSerialNumber)}/statistics?lastOnly=true`, options, ); const statusRequest = axiosInstance.get( - `/device/${encodeURIComponent(deviceSerialNumber)}/status`, + `${endpoints.ucentralgw}/api/v1/device/${encodeURIComponent(deviceSerialNumber)}/status`, options, ); diff --git a/src/components/FactoryResetModal/index.js b/src/components/FactoryResetModal/index.js index 978f0b4..03da2d1 100644 --- a/src/components/FactoryResetModal/index.js +++ b/src/components/FactoryResetModal/index.js @@ -23,7 +23,7 @@ import styles from './index.module.scss'; const ConfigureModal = ({ show, toggleModal }) => { const { t } = useTranslation(); - const { currentToken } = useAuth(); + const { currentToken, endpoints } = useAuth(); const { deviceSerialNumber } = useDevice(); const [hadSuccess, setHadSuccess] = useState(false); const [hadFailure, setHadFailure] = useState(false); @@ -65,7 +65,7 @@ const ConfigureModal = ({ show, toggleModal }) => { }; axiosInstance - .post(`/device/${encodeURIComponent(deviceSerialNumber)}/factory`, parameters, { headers }) + .post(`${endpoints.ucentralgw}/api/v1/device/${encodeURIComponent(deviceSerialNumber)}/factory`, parameters, { headers }) .then(() => { setHadSuccess(true); }) diff --git a/src/components/FirmwareUpgradeModal/UpgradeWaitingBody.js b/src/components/FirmwareUpgradeModal/UpgradeWaitingBody.js index 4df7abe..b7d3b4d 100644 --- a/src/components/FirmwareUpgradeModal/UpgradeWaitingBody.js +++ b/src/components/FirmwareUpgradeModal/UpgradeWaitingBody.js @@ -8,7 +8,7 @@ import axiosInstance from 'utils/axiosInstance'; const UpgradeWaitingBody = ({ serialNumber }) => { const { t } = useTranslation(); - const { currentToken } = useAuth(); + const { currentToken, endpoints } = useAuth(); const [currentStep, setCurrentStep] = useState(0); const [secondsElapsed, setSecondsElapsed] = useState(0); const [labelsToShow, setLabelsToShow] = useState(['upgrade.command_submitted']); @@ -22,7 +22,7 @@ const UpgradeWaitingBody = ({ serialNumber }) => { }; axiosInstance - .get(`/device/${encodeURIComponent(serialNumber)}/status`, options) + .get(`${endpoints.ucentralgw}/api/v1/device/${encodeURIComponent(serialNumber)}/status`, options) .then((response) => response.data.connected) .catch(() => {}); }; @@ -36,7 +36,7 @@ const UpgradeWaitingBody = ({ serialNumber }) => { }; axiosInstance - .get(`/device/${encodeURIComponent(serialNumber)}`, options) + .get(`${endpoints.ucentralgw}/api/v1/device/${encodeURIComponent(serialNumber)}`, options) .then((response) => response.data.firmware) .catch(() => {}); }; diff --git a/src/components/FirmwareUpgradeModal/index.js b/src/components/FirmwareUpgradeModal/index.js index 97b27dc..26ef6fb 100644 --- a/src/components/FirmwareUpgradeModal/index.js +++ b/src/components/FirmwareUpgradeModal/index.js @@ -28,7 +28,7 @@ import UpgradeWaitingBody from './UpgradeWaitingBody'; const FirmwareUpgradeModal = ({ show, toggleModal }) => { const { t } = useTranslation(); - const { currentToken } = useAuth(); + const { currentToken, endpoints } = useAuth(); const { deviceSerialNumber } = useDevice(); const [isNow, setIsNow] = useState(true); const [waitForUpgrade, setWaitForUpgrade] = useState(false); @@ -84,7 +84,7 @@ const FirmwareUpgradeModal = ({ show, toggleModal }) => { useEffect(() => { if (deviceSerialNumber !== null && show) { const asyncGet = async () => { - const isConnected = await getDeviceConnection(deviceSerialNumber, currentToken); + const isConnected = await getDeviceConnection(deviceSerialNumber, currentToken, endpoints.ucentralgw); setDisableWaiting(!isConnected); setDeviceConnected(isConnected); }; @@ -108,7 +108,7 @@ const FirmwareUpgradeModal = ({ show, toggleModal }) => { uri: firmware, }; axiosInstance - .post(`/device/${encodeURIComponent(deviceSerialNumber)}/upgrade`, parameters, { headers }) + .post(`${endpoints.ucentralgw}/api/v1/device/${encodeURIComponent(deviceSerialNumber)}/upgrade`, parameters, { headers }) .then(() => { if (waitForUpgrade) { setShowWaitingConsole(true); diff --git a/src/components/InterfaceStatistics/LatestStatisticsModal.js b/src/components/InterfaceStatistics/LatestStatisticsModal.js index 4552554..9ae2f1e 100644 --- a/src/components/InterfaceStatistics/LatestStatisticsModal.js +++ b/src/components/InterfaceStatistics/LatestStatisticsModal.js @@ -16,7 +16,7 @@ import styles from './index.module.scss'; const LatestStatisticsModal = ({ show, toggle }) => { const { t } = useTranslation(); - const { currentToken } = useAuth(); + const { currentToken, endpoints } = useAuth(); const { deviceSerialNumber } = useDevice(); const [latestStats, setLatestStats] = useState(''); @@ -29,7 +29,7 @@ const LatestStatisticsModal = ({ show, toggle }) => { }; axiosInstance - .get(`/device/${deviceSerialNumber}/statistics?lastOnly=true`, options) + .get(`${endpoints.ucentralgw}/api/v1/device/${deviceSerialNumber}/statistics?lastOnly=true`, options) .then((response) => { setLatestStats(response.data); }) diff --git a/src/components/InterfaceStatistics/StatisticsChartList.js b/src/components/InterfaceStatistics/StatisticsChartList.js index 74229a5..52d5be1 100644 --- a/src/components/InterfaceStatistics/StatisticsChartList.js +++ b/src/components/InterfaceStatistics/StatisticsChartList.js @@ -10,7 +10,7 @@ import DeviceStatisticsChart from './DeviceStatisticsChart'; const StatisticsChartList = () => { const { t } = useTranslation(); - const { currentToken } = useAuth(); + const { currentToken, endpoints } = useAuth(); const { deviceSerialNumber } = useDevice(); const [statOptions, setStatOptions] = useState({ interfaceList: [], @@ -128,7 +128,7 @@ const StatisticsChartList = () => { }; axiosInstance - .get(`/device/${deviceSerialNumber}/statistics?newest=true&limit=50`, options) + .get(`${endpoints.ucentralgw}/api/v1/device/${deviceSerialNumber}/statistics?newest=true&limit=50`, options) .then((response) => { transformIntoDataset(response.data.data); }) diff --git a/src/components/RebootModal/index.js b/src/components/RebootModal/index.js index 3d3175f..a09299c 100644 --- a/src/components/RebootModal/index.js +++ b/src/components/RebootModal/index.js @@ -25,7 +25,7 @@ import styles from './index.module.scss'; const ActionModal = ({ show, toggleModal }) => { const { t } = useTranslation(); - const { currentToken } = useAuth(); + const { currentToken, endpoints } = useAuth(); const { deviceSerialNumber } = useDevice(); const [waiting, setWaiting] = useState(false); const [result, setResult] = useState(null); @@ -66,7 +66,7 @@ const ActionModal = ({ show, toggleModal }) => { }; axiosInstance - .post(`/device/${encodeURIComponent(deviceSerialNumber)}/reboot`, parameters, { headers }) + .post(`${endpoints.ucentralgw}/api/v1/device/${encodeURIComponent(deviceSerialNumber)}/reboot`, parameters, { headers }) .then(() => { setResult('success'); }) diff --git a/src/components/TraceModal/WaitingForTraceBody.js b/src/components/TraceModal/WaitingForTraceBody.js index a472944..557bdc4 100644 --- a/src/components/TraceModal/WaitingForTraceBody.js +++ b/src/components/TraceModal/WaitingForTraceBody.js @@ -9,7 +9,7 @@ import styles from './index.module.scss'; const WaitingForTraceBody = ({ serialNumber, commandUuid, toggle }) => { const { t } = useTranslation(); - const { currentToken } = useAuth(); + const { currentToken, endpoints } = useAuth(); const [secondsElapsed, setSecondsElapsed] = useState(0); const [waitingForFile, setWaitingForFile] = useState(true); @@ -22,7 +22,7 @@ const WaitingForTraceBody = ({ serialNumber, commandUuid, toggle }) => { }; axiosInstance - .get(`/command/${encodeURIComponent(commandUuid)}`, options) + .get(`${endpoints.ucentralgw}/api/v1/command/${encodeURIComponent(commandUuid)}`, options) .then((response) => { if (response.data.waitingForFile === 0) { setWaitingForFile(false); @@ -41,7 +41,7 @@ const WaitingForTraceBody = ({ serialNumber, commandUuid, toggle }) => { }; axiosInstance - .get(`/file/${commandUuid}?serialNumber=${serialNumber}`, options) + .get(`${endpoints.ucentralgw}/api/v1/file/${commandUuid}?serialNumber=${serialNumber}`, options) .then((response) => { const blob = new Blob([response.data], { type: 'application/octet-stream' }); const link = document.createElement('a'); diff --git a/src/components/TraceModal/index.js b/src/components/TraceModal/index.js index e47875d..54ea015 100644 --- a/src/components/TraceModal/index.js +++ b/src/components/TraceModal/index.js @@ -30,7 +30,7 @@ import styles from './index.module.scss'; const TraceModal = ({ show, toggleModal }) => { const { t } = useTranslation(); - const { currentToken } = useAuth(); + const { currentToken, endpoints } = useAuth(); const { deviceSerialNumber } = useDevice(); const [hadSuccess, setHadSuccess] = useState(false); const [hadFailure, setHadFailure] = useState(false); @@ -83,7 +83,7 @@ const TraceModal = ({ show, toggleModal }) => { }; axiosInstance - .post(`/device/${encodeURIComponent(deviceSerialNumber)}/trace`, parameters, { headers }) + .post(`${endpoints.ucentralgw}/api/v1/device/${encodeURIComponent(deviceSerialNumber)}/trace`, parameters, { headers }) .then((response) => { setHadSuccess(true); if (waitForTrace) { @@ -105,7 +105,7 @@ const TraceModal = ({ show, toggleModal }) => { useEffect(() => { if (deviceSerialNumber !== null && show) { const asyncGet = async () => { - const isConnected = await getDeviceConnection(deviceSerialNumber, currentToken); + const isConnected = await getDeviceConnection(deviceSerialNumber, currentToken, endpoints.ucentralgw); setIsDeviceConnected(isConnected); }; asyncGet(); diff --git a/src/components/WifiScanModal/index.js b/src/components/WifiScanModal/index.js index fa59949..9e4214a 100644 --- a/src/components/WifiScanModal/index.js +++ b/src/components/WifiScanModal/index.js @@ -25,7 +25,7 @@ import styles from './index.module.scss'; const WifiScanModal = ({ show, toggleModal }) => { const { t } = useTranslation(); - const { currentToken } = useAuth(); + const { currentToken, endpoints } = useAuth(); const { deviceSerialNumber } = useDevice(); const [hadSuccess, setHadSuccess] = useState(false); const [hadFailure, setHadFailure] = useState(false); @@ -100,7 +100,7 @@ const WifiScanModal = ({ show, toggleModal }) => { }; axiosInstance - .post(`/device/${encodeURIComponent(deviceSerialNumber)}/wifiscan`, parameters, { headers }) + .post(`${endpoints.ucentralgw}/api/v1/device/${encodeURIComponent(deviceSerialNumber)}/wifiscan`, parameters, { headers }) .then((response) => { const scanList = response?.data?.results?.status?.scan; diff --git a/src/contexts/AuthProvider/index.js b/src/contexts/AuthProvider/index.js index 195fdba..5f294cf 100644 --- a/src/contexts/AuthProvider/index.js +++ b/src/contexts/AuthProvider/index.js @@ -3,11 +3,12 @@ import PropTypes from 'prop-types'; const AuthContext = React.createContext(); -export const AuthProvider = ({ token, children }) => { +export const AuthProvider = ({ token, apiEndpoints, children }) => { const [currentToken, setCurrentToken] = useState(token); + const [endpoints, setEndpoints] = useState(apiEndpoints); return ( - + {children} ); @@ -16,6 +17,11 @@ export const AuthProvider = ({ token, children }) => { AuthProvider.propTypes = { token: PropTypes.string.isRequired, children: PropTypes.node.isRequired, + apiEndpoints: PropTypes.instanceOf(Object) }; +AuthProvider.defaultProps = { + apiEndpoints: {} +} + export const useAuth = () => React.useContext(AuthContext); diff --git a/src/layout/Footer/index.js b/src/layout/Footer/index.js index 769cb40..df04d39 100644 --- a/src/layout/Footer/index.js +++ b/src/layout/Footer/index.js @@ -6,7 +6,7 @@ const TheFooter = () => ( {(t) => ( -
{t('footer.version')} 0.9.9
+
{t('footer.version')} 0.9.10
{t('footer.powered_by')} diff --git a/src/layout/Header/index.js b/src/layout/Header/index.js index 7dbbbf3..a0857c4 100644 --- a/src/layout/Header/index.js +++ b/src/layout/Header/index.js @@ -16,9 +16,11 @@ import { cilAccountLogout } from '@coreui/icons'; import { logout } from 'utils/authHelper'; import routes from 'routes'; import LanguageSwitcher from 'components/LanguageSwitcher'; +import { useAuth } from 'contexts/AuthProvider'; const TheHeader = ({ showSidebar, setShowSidebar }) => { const { t, i18n } = useTranslation(); + const { currentToken, endpoints} = useAuth(); const [translatedRoutes, setTranslatedRoutes] = useState(routes); const toggleSidebar = () => { @@ -52,7 +54,7 @@ const TheHeader = ({ showSidebar, setShowSidebar }) => { - + logout(currentToken, endpoints.ucentralsec)} /> diff --git a/src/pages/LoginPage/index.js b/src/pages/LoginPage/index.js index 37120f1..102dde2 100644 --- a/src/pages/LoginPage/index.js +++ b/src/pages/LoginPage/index.js @@ -17,6 +17,7 @@ import { CAlert, CInvalidFeedback, } from '@coreui/react'; + import CIcon from '@coreui/icons-react'; import { useAuth } from 'contexts/AuthProvider'; import { cilUser, cilLockLocked, cilLink } from '@coreui/icons'; @@ -27,7 +28,7 @@ import styles from './index.module.scss'; const Login = () => { const { t } = useTranslation(); - const { setCurrentToken } = useAuth(); + const { setCurrentToken, setEndpoints } = useAuth(); const [userId, setUsername] = useState(''); const [password, setPassword] = useState(''); const [gatewayUrl, setGatewayUrl] = useState(''); @@ -36,7 +37,7 @@ const Login = () => { const [emptyPassword, setEmptyPassword] = useState(false); const [emptyGateway, setEmptyGateway] = useState(false); const [defaultConfig, setDefaultConfig] = useState({ - DEFAULT_GATEWAY_URL: '', + DEFAULT_UCENTRALSEC_URL: '', ALLOW_GATEWAY_CHANGE: true, }); const placeholderUrl = 'Gateway URL (ex: https://your-url:port)'; @@ -78,16 +79,31 @@ const Login = () => { }; const SignIn = (credentials) => { - const gatewayUrlToUse = defaultConfig.ALLOW_GATEWAY_CHANGE + let token = ''; + const uCentralSecUrl = defaultConfig.ALLOW_GATEWAY_CHANGE ? gatewayUrl - : defaultConfig.DEFAULT_GATEWAY_URL; + : defaultConfig.DEFAULT_UCENTRALSEC_URL; axiosInstance - .post(`${gatewayUrlToUse}/api/v1/oauth2`, credentials) + .post(`${uCentralSecUrl}/api/v1/oauth2`, credentials) .then((response) => { - sessionStorage.setItem('gw_url', `${gatewayUrlToUse}/api/v1`); sessionStorage.setItem('access_token', response.data.access_token); - setCurrentToken(response.data.access_token); + token = response.data.access_token; + return axiosInstance.get(`${uCentralSecUrl}/api/v1/systemEndpoints`, { headers: { + Accept: 'application/json', + Authorization: `Bearer ${response.data.access_token}`, + }}); + }) + .then ((response) => { + const endpoints = { + ucentralsec: uCentralSecUrl + }; + for (const endpoint of response.data.endpoints){ + endpoints[endpoint.type] = endpoint.uri; + } + sessionStorage.setItem('gateway_endpoints', JSON.stringify(endpoints)); + setEndpoints(endpoints); + setCurrentToken(token); }) .catch(() => { setHadError(true); @@ -113,7 +129,7 @@ const Login = () => { getDefaultConfig(); }, []); useEffect(() => { - setGatewayUrl(defaultConfig.DEFAULT_GATEWAY_URL); + setGatewayUrl(defaultConfig.DEFAULT_UCENTRALSEC_URL); }, [defaultConfig]); return ( diff --git a/src/router/index.js b/src/router/index.js index dae0eda..90746e8 100644 --- a/src/router/index.js +++ b/src/router/index.js @@ -6,13 +6,13 @@ const TheLayout = React.lazy(() => import('layout')); const Login = React.lazy(() => import('pages/LoginPage')); const Routes = () => { - const { currentToken } = useAuth(); + const { currentToken, endpoints } = useAuth(); return ( (currentToken !== '' ? : )} + render={(props) => (currentToken !== '' && Object.keys(endpoints).length !== 0 ? : )} /> ); }; diff --git a/src/utils/authHelper.js b/src/utils/authHelper.js index 8ab0ee6..9fe7edb 100644 --- a/src/utils/authHelper.js +++ b/src/utils/authHelper.js @@ -1,6 +1,19 @@ -export const logout = () => { - sessionStorage.clear(); - window.location.replace('/'); +import axiosInstance from "./axiosInstance"; + +export const logout = (token, endpoint) => { + axiosInstance + .delete(`${endpoint}/api/v1/oauth2/${token}`, { + headers: { + Accept: 'application/json', + Authorization: `Bearer ${token}`, + }, + }) + .then(() => {}) + .catch(() => {}) + .finally(() => { + sessionStorage.clear(); + window.location.replace('/'); + }) }; export const getToken = () => { diff --git a/src/utils/axiosInstance.js b/src/utils/axiosInstance.js index 2a4b342..b228240 100644 --- a/src/utils/axiosInstance.js +++ b/src/utils/axiosInstance.js @@ -16,15 +16,6 @@ axiosInstance.defaults.timeout = 30000; axiosInstance.defaults.headers.get.Accept = 'application/json'; axiosInstance.defaults.headers.post.Accept = 'application/json'; -axiosInstance.interceptors.request.use((config) => { - const newConfig = config; - const url = sessionStorage.getItem('gw_url'); - if (url !== undefined && url !== null && !newConfig.url.includes(url)) { - newConfig.url = url + newConfig.url; - } - return newConfig; -}); - axiosInstance.interceptors.response.use( // Success actions undefined, diff --git a/src/utils/deviceHelper.js b/src/utils/deviceHelper.js index 44f755f..03c302e 100644 --- a/src/utils/deviceHelper.js +++ b/src/utils/deviceHelper.js @@ -1,6 +1,6 @@ import axiosInstance from 'utils/axiosInstance'; -export default async (deviceId, token) => { +export default async (deviceId, token, endpoint) => { const options = { headers: { Accept: 'application/json', @@ -9,7 +9,7 @@ export default async (deviceId, token) => { }; return axiosInstance - .get(`/device/${encodeURIComponent(deviceId)}/status`, options) + .get(`${endpoint}/api/v1/device/${encodeURIComponent(deviceId)}/status`, options) .then((response) => response.data.connected) .catch(() => false); };