kandimat/redaktions-app/src/App.tsx
Christoph Lienhard b26d6d6e69
Fix "used before it was defined" warning
* updated react-scripts (fix eslint-plugin problems)
* reorder functions where needed
2021-02-07 23:06:58 +01:00

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;