added server auth and protected routes

This commit is contained in:
kurpfish
2021-08-04 13:04:05 +03:00
parent f42f7a9f71
commit e4ec5fd091
6 changed files with 181 additions and 113 deletions

View File

@ -0,0 +1,23 @@
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')
const isTokenAlive = (existingToken && expiresAt && new Date(expiresAt).getTime() > (new Date()).getTime());
return (
<Route
{...rest}
render={props =>
( isAuth || isTokenAlive) ? (
<Component {...props} />
) : <Redirect to='/auth' />
}
/>
);
}