set token on http requests
parent
9fa3091ab1
commit
30510d9a73
|
@ -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 (
|
||||
<>
|
||||
<div>NetworkContainer</div>
|
||||
|
@ -22,6 +31,9 @@ const NetworkContainer = () => {
|
|||
onChange={handleChange("test")}
|
||||
value={state.network.test}
|
||||
/>
|
||||
<Button variant="contained" color="primary" onClick={handleReadMachines}>
|
||||
Read machines
|
||||
</Button>
|
||||
</>
|
||||
);
|
||||
};
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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 };
|
|
@ -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 };
|
||||
|
|
|
@ -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 };
|
Loading…
Reference in New Issue