aboutsummaryrefslogtreecommitdiff
path: root/src/pages/PrivateRoute.tsx
diff options
context:
space:
mode:
authoreug-vs <eug-vs@keemail.me>2020-08-07 21:53:48 +0300
committereug-vs <eug-vs@keemail.me>2020-08-07 21:53:48 +0300
commitd9c74d25ab1f4e13cb980074e188ef6125d2d290 (patch)
tree83058ec297a5a25f3d5e06a8b6e8c6097a2f0388 /src/pages/PrivateRoute.tsx
parent5a597796f622407be7ba03f5eb7aeb608c418fc2 (diff)
downloadwhich-ui-d9c74d25ab1f4e13cb980074e188ef6125d2d290.tar.gz
feat: setup initial routing
Diffstat (limited to 'src/pages/PrivateRoute.tsx')
-rw-r--r--src/pages/PrivateRoute.tsx36
1 files changed, 36 insertions, 0 deletions
diff --git a/src/pages/PrivateRoute.tsx b/src/pages/PrivateRoute.tsx
new file mode 100644
index 0000000..cdee67b
--- /dev/null
+++ b/src/pages/PrivateRoute.tsx
@@ -0,0 +1,36 @@
+import React from 'react';
+import { Redirect, Route } from 'react-router-dom';
+import { useAuth } from '../hooks/useAuth';
+
+const urls = {
+ home: '/',
+ login: '/login',
+ registration: '/registration',
+ profile: '/profile',
+ feed: '/feed',
+ notifications: '/notifications'
+};
+
+const PrivateRoute: React.FC<any> = ({ component: ProtectedComponent, ...rest }) => {
+ const { isAuthenticated } = useAuth();
+
+ const getComponent: React.FC<any> = (props) => {
+ if (props.match.path === urls.login || props.match.path === urls.registration) {
+ return isAuthenticated() ? (
+ <Redirect to={urls.profile} />
+ ) : (
+ <ProtectedComponent {...props} />
+ );
+ }
+
+ return isAuthenticated() ? (
+ <ProtectedComponent {...props} />
+ ) : (
+ <Redirect to={{ pathname: urls.login, state: { from: props.location } }} />
+ );
+ }
+
+ return <Route {...rest} render={getComponent} />;
+};
+
+export default PrivateRoute;