32 lines
815 B
JavaScript
Raw Normal View History

2023-05-25 16:42:37 +03:00
import React from "react";
import { useSelector } from "react-redux";
2023-05-31 11:24:46 +03:00
import { Route } from "react-router-dom";
2023-05-25 16:42:37 +03:00
2023-05-30 10:10:34 +03:00
import { selectAuth } from "@redux/outstaffingSlice";
2021-08-04 13:04:05 +03:00
export const ProtectedRoute = ({ component: Component, ...rest }) => {
2023-05-25 16:42:37 +03:00
const isAuth = useSelector(selectAuth);
const existingToken = localStorage.getItem("auth_token");
const expiresAt = localStorage.getItem("access_token_expired_at");
2021-08-04 13:04:05 +03:00
2023-05-31 11:24:46 +03:00
// eslint-disable-next-line no-unused-vars
2023-05-25 16:42:37 +03:00
const isTokenAlive =
!isAuth &&
existingToken &&
expiresAt &&
new Date(expiresAt).getTime() > new Date().getTime();
2023-05-25 16:42:37 +03:00
return (
<Route
{...rest}
render={
(props) => (
// ( isAuth || isTokenAlive) ? (
<Component {...props} />
)
// ) : <Redirect to='/auth' />
}
/>
);
};