From 8e67a91dc15ed45c2ddc0648d02472681a71fdbd Mon Sep 17 00:00:00 2001 From: cami Date: Wed, 23 Jun 2021 00:15:43 +0200 Subject: [PATCH] Added login functionality and a redirect if not logged in --- frontend/src/App.js | 6 +++++- frontend/src/auth/PrivateRoute.js | 18 ++++++++++++++++++ frontend/src/components/Navbar.js | 2 +- frontend/src/components/pages/Login.js | 8 +++++--- frontend/src/components/pages/Secret.js | 25 +++++++++++++++++++++++++ 5 files changed, 54 insertions(+), 5 deletions(-) create mode 100644 frontend/src/auth/PrivateRoute.js create mode 100644 frontend/src/components/pages/Secret.js diff --git a/frontend/src/App.js b/frontend/src/App.js index 0c92cb2..f9bbaf7 100644 --- a/frontend/src/App.js +++ b/frontend/src/App.js @@ -8,8 +8,10 @@ import Ueber from "./components/pages/Ueber"; import Lizenzen from "./components/pages/lizenzen"; import Privacy from "./components/pages/Privacy"; import Login from "./components/pages/Login"; -import Register from "./components/pages/Register" +import Register from "./components/pages/Register"; import Manual from "./components/pages/Manual"; +import Secret from "./components/pages/Secret"; +import PrivateRoute from "./auth/PrivateRoute"; function App() { return ( @@ -25,6 +27,8 @@ function App() { + + diff --git a/frontend/src/auth/PrivateRoute.js b/frontend/src/auth/PrivateRoute.js new file mode 100644 index 0000000..fda4abd --- /dev/null +++ b/frontend/src/auth/PrivateRoute.js @@ -0,0 +1,18 @@ +import { Component } from "react"; +import { Redirect, Route } from "react-router-dom"; +import { useAuth } from "./AuthProvider"; + +const PrivateRoute = ({ component: Component, ...rest }) => { + const [logged] = useAuth(); + + return ( + + logged ? : + } + /> + ); +}; + +export default PrivateRoute \ No newline at end of file diff --git a/frontend/src/components/Navbar.js b/frontend/src/components/Navbar.js index 8e703c3..3f47866 100644 --- a/frontend/src/components/Navbar.js +++ b/frontend/src/components/Navbar.js @@ -66,7 +66,7 @@ function Navbar() {
  • diff --git a/frontend/src/components/pages/Login.js b/frontend/src/components/pages/Login.js index 5f85009..a71e73c 100644 --- a/frontend/src/components/pages/Login.js +++ b/frontend/src/components/pages/Login.js @@ -4,7 +4,8 @@ import "../../App.css"; import Footer from "../../Footer"; import InputField from "../InputField"; import SubmitField from "../SubmitField"; -import { login, useAuth, logout } from "../../auth/AuthProvider"; +import { login, authFetch, useAuth, logout } from "../../auth/AuthProvider"; +import Secret from "./Secret"; export default function Login() { const [username, setUsername] = useState(""); @@ -12,7 +13,6 @@ export default function Login() { const onSubmitClick = (e) => { e.preventDefault(); - console.log("You pressed login"); let opts = { username: username, password: password, @@ -26,7 +26,6 @@ export default function Login() { .then((token) => { if (token.access_token) { login(token); - console.log(token); } else { console.log("Please type in the correct username / password"); } @@ -65,7 +64,10 @@ export default function Login() { ) : ( + <> + + )}