Files
FBSAdminTool/src/containers/TheContent.js

150 lines
4.8 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-18 10:12:26 +05:30
const sdata = { "expiration": 479, "client_address": "123.231.121.140", "protocol": "urn:oasis:names:tc:SAML:2.0:protocol", "identity_provider": "https://sso.ts.bizside.biz/idp/shibboleth", "authn_instant": "2021-08-16T11:29:41.254Z", "authncontext_class": "urn:oasis:names:tc:SAML:2.0:ac:classes:PasswordProtectedTransport", "attributes": [ { "name": "mail", "values": [ "akira.miyata@mobilous.com" ] } ] }
useEffect(() => {
setSsoSession(sdata);
}, []);
2021-08-17 20:28:03 +05:30
const [SsoSession, setSsoSession] = useState('');
const [UserData, setUserData] = useState('');
async function fetchSession() {
// You can await here
const result = await axios('https://fsbsso.sumasen.net/Shibboleth.sso/Session');
2021-08-19 15:35:42 +05:30
if(JSON.stringify(SsoSession) !== JSON.stringify(result.data)) {
setSsoSession(result.data);
}
2021-08-17 20:28:03 +05:30
}
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-08-23 15:36:37 +05:30
const result = await axios.post('https://fsbsso.sumasen.net/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() {
2021-08-23 15:02:36 +05:30
//console.log(SsoSession)
2021-08-17 20:58:17 +05:30
const user_email = SsoSession.attributes[0].values[0]
2021-08-23 15:02:36 +05:30
//console.log(user_email);
2021-08-17 20:28:03 +05:30
const company_code = "FBS";
const key = "api"
const pwd = "c558a56c63c44f65956adde8863ecc3558f3e55a465d4338bb2e7d2692866fd8";
2021-08-18 14:25:00 +05:30
const result = await axios.get('https://fsbsso.sumasen.net/users?email=akira.miyata@mobilous.com', {
2021-08-17 20:28:03 +05:30
auth: {
username: key,
password: pwd
2021-07-30 17:44:53 +05:30
}
2021-08-19 15:35:42 +05:30
}).catch((err) => {
console.log(err);
return false;
2021-07-30 17:44:53 +05:30
});
2021-08-23 15:02:36 +05:30
//console.log(result.data);
2021-08-19 15:57:21 +05:30
if(JSON.stringify(UserData) !== JSON.stringify(result.data[0])) {
2021-08-19 15:35:42 +05:30
setUserData(result.data[0]);
2021-08-18 15:50:52 +05:30
}
2021-08-19 15:35:42 +05:30
return true;
2021-07-30 16:56:49 +05:30
}
2021-08-17 20:28:03 +05:30
2021-08-23 18:02:13 +05:30
function userPathname(){
2021-08-23 17:30:57 +05:30
const location = useLocation();
return location.pathname;
}
2021-08-17 20:28:03 +05:30
function get_token_from_storage_or_cookie() {
2021-08-19 15:35:42 +05:30
const cookies = new Cookies();
const shib = cookies.get('_shibsession_64656661756c7468747470733a2f2f66736273736f2e73756d6173656e2e6e65742f73686962626f6c657468')
if (shib !== undefined) {
fetchSession().then(() => {
if (SsoSession !== null) {
fetchUser().then(() => {
2021-08-23 13:28:23 +05:30
let id = Math.floor(100000 + Math.random() * 900000)
2021-08-19 15:35:42 +05:30
const _firstname = UserData.firstname;
2021-08-19 16:11:09 +05:30
const _lastname = UserData.lastname;
const _email = UserData.email;
const _empcode = UserData.employee_code;
2021-08-23 14:59:11 +05:30
const json_str = '[{"firstname": "' + _firstname + '", "lastname":"' +_lastname+ '", "email":" ' + _email + '", "empcode": "' + _empcode + '"}]';
2021-08-23 17:07:42 +05:30
if(_empcode !== null && _empcode !== undefined){
2021-08-23 18:02:13 +05:30
console.log(userPathname());
2021-08-23 17:55:15 +05:30
2021-08-23 17:07:42 +05:30
postUserData(json_str);
}
2021-08-19 15:35:42 +05:30
return true;
});
}
});
} else {
return null;
}
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 }
render = {
props =>
get_token_from_storage_or_cookie() !== null ?
( <route.component {...props }/>
) : ( <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)