From 30510d9a737a5d55d39b4bd32273754e2a3f8f24 Mon Sep 17 00:00:00 2001 From: Tudor Stanciu Date: Wed, 23 Dec 2020 10:39:44 +0200 Subject: [PATCH] set token on http requests --- .../network/components/NetworkContainer.js | 14 +++++++- src/utils/axios.js | 6 ++-- src/utils/dataType.js | 23 ++++++++++++ src/utils/identity.js | 14 ++++++-- src/utils/localStorage.js | 36 +++++++++++++++++++ 5 files changed, 87 insertions(+), 6 deletions(-) create mode 100644 src/utils/dataType.js create mode 100644 src/utils/localStorage.js diff --git a/src/features/network/components/NetworkContainer.js b/src/features/network/components/NetworkContainer.js index 7d33f64..11e8477 100644 --- a/src/features/network/components/NetworkContainer.js +++ b/src/features/network/components/NetworkContainer.js @@ -1,9 +1,10 @@ import React, { useContext } from "react"; -import { TextField } from "@material-ui/core"; +import { TextField, Button } from "@material-ui/core"; import { ApplicationStateContext, ApplicationDispatchContext } from "../../../state/ApplicationContexts"; +import { get } from "../../../utils/axios"; const NetworkContainer = () => { const state = useContext(ApplicationStateContext); @@ -13,6 +14,14 @@ const NetworkContainer = () => { dispatchActions.onNetworkChange(prop, event.target.value); }; + const handleReadMachines = async () => { + debugger; + const url = "http://localhost:5064/resurrector-agent/machines"; + const machines = await get(url); + + const status = "done"; + }; + return ( <>
NetworkContainer
@@ -22,6 +31,9 @@ const NetworkContainer = () => { onChange={handleChange("test")} value={state.network.test} /> + ); }; diff --git a/src/utils/axios.js b/src/utils/axios.js index 70687dd..0f17e8d 100644 --- a/src/utils/axios.js +++ b/src/utils/axios.js @@ -1,10 +1,10 @@ import axios from "axios"; import i18next from "i18next"; -import { useAuthorizationToken } from "../hooks/useAuthorizationToken"; +import { getItem } from "./localStorage"; +import { storageKeys } from "./identity"; function getHeaders() { - // eslint-disable-next-line react-hooks/rules-of-hooks - const token = useAuthorizationToken(); + const token = getItem(storageKeys.TOKEN); const language = i18next.language; return { diff --git a/src/utils/dataType.js b/src/utils/dataType.js new file mode 100644 index 0000000..83b5870 --- /dev/null +++ b/src/utils/dataType.js @@ -0,0 +1,23 @@ +const isArray = parameter => { + const _isArray = parameter.constructor === Array; + // parameter instanceof Array; + // Array.isArray(parameter); + return _isArray; +}; + +const isObject = parameter => { + const _isObject = typeof parameter === "object" && parameter !== null; + + return _isObject; +}; + +function isJson(str) { + try { + const data = JSON.parse(str); + return { data, success: true }; + } catch (e) { + return { data: null, success: false }; + } +} + +export { isArray, isObject, isJson }; diff --git a/src/utils/identity.js b/src/utils/identity.js index c3a46ed..298f25e 100644 --- a/src/utils/identity.js +++ b/src/utils/identity.js @@ -1,6 +1,11 @@ import { request } from "./axios"; +import { setItem } from "./localStorage"; -export const authenticate = (username, password) => { +const storageKeys = { + TOKEN: "AUTHORIZATION_TOKEN" +}; + +const authenticate = async (username, password) => { const urlTemplate = process.env.REACT_APP_IDENTITY_AUTHENTICATION_URL; const url = urlTemplate .replace("{username}", username) @@ -9,5 +14,10 @@ export const authenticate = (username, password) => { method: "post" }; - return request(url, options); + const token = await request(url, options); + setItem(storageKeys.TOKEN, token); + + return token; }; + +export { storageKeys, authenticate }; diff --git a/src/utils/localStorage.js b/src/utils/localStorage.js new file mode 100644 index 0000000..24b5038 --- /dev/null +++ b/src/utils/localStorage.js @@ -0,0 +1,36 @@ +import { isArray, isObject, isJson } from "./dataType"; + +const setItem = (key, value) => { + let valueToStore = value; + if (isArray(value) || isObject(value)) { + valueToStore = JSON.stringify(value); + } + + window.localStorage.setItem(key, valueToStore); +}; + +const getItem = key => { + var value = window.localStorage.getItem(key); + var { data, success } = isJson(value); + + if (success) { + return data; + } else { + return value; + } +}; + +const removeItem = key => { + window.localStorage.removeItem(key); +}; + +const clear = () => { + window.localStorage.clear(); +}; + +const key = index => { + var keyName = window.localStorage.key(index); + return keyName; +}; + +export { setItem, getItem, removeItem, clear, key };