30 lines
530 B
JavaScript
30 lines
530 B
JavaScript
import React from 'react';
|
|
import { Route, Redirect } from 'react-router-dom';
|
|
import { getToken } from '../../utils/common';
|
|
|
|
const isLoggedIn = getToken();
|
|
|
|
const PrivateRoute = ({ component: Component, user, ...rest }) => {
|
|
return (
|
|
<Route
|
|
{...rest}
|
|
render={(props) =>
|
|
isLoggedIn ? (
|
|
<Component {...rest} {...props} />
|
|
) : (
|
|
<Redirect
|
|
to={{
|
|
pathname: '/login',
|
|
state: {
|
|
from: props.location,
|
|
},
|
|
}}
|
|
/>
|
|
)
|
|
}
|
|
/>
|
|
);
|
|
};
|
|
|
|
export default PrivateRoute;
|