Files
FBSAdminTool/src/containers/TheContent.js

186 lines
6.0 KiB
JavaScript
Raw Normal View History

2021-07-30 13:23:23 +05:30
import React, { Suspense, useState, useEffect } from 'react'
import axios from 'axios';
2021-07-29 13:52:29 +05:30
//import Cookies from 'js-cookie';
import Cookies from 'universal-cookie';
2021-07-12 09:41:26 +05:30
import {
2021-08-17 20:28:03 +05:30
Redirect,
Route,
2021-08-23 17:07:42 +05:30
useLocation,
2021-08-17 20:28:03 +05:30
Switch
2021-07-12 09:41:26 +05:30
} from 'react-router-dom'
import { CContainer, CFade } from '@coreui/react'
// routes config
import routes from '../routes'
2021-08-18 10:12:26 +05:30
import Sso from 'src/views/sso';
2021-07-28 17:28:25 +05:30
2021-08-17 20:28:03 +05:30
const loading = ( <
div className = "pt-3 text-center" >
<div className = "sk-spinner sk-spinner-pulse" > </div>
</div>
2021-07-12 09:41:26 +05:30
)
const TheContent = () => {
2021-07-28 17:28:25 +05:30
2021-08-17 20:28:03 +05:30
const [SsoSession, setSsoSession] = useState('');
const [UserData, setUserData] = useState('');
2021-08-23 18:49:44 +05:30
const [UserId, setUserId] = useState('');
2021-08-17 20:28:03 +05:30
async function fetchSession() {
2021-09-13 18:34:39 +05:30
//setSsoSession('{ "expiration": 468, "client_address": "111.223.144.163", "protocol": "urn:oasis:names:tc:SAML:2.0:protocol", "identity_provider": "https://sso.ts.bizside.biz/idp/shibboleth", "authn_instant": "2021-09-03T08:26:41.248Z", "authncontext_class": "urn:oasis:names:tc:SAML:2.0:ac:classes:PasswordProtectedTransport", "attributes": [ { "name": "mail", "values": [ "akira.miyata@mobilous.com" ] } ] }')
2021-11-09 14:45:35 +05:30
const result = await axios('https://fbssso.ts.bizside.biz/Shibboleth.sso/Session');
2021-09-13 18:34:39 +05:30
if(JSON.stringify(SsoSession) !== JSON.stringify(result.data)) {
2022-02-18 14:35:59 +05:30
console.log(result.data);
2021-09-13 18:34:39 +05:30
setSsoSession(result.data);
}
2021-08-17 20:28:03 +05:30
}
2021-08-24 11:49:38 +05:30
useEffect(() => {
fetchSession()
}, []);
2021-08-23 13:28:23 +05:30
async function postUserData(data) {
let axiosConfig = {
headers: {
'Content-Type': 'application/json;charset=UTF-8',
"Access-Control-Allow-Origin": "*",
}
};
2021-11-09 14:56:16 +05:30
const result = await axios.post('https://fbssso.ts.bizside.biz/csv', data, axiosConfig)
2021-08-23 13:28:23 +05:30
.catch((err) => {
console.log("AXIOS ERROR: ", err);
});
2021-08-23 15:14:10 +05:30
console.log("user data posted");
2021-08-23 13:28:23 +05:30
}
2021-08-17 20:28:03 +05:30
async function fetchUser() {
2022-02-18 14:45:07 +05:30
if(SsoSession.attributes == null || SsoSession.attributes[0] === undefined) {
2021-09-03 13:31:24 +05:30
return false;
}
const user_email = SsoSession.attributes[0].values[0]
//console.log(user_email);
const company_code = "FBS";
const key = "api"
const pwd = "c558a56c63c44f65956adde8863ecc3558f3e55a465d4338bb2e7d2692866fd8";
2021-09-03 13:25:52 +05:30
2021-11-09 14:56:16 +05:30
const result = await axios.get('https://fbssso.ts.bizside.biz/users?email=' + user_email, {
2021-09-03 13:31:24 +05:30
auth: {
username: key,
password: pwd
}
}).catch((err) => {
console.log(err);
return false;
});
//console.log(result.data);
if(JSON.stringify(UserData) !== JSON.stringify(result.data[0])) {
setUserData(result.data[0]);
}
return true;
2021-07-30 16:56:49 +05:30
}
2021-08-17 20:28:03 +05:30
2021-08-23 18:07:50 +05:30
function isCSVPath(){
const url =window.location.href;
if(url.includes('dashboard')) {
return true;
}
else {
return false;
}
2021-08-23 17:30:57 +05:30
}
2021-08-17 20:28:03 +05:30
function get_token_from_storage_or_cookie() {
2021-09-07 20:00:28 +05:30
//return true;
2021-09-13 18:34:39 +05:30
const cookies = new Cookies();
2021-11-09 16:24:05 +05:30
const shib = cookies.get('_shibsession_64656661756c7468747470733a2f2f66627373736f2e74732e62697a736964652e62697a2f73686962626f6c657468')
2021-09-13 18:34:39 +05:30
if (shib !== undefined) {
fetchSession().then(() => {
if (SsoSession !== null) {
console.log(shib);
fetchUser().then(() => {
if(UserId === ''){
let id = Math.floor(100000 + Math.random() * 900000)
setUserId(id);
}
const _firstname = UserData.firstname;
const _lastname = UserData.lastname;
const _email = UserData.email;
const _empcode = UserData.employee_code;
const json_str = `[
{
"mailadress": "${_email}",
"name": "${_lastname} ${_firstname}",
"jsessionid": ${UserId},
"_idp_session": "",
2021-11-09 16:24:05 +05:30
"_opensaml_key": "_shibsession_64656661756c7468747470733a2f2f66627373736f2e74732e62697a736964652e62697a2f73686962626f6c657468",
2021-09-13 18:34:39 +05:30
"_opensaml_value": "${shib}"
}
]`;
//const json_str = '[{"jsessonid": "' + UserId + '" , "name": "' + _firstname + ' ' +_lastname+ '", "mailadress":" ' + _email + '", "empcode": "' + _empcode + '"}]';
if(_empcode !== null && _empcode !== undefined){
if(isCSVPath()){
postUserData(json_str);
}
}
return true;
});
}
});
} else {
return null;
}
2021-08-19 15:35:42 +05:30
2021-07-30 13:23:23 +05:30
}
2021-08-17 20:28:03 +05:30
return (
<main className = "c-main">
<CContainer fluid >
<Suspense fallback = { loading }>
<Switch > {
routes.map((route, idx) => {
return route.component && (
<Route key = { idx }
path = { route.path }
exact = { route.exact }
name = { route.name }
2022-02-18 14:35:59 +05:30
// render = {
// props =>
// ( <route.component {...props} userid={1} email={"akira.miyata@mobilous.com"}/>
// )
// }
2021-08-17 20:28:03 +05:30
render = {
props =>
get_token_from_storage_or_cookie() !== null ?
2021-09-13 18:34:39 +05:30
( <route.component {...props} userid={UserId} email={UserData.email}/>
2021-08-17 20:28:03 +05:30
) : ( <Redirect to = {
{ pathname: "/sso" }
}
/>
)
}
/>
)
})
}
<Redirect
from = "/"
to = "/dashboard" / >
</Switch> </Suspense >
</CContainer> </main >
)
2021-07-12 09:41:26 +05:30
}
export default React.memo(TheContent)