|
|
@@ -3,12 +3,15 @@ import axios from "axios"; |
|
|
|
import {isUserLoggedIn} from "../utils/Utils"; |
|
|
|
import jwtApplicationConfig from "auth/jwtApplicationConfig"; |
|
|
|
import jwt_decode from "jwt-decode"; |
|
|
|
import {apiPath} from "./utils"; |
|
|
|
|
|
|
|
//import axios from "axios"; |
|
|
|
export const refreshIntervalName = 'refreshInterval' |
|
|
|
export const predictProductionQty = 'predictProductionQty' |
|
|
|
export const predictUsageCount = 'predictUsageCount' |
|
|
|
export const windowCount = 'windowCount' |
|
|
|
import {useNavigate} from "react-router-dom"; |
|
|
|
import {useDispatch} from "react-redux"; |
|
|
|
|
|
|
|
// ** Handle User Login |
|
|
|
export const handleLogin = data => { |
|
|
@@ -37,16 +40,38 @@ export const handleLogin = data => { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
export const isLocalTokenValid = () => { |
|
|
|
axios.get(`${apiPath}/test`) |
|
|
|
.then((response) => { |
|
|
|
if(response.status === 200){ |
|
|
|
return true; |
|
|
|
} |
|
|
|
else{ |
|
|
|
return false; |
|
|
|
} |
|
|
|
}) |
|
|
|
.catch(error => { |
|
|
|
console.log(error); |
|
|
|
return false; |
|
|
|
}); |
|
|
|
return false; |
|
|
|
} |
|
|
|
|
|
|
|
export const isTokenValid = () =>{ |
|
|
|
let isExpired = false; |
|
|
|
const token = localStorage.getItem('accessToken'); |
|
|
|
let decodedToken=jwt_decode(token); |
|
|
|
let dateNow = new Date(); |
|
|
|
|
|
|
|
if(decodedToken.exp < dateNow.getTime()) |
|
|
|
isExpired = true; |
|
|
|
return isExpired; |
|
|
|
} |
|
|
|
if(localStorage.getItem('accessToken') !== null && localStorage.getItem('accessToken') !== 'null'){ |
|
|
|
let isExpired = false; |
|
|
|
const token = localStorage.getItem('accessToken'); |
|
|
|
let decodedToken=jwt_decode(token); |
|
|
|
let dateNow = new Date(); |
|
|
|
|
|
|
|
if(decodedToken.exp < dateNow.getTime()) |
|
|
|
isExpired = true; |
|
|
|
return isExpired; |
|
|
|
} |
|
|
|
else{ |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// ** Handle User Logout |
|
|
|
export const handleLogoutFunction = () => { |
|
|
@@ -73,7 +98,11 @@ export const handleLogoutFunction = () => { |
|
|
|
} |
|
|
|
|
|
|
|
// ** Handle axios token |
|
|
|
export const setupAxiosInterceptors = () => { |
|
|
|
export const SetupAxiosInterceptors = () => { |
|
|
|
const navigate = useNavigate() |
|
|
|
const dispatch = useDispatch(); |
|
|
|
//const updateLastRequestTime = useContext(TimerContext); |
|
|
|
|
|
|
|
axios.interceptors.request.use( |
|
|
|
config => { |
|
|
|
// ** Get token from localStorage |
|
|
@@ -89,4 +118,32 @@ export const setupAxiosInterceptors = () => { |
|
|
|
}, |
|
|
|
error => Promise.reject(error) |
|
|
|
) |
|
|
|
|
|
|
|
axios.interceptors.response.use( |
|
|
|
response => { |
|
|
|
//updateLastRequestTime(Date.now()); |
|
|
|
return response; |
|
|
|
}, |
|
|
|
error => { |
|
|
|
// ** const { config, response: { status } } = error |
|
|
|
const {response} = error |
|
|
|
|
|
|
|
if (error.response.status === 401) { |
|
|
|
dispatch(handleLogoutFunction()); |
|
|
|
navigate('/login'); |
|
|
|
} |
|
|
|
|
|
|
|
// ** if (status === 401) { |
|
|
|
if (response.status === 401) { |
|
|
|
dispatch(handleLogoutFunction()); |
|
|
|
navigate('/login'); |
|
|
|
} |
|
|
|
|
|
|
|
if (response && response.status === 401) { |
|
|
|
dispatch(handleLogoutFunction()); |
|
|
|
navigate('/login'); |
|
|
|
} |
|
|
|
return Promise.reject(error) |
|
|
|
} |
|
|
|
) |
|
|
|
} |