2023-03-03 08:48:10 +02:00
|
|
|
import React from "react";
|
2023-03-03 23:56:31 +02:00
|
|
|
import PropTypes from "prop-types";
|
2023-03-04 00:37:52 +02:00
|
|
|
import AppLayout from "./layout/AppLayout";
|
2023-03-03 08:48:10 +02:00
|
|
|
import { BrowserRouter, Switch, Redirect, Route } from "react-router-dom";
|
|
|
|
import { useTuitioToken } from "@flare/tuitio-client-react";
|
|
|
|
import LoginContainer from "../features/login/components/LoginContainer";
|
2020-12-16 03:09:24 +02:00
|
|
|
|
2023-03-03 08:48:10 +02:00
|
|
|
const PrivateRoute = ({ component, ...rest }) => {
|
|
|
|
const { valid } = useTuitioToken();
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Route
|
|
|
|
{...rest}
|
|
|
|
render={props =>
|
|
|
|
valid ? (
|
|
|
|
React.createElement(component, props)
|
|
|
|
) : (
|
|
|
|
<Redirect
|
|
|
|
to={{
|
|
|
|
pathname: "/login",
|
|
|
|
state: {
|
|
|
|
from: props.location
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
/>
|
2022-11-22 19:39:58 +02:00
|
|
|
);
|
2023-03-03 08:48:10 +02:00
|
|
|
};
|
|
|
|
|
2023-03-03 23:56:31 +02:00
|
|
|
PrivateRoute.propTypes = {
|
2023-03-04 00:01:24 +02:00
|
|
|
component: PropTypes.func.isRequired,
|
|
|
|
location: PropTypes.object
|
2023-03-03 23:56:31 +02:00
|
|
|
};
|
|
|
|
|
2023-03-03 08:48:10 +02:00
|
|
|
const PublicRoute = ({ component, ...rest }) => {
|
|
|
|
const { valid } = useTuitioToken();
|
|
|
|
return (
|
|
|
|
<Route
|
|
|
|
{...rest}
|
|
|
|
render={props =>
|
|
|
|
valid ? (
|
|
|
|
<Redirect
|
|
|
|
to={{
|
|
|
|
pathname: "/"
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
React.createElement(component, props)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
};
|
2020-12-19 02:08:36 +02:00
|
|
|
|
2023-03-03 23:56:31 +02:00
|
|
|
PublicRoute.propTypes = {
|
2023-03-04 00:01:24 +02:00
|
|
|
component: PropTypes.func.isRequired
|
2023-03-03 23:56:31 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const App = () => {
|
2020-12-16 03:09:24 +02:00
|
|
|
return (
|
2023-03-03 08:48:10 +02:00
|
|
|
<BrowserRouter basename={process.env.PUBLIC_URL || ""}>
|
|
|
|
<Switch>
|
2023-03-04 22:11:39 +02:00
|
|
|
<Route exact path="/" render={() => <Redirect to="/dashboard" />} />
|
2023-03-03 08:48:10 +02:00
|
|
|
<PublicRoute path="/login" component={LoginContainer} />
|
2023-03-04 00:37:52 +02:00
|
|
|
<PrivateRoute path="/" component={AppLayout} />
|
2023-03-03 08:48:10 +02:00
|
|
|
</Switch>
|
|
|
|
</BrowserRouter>
|
2020-12-16 03:09:24 +02:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2023-03-03 23:56:31 +02:00
|
|
|
export default App;
|