From f6e2b445d7d21b209714f29214787c9b62b7022a Mon Sep 17 00:00:00 2001 From: Jagraj Aulakh Date: Sun, 19 Mar 2023 16:34:44 -0400 Subject: [PATCH 1/2] #21 Using wouter for navigation and links. Register page works by sending request to register endpoint --- frontend/src/App.js | 28 +++- .../src/components/AuthenticatedRoute.jsx | 20 +++ frontend/src/components/MyNavbar.jsx | 38 +++-- frontend/src/index.js | 19 ++- frontend/src/pages/HomePage.jsx | 10 +- frontend/src/pages/LoginPage.jsx | 20 +-- frontend/src/pages/RegisterPage.jsx | 146 ++++++++++++++++++ 7 files changed, 235 insertions(+), 46 deletions(-) create mode 100644 frontend/src/components/AuthenticatedRoute.jsx create mode 100644 frontend/src/pages/RegisterPage.jsx diff --git a/frontend/src/App.js b/frontend/src/App.js index 4b2861f..f2771cb 100644 --- a/frontend/src/App.js +++ b/frontend/src/App.js @@ -2,17 +2,33 @@ import "./App.css"; import { Route } from "wouter"; import HomePage from "./pages/HomePage"; import LoginPage from "./pages/LoginPage"; -import { UserContextProvider } from "./contexts/UserContext"; import LogoutPage from "./pages/LogoutPage"; +import RegisterPage from "./pages/RegisterPage"; +import AuthenticatedRoute from "./components/AuthenticatedRoute"; function App() { return (
- - - - - + + + + + + + + + + + + + + + + + + + +
); } diff --git a/frontend/src/components/AuthenticatedRoute.jsx b/frontend/src/components/AuthenticatedRoute.jsx new file mode 100644 index 0000000..9d7b5bc --- /dev/null +++ b/frontend/src/components/AuthenticatedRoute.jsx @@ -0,0 +1,20 @@ +import { useContext, useEffect } from "react"; +import { useLocation } from "wouter"; +import UserContext from "../contexts/UserContext"; + +const AuthenticatedRoute = ({ children, isAuthenticated }) => { + const { currentUser } = useContext(UserContext); + const [location, setLocation] = useLocation(); + + useEffect(() => { + if (isAuthenticated && !currentUser?.id) { + setLocation("/login"); + } else if (!isAuthenticated && currentUser?.id) { + setLocation("/"); + } + }, [currentUser]); + + return children; +}; + +export default AuthenticatedRoute; diff --git a/frontend/src/components/MyNavbar.jsx b/frontend/src/components/MyNavbar.jsx index 1cd46ff..29df7fa 100644 --- a/frontend/src/components/MyNavbar.jsx +++ b/frontend/src/components/MyNavbar.jsx @@ -1,24 +1,32 @@ -import { useContext } from "react"; +import React, { useContext } from "react"; import { Nav, Container, Navbar } from "react-bootstrap"; +import { Link } from "wouter"; import UserContext from "../contexts/UserContext"; const MyNavbar = () => { const { currentUser } = useContext(UserContext); + + const MyLink = ({ children, ...rest }) => { + return {children}; + }; + return ( - - - LearningTree - - - - - - + + + + LearningTree + + + + + + + ); }; diff --git a/frontend/src/index.js b/frontend/src/index.js index 06f12b9..61fef7d 100644 --- a/frontend/src/index.js +++ b/frontend/src/index.js @@ -1,14 +1,17 @@ -import React from 'react'; -import ReactDOM from 'react-dom/client'; -import './index.css'; -import App from './App'; -import reportWebVitals from './reportWebVitals'; -import 'bootstrap/dist/css/bootstrap.min.css'; +import React from "react"; +import ReactDOM from "react-dom/client"; +import "./index.css"; +import App from "./App"; +import reportWebVitals from "./reportWebVitals"; +import "bootstrap/dist/css/bootstrap.min.css"; +import { UserContextProvider } from "./contexts/UserContext"; -const root = ReactDOM.createRoot(document.getElementById('root')); +const root = ReactDOM.createRoot(document.getElementById("root")); root.render( - + + + ); diff --git a/frontend/src/pages/HomePage.jsx b/frontend/src/pages/HomePage.jsx index ebc5b5d..46b1559 100644 --- a/frontend/src/pages/HomePage.jsx +++ b/frontend/src/pages/HomePage.jsx @@ -6,18 +6,12 @@ import UserContext from "../contexts/UserContext"; const HomePage = () => { const { currentUser } = useContext(UserContext); - useEffect(() => { - if (!currentUser?.id) { - window.location.replace("/login"); - } - }, [currentUser]); - return (
- +
-

This is the home page

+

Welcome back {currentUser?.username}!

diff --git a/frontend/src/pages/LoginPage.jsx b/frontend/src/pages/LoginPage.jsx index 21ef318..6f49bfc 100644 --- a/frontend/src/pages/LoginPage.jsx +++ b/frontend/src/pages/LoginPage.jsx @@ -1,5 +1,6 @@ import React, { useContext, useEffect, useState } from "react"; import { Button, Col, Container, Form, Row, Alert } from "react-bootstrap"; +import { Link } from "wouter"; import MyNavbar from "../components/MyNavbar"; import UserContext from "../contexts/UserContext"; import { makeRequest } from "../utils.ts"; @@ -12,7 +13,6 @@ const LoginPage = () => { const { currentUser, setCurrentUser } = useContext(UserContext); useEffect(() => { - console.log(currentUser); if (currentUser?.id) { gotoHome(); } @@ -42,14 +42,16 @@ const LoginPage = () => { return ( - {error && ( - - - {error.message} - - - )} + {error && ( + + + {error.message} + + + )} +

Login

+
@@ -87,7 +89,7 @@ const LoginPage = () => {

- Don't have an account? Register here + Don't have an account? Register here

diff --git a/frontend/src/pages/RegisterPage.jsx b/frontend/src/pages/RegisterPage.jsx new file mode 100644 index 0000000..1288cf5 --- /dev/null +++ b/frontend/src/pages/RegisterPage.jsx @@ -0,0 +1,146 @@ +import React, { useState } from "react"; +import { Button, Col, Container, Form, Row, Alert } from "react-bootstrap"; +import { Link } from "wouter"; +import MyNavbar from "../components/MyNavbar"; +import { makeRequest } from "../utils.ts"; + +const RegisterPage = () => { + const [role, setRole] = useState("student"); + const [username, setUsername] = useState(""); + const [email, setEmail] = useState(""); + const [password, setPassword] = useState(""); + const [password2, setPassword2] = useState(""); + + const [error, setError] = useState(null); + + const sendRegisterRequest = (e) => { + e?.preventDefault(); + makeRequest({ + url: "http://localhost:5000/register", + method: "POST", + body: { + role, + email, + username, + password, + password2, + }, + }) + .then((resp) => resp.json()) + .then((data) => { + if (data.error) { + setError(data); + return; + } + window.location.href = "/login"; + }); + }; + + return ( + + + + {error && ( + + + {error.message} + + + )} +

Register

+
+
+ + + Role + + + { + setRole(e.target.value); + }} + > + + + + + + + + Username + + + { + setUsername(e.target.value); + }} + /> + + + + + Email + + + { + setEmail(e.target.value); + }} + /> + + + + + Password + + + { + setPassword(e.target.value); + }} + /> + + + + + Confirm Password + + + { + setPassword2(e.target.value); + }} + /> + + + + + + + + + +

+ Already have an account? Login here +

+
+
+
+
+ ); +}; + +export default RegisterPage; -- 2.49.1 From d05e0fb0e0cbeac91c4cefbb9c231c7f5a9bfbab Mon Sep 17 00:00:00 2001 From: Jagraj Aulakh Date: Sun, 19 Mar 2023 16:41:52 -0400 Subject: [PATCH 2/2] #21 Cleanup --- frontend/src/components/MyNavbar.jsx | 36 +++++++++++++++------------- frontend/src/pages/HomePage.jsx | 2 +- frontend/src/pages/LoginPage.jsx | 5 ++-- frontend/src/pages/RegisterPage.jsx | 5 ++-- 4 files changed, 26 insertions(+), 22 deletions(-) diff --git a/frontend/src/components/MyNavbar.jsx b/frontend/src/components/MyNavbar.jsx index 29df7fa..d63cf7a 100644 --- a/frontend/src/components/MyNavbar.jsx +++ b/frontend/src/components/MyNavbar.jsx @@ -7,26 +7,28 @@ const MyNavbar = () => { const { currentUser } = useContext(UserContext); const MyLink = ({ children, ...rest }) => { - return {children}; + return ( + + {children} + + ); }; return ( - - - - LearningTree - - - - - - - + + + LearningTree + + + + + + ); }; diff --git a/frontend/src/pages/HomePage.jsx b/frontend/src/pages/HomePage.jsx index 46b1559..467f6d8 100644 --- a/frontend/src/pages/HomePage.jsx +++ b/frontend/src/pages/HomePage.jsx @@ -1,4 +1,4 @@ -import { useContext, useEffect } from "react"; +import { useContext } from "react"; import { Container } from "react-bootstrap"; import MyNavbar from "../components/MyNavbar"; import UserContext from "../contexts/UserContext"; diff --git a/frontend/src/pages/LoginPage.jsx b/frontend/src/pages/LoginPage.jsx index 6f49bfc..5f55e96 100644 --- a/frontend/src/pages/LoginPage.jsx +++ b/frontend/src/pages/LoginPage.jsx @@ -1,6 +1,6 @@ import React, { useContext, useEffect, useState } from "react"; import { Button, Col, Container, Form, Row, Alert } from "react-bootstrap"; -import { Link } from "wouter"; +import { Link, useLocation } from "wouter"; import MyNavbar from "../components/MyNavbar"; import UserContext from "../contexts/UserContext"; import { makeRequest } from "../utils.ts"; @@ -11,6 +11,7 @@ const LoginPage = () => { const [error, setError] = useState(null); const { currentUser, setCurrentUser } = useContext(UserContext); + const [location, setLocation] = useLocation(); useEffect(() => { if (currentUser?.id) { @@ -19,7 +20,7 @@ const LoginPage = () => { }, [currentUser]); const gotoHome = () => { - window.location.href = "/"; + setLocation("/"); }; const sendLoginRequest = async (e) => { diff --git a/frontend/src/pages/RegisterPage.jsx b/frontend/src/pages/RegisterPage.jsx index 1288cf5..37796d5 100644 --- a/frontend/src/pages/RegisterPage.jsx +++ b/frontend/src/pages/RegisterPage.jsx @@ -1,6 +1,6 @@ import React, { useState } from "react"; import { Button, Col, Container, Form, Row, Alert } from "react-bootstrap"; -import { Link } from "wouter"; +import { Link, useLocation } from "wouter"; import MyNavbar from "../components/MyNavbar"; import { makeRequest } from "../utils.ts"; @@ -12,6 +12,7 @@ const RegisterPage = () => { const [password2, setPassword2] = useState(""); const [error, setError] = useState(null); + const [location, setLocation] = useLocation(); const sendRegisterRequest = (e) => { e?.preventDefault(); @@ -32,7 +33,7 @@ const RegisterPage = () => { setError(data); return; } - window.location.href = "/login"; + setLocation("/login"); }); }; -- 2.49.1