From b1241d89272d2787d822f474f0b9d7e49e74172e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Malak?= Date: Mon, 18 Oct 2021 16:12:06 +0200 Subject: [PATCH] Added AuthContext. Protected routes and functionality based on isAuthenticated property --- client/src/App.tsx | 27 ++++++---- client/src/components/Navigation/Navbar.tsx | 27 +++++++--- client/src/components/Navigation/routes.json | 9 ++-- .../components/Snippets/SnippetDetails.tsx | 53 ++++++++++--------- client/src/components/Snippets/SnippetPin.tsx | 21 +++++--- client/src/store/AuthContext.tsx | 29 ++++++++++ client/src/store/SnippetsContext.tsx | 9 ++-- client/src/store/index.ts | 1 + client/src/typescript/interfaces/Context.ts | 7 ++- client/src/typescript/interfaces/Route.ts | 1 + client/src/utils/ProtectedRoute.tsx | 15 ++++++ client/src/utils/index.ts | 1 + 12 files changed, 140 insertions(+), 60 deletions(-) create mode 100644 client/src/store/AuthContext.tsx create mode 100644 client/src/utils/ProtectedRoute.tsx diff --git a/client/src/App.tsx b/client/src/App.tsx index 73dd8e2..5fab494 100644 --- a/client/src/App.tsx +++ b/client/src/App.tsx @@ -1,20 +1,25 @@ import { BrowserRouter, Switch, Route } from 'react-router-dom'; import { Navbar } from './components/Navigation/Navbar'; -import { Editor, Home, Snippet, Snippets } from './containers'; -import { SnippetsContextProvider } from './store'; +import { Editor, Home, Snippet, Snippets, Auth } from './containers'; +import { AuthContextProvider, SnippetsContextProvider } from './store'; +import { ProtectedRoute } from './utils'; export const App = () => { return ( - - - - - - - - - + + + + + + + + + + + + + ); }; diff --git a/client/src/components/Navigation/Navbar.tsx b/client/src/components/Navigation/Navbar.tsx index 2ef7d92..461c6f9 100644 --- a/client/src/components/Navigation/Navbar.tsx +++ b/client/src/components/Navigation/Navbar.tsx @@ -1,21 +1,34 @@ import { NavLink } from 'react-router-dom'; import { Route } from '../../typescript/interfaces'; import { routes as clientRoutes } from './routes.json'; +import { useContext } from 'react'; +import { AuthContext } from '../../store'; export const Navbar = (): JSX.Element => { const routes = clientRoutes as Route[]; + const { isAuthenticated } = useContext(AuthContext); return ( diff --git a/client/src/components/Navigation/routes.json b/client/src/components/Navigation/routes.json index b2db906..b07cf9d 100644 --- a/client/src/components/Navigation/routes.json +++ b/client/src/components/Navigation/routes.json @@ -2,15 +2,18 @@ "routes": [ { "name": "Home", - "dest": "/" + "dest": "/", + "isPublic": true }, { "name": "Snippets", - "dest": "/snippets" + "dest": "/snippets", + "isPublic": true }, { "name": "Editor", - "dest": "/editor" + "dest": "/editor", + "isPublic": false } ] } diff --git a/client/src/components/Snippets/SnippetDetails.tsx b/client/src/components/Snippets/SnippetDetails.tsx index 016ec94..b7c46cc 100644 --- a/client/src/components/Snippets/SnippetDetails.tsx +++ b/client/src/components/Snippets/SnippetDetails.tsx @@ -1,6 +1,6 @@ -import { useContext } from 'react'; +import { Fragment, useContext } from 'react'; import { useHistory } from 'react-router-dom'; -import { SnippetsContext } from '../../store'; +import { SnippetsContext, AuthContext } from '../../store'; import { Snippet } from '../../typescript/interfaces'; import { dateParser } from '../../utils'; import { Badge, Button, Card } from '../UI'; @@ -27,14 +27,11 @@ export const SnippetDetails = (props: Props): JSX.Element => { const history = useHistory(); const { deleteSnippet, setSnippet } = useContext(SnippetsContext); + const { isAuthenticated } = useContext(AuthContext); const creationDate = dateParser(createdAt); const updateDate = dateParser(updatedAt); - // const copyHandler = () => { - // copy(code); - // }; - return (
@@ -74,27 +71,31 @@ export const SnippetDetails = (props: Props): JSX.Element => { {/* ACTIONS */}
-