set token on http requests
parent
9fa3091ab1
commit
30510d9a73
|
@ -1,9 +1,10 @@
|
||||||
import React, { useContext } from "react";
|
import React, { useContext } from "react";
|
||||||
import { TextField } from "@material-ui/core";
|
import { TextField, Button } from "@material-ui/core";
|
||||||
import {
|
import {
|
||||||
ApplicationStateContext,
|
ApplicationStateContext,
|
||||||
ApplicationDispatchContext
|
ApplicationDispatchContext
|
||||||
} from "../../../state/ApplicationContexts";
|
} from "../../../state/ApplicationContexts";
|
||||||
|
import { get } from "../../../utils/axios";
|
||||||
|
|
||||||
const NetworkContainer = () => {
|
const NetworkContainer = () => {
|
||||||
const state = useContext(ApplicationStateContext);
|
const state = useContext(ApplicationStateContext);
|
||||||
|
@ -13,6 +14,14 @@ const NetworkContainer = () => {
|
||||||
dispatchActions.onNetworkChange(prop, event.target.value);
|
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 (
|
return (
|
||||||
<>
|
<>
|
||||||
<div>NetworkContainer</div>
|
<div>NetworkContainer</div>
|
||||||
|
@ -22,6 +31,9 @@ const NetworkContainer = () => {
|
||||||
onChange={handleChange("test")}
|
onChange={handleChange("test")}
|
||||||
value={state.network.test}
|
value={state.network.test}
|
||||||
/>
|
/>
|
||||||
|
<Button variant="contained" color="primary" onClick={handleReadMachines}>
|
||||||
|
Read machines
|
||||||
|
</Button>
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,10 +1,10 @@
|
||||||
import axios from "axios";
|
import axios from "axios";
|
||||||
import i18next from "i18next";
|
import i18next from "i18next";
|
||||||
import { useAuthorizationToken } from "../hooks/useAuthorizationToken";
|
import { getItem } from "./localStorage";
|
||||||
|
import { storageKeys } from "./identity";
|
||||||
|
|
||||||
function getHeaders() {
|
function getHeaders() {
|
||||||
// eslint-disable-next-line react-hooks/rules-of-hooks
|
const token = getItem(storageKeys.TOKEN);
|
||||||
const token = useAuthorizationToken();
|
|
||||||
const language = i18next.language;
|
const language = i18next.language;
|
||||||
|
|
||||||
return {
|
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 { 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 urlTemplate = process.env.REACT_APP_IDENTITY_AUTHENTICATION_URL;
|
||||||
const url = urlTemplate
|
const url = urlTemplate
|
||||||
.replace("{username}", username)
|
.replace("{username}", username)
|
||||||
|
@ -9,5 +14,10 @@ export const authenticate = (username, password) => {
|
||||||
method: "post"
|
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