2021-08-04 13:04:05 +03:00
|
|
|
import React from 'react';
|
|
|
|
import { useSelector } from 'react-redux'
|
|
|
|
import { Route, Redirect } from 'react-router-dom';
|
|
|
|
import { selectAuth } from '../../redux/outstaffingSlice';
|
|
|
|
|
|
|
|
export const ProtectedRoute = ({ component: Component, ...rest }) => {
|
|
|
|
const isAuth = useSelector(selectAuth)
|
|
|
|
const existingToken = localStorage.getItem('auth_token')
|
|
|
|
const expiresAt = localStorage.getItem('access_token_expired_at')
|
|
|
|
|
2023-01-13 13:02:48 +03:00
|
|
|
const isTokenAlive = !isAuth && (existingToken && expiresAt && new Date(expiresAt).getTime() > (new Date()).getTime());
|
|
|
|
|
2021-08-04 13:04:05 +03:00
|
|
|
return (
|
|
|
|
<Route
|
|
|
|
{...rest}
|
|
|
|
render={props =>
|
2023-01-13 13:02:48 +03:00
|
|
|
// ( isAuth || isTokenAlive) ? (
|
2021-08-04 13:04:05 +03:00
|
|
|
<Component {...props} />
|
2023-01-13 13:02:48 +03:00
|
|
|
// ) : <Redirect to='/auth' />
|
2021-08-04 13:04:05 +03:00
|
|
|
}
|
|
|
|
/>
|
|
|
|
);
|
2023-01-13 13:02:48 +03:00
|
|
|
};
|