b26d6d6e69
* updated react-scripts (fix eslint-plugin problems) * reorder functions where needed
66 lines
1.3 KiB
TypeScript
66 lines
1.3 KiB
TypeScript
import "./App.css";
|
|
import React from "react";
|
|
import Main from "./components/Main";
|
|
import { Redirect, Route, RouteProps, Switch } from "react-router-dom";
|
|
import SignIn from "./components/SignIn";
|
|
import SignUp from "./components/SignUp";
|
|
|
|
export const isLoggedIn = (): boolean => !!localStorage.getItem("token");
|
|
|
|
function PrivateRoute({ children, ...rest }: RouteProps) {
|
|
return (
|
|
<Route
|
|
{...rest}
|
|
render={({ location }) =>
|
|
isLoggedIn() ? (
|
|
children
|
|
) : (
|
|
<Redirect
|
|
to={{
|
|
pathname: "/login",
|
|
state: { from: location },
|
|
}}
|
|
/>
|
|
)
|
|
}
|
|
/>
|
|
);
|
|
}
|
|
|
|
function NotLoggedInOnlyRoute({ children, ...rest }: RouteProps) {
|
|
return (
|
|
<Route
|
|
{...rest}
|
|
render={() =>
|
|
!isLoggedIn() ? (
|
|
children
|
|
) : (
|
|
<Redirect
|
|
to={{
|
|
pathname: "/",
|
|
}}
|
|
/>
|
|
)
|
|
}
|
|
/>
|
|
);
|
|
}
|
|
|
|
function App(): React.ReactElement {
|
|
return (
|
|
<Switch>
|
|
<PrivateRoute exact path={"/"}>
|
|
<Main />
|
|
</PrivateRoute>
|
|
<NotLoggedInOnlyRoute path={"/login"}>
|
|
<SignIn />
|
|
</NotLoggedInOnlyRoute>
|
|
<NotLoggedInOnlyRoute path={"/signup"}>
|
|
<SignUp />
|
|
</NotLoggedInOnlyRoute>
|
|
</Switch>
|
|
);
|
|
}
|
|
|
|
export default App;
|