first commit
This commit is contained in:
86
src/components/Connection/index.js
Normal file
86
src/components/Connection/index.js
Normal file
@@ -0,0 +1,86 @@
|
||||
/* global document, window */
|
||||
|
||||
const axios = require('axios');
|
||||
|
||||
const Instance = () => {
|
||||
const apiUrl = process.env.NODE_ENV === 'production'
|
||||
? 'https://portal-api.bcbsinstitute.com'
|
||||
: 'https://portal-api.dev.bcbsinstitute.com';
|
||||
window.localStorage.setItem('App', '8a266a40-ed2e-4be2-bdfc-459a507bf02e');
|
||||
|
||||
let instance = axios.create({
|
||||
baseURL: apiUrl,
|
||||
timeout: 60000,
|
||||
headers: { App: window.localStorage.getItem('App') },
|
||||
});
|
||||
|
||||
const setCookie = (cname, cvalue, date) => {
|
||||
const d = new Date(date * 1000);
|
||||
const expires = `expires=${d.toUTCString()}`;
|
||||
document.cookie = `${cname}=${cvalue};${expires};path=/`;
|
||||
};
|
||||
|
||||
const getCookie = (cname) => {
|
||||
const name = `${cname}=`;
|
||||
const decodedCookie = decodeURIComponent(document.cookie);
|
||||
const ca = decodedCookie.split(';');
|
||||
for (let i = 0; i < ca.length; i += 1) {
|
||||
let c = ca[i];
|
||||
while (c.charAt(0) === ' ') {
|
||||
c = c.substring(1);
|
||||
}
|
||||
|
||||
if (c.indexOf(name) === 0) {
|
||||
return c.substring(name.length, c.length);
|
||||
}
|
||||
}
|
||||
|
||||
return '';
|
||||
};
|
||||
|
||||
const setToken = (token) => {
|
||||
if (token && token !== null) {
|
||||
instance = axios.create({
|
||||
baseURL: apiUrl,
|
||||
timeout: 60000,
|
||||
headers: { App: window.localStorage.getItem('App'), Token: `Bearer ${token}` },
|
||||
});
|
||||
} else {
|
||||
instance = axios.create({
|
||||
baseURL: apiUrl,
|
||||
timeout: 60000,
|
||||
headers: { App: window.localStorage.getItem('App') },
|
||||
});
|
||||
}
|
||||
|
||||
return instance;
|
||||
};
|
||||
|
||||
const getConnection = () => {
|
||||
const token = getCookie('token');
|
||||
return setToken(token);
|
||||
};
|
||||
|
||||
const getRawConn = () => {
|
||||
const token = getCookie('token');
|
||||
if (token && token !== null && token !== '') {
|
||||
return instance;
|
||||
}
|
||||
window.location.href = '/#/login';
|
||||
return null;
|
||||
};
|
||||
|
||||
|
||||
const token = getCookie('token');
|
||||
instance = setToken(token);
|
||||
|
||||
return {
|
||||
getCookie,
|
||||
setCookie,
|
||||
getConnection,
|
||||
setToken,
|
||||
getRawConn,
|
||||
};
|
||||
};
|
||||
|
||||
export default Instance();
|
||||
Reference in New Issue
Block a user