From c195d3becb4303a396ff6d18e7fbcc870420319a Mon Sep 17 00:00:00 2001 From: PrathamX595 Date: Mon, 10 Jun 2024 23:20:14 +0530 Subject: [PATCH] Frontend: Added Login Page with modifications to other components added a Login page with changes to input and button components. routes for login and signin pages were also added Fixes: #79 integrated AuthContext added Signup page --- frontend/src/App.tsx | 4 + frontend/src/Navbar.tsx | 28 +++-- frontend/src/contexts/AuthContext.tsx | 14 +-- frontend/src/library/button.tsx | 3 + frontend/src/library/input.tsx | 2 +- frontend/src/pages/Home.tsx | 2 +- frontend/src/pages/Login.tsx | 125 +++++++++++++++++++++++ frontend/src/pages/SignUp.tsx | 142 ++++++++++++++++++++++++++ 8 files changed, 300 insertions(+), 20 deletions(-) create mode 100644 frontend/src/pages/Login.tsx create mode 100644 frontend/src/pages/SignUp.tsx diff --git a/frontend/src/App.tsx b/frontend/src/App.tsx index 328ad8e..4c00605 100644 --- a/frontend/src/App.tsx +++ b/frontend/src/App.tsx @@ -6,6 +6,8 @@ import Error from './pages/Error'; import Game from './pages/Game'; import About from './pages/About'; import { AuthProvider } from './contexts/AuthContext'; +import Login from './pages/Login'; +import SignUp from './pages/SignUp' const router = createBrowserRouter([ { @@ -23,6 +25,8 @@ const router = createBrowserRouter([ }, { path: '/about', element: }, { path: '/error', element: }, + { path: '/login', element: }, + { path: '/signup', element: } ], }, ]); diff --git a/frontend/src/Navbar.tsx b/frontend/src/Navbar.tsx index c43565c..5cb4348 100644 --- a/frontend/src/Navbar.tsx +++ b/frontend/src/Navbar.tsx @@ -36,17 +36,23 @@ const Navbar: React.FC = ({ /> - {!isLoggedIn && ( -
-
- )} + {isLoggedIn ? ( + <> + + + ) : ( + <> +
+
+ + )} + {isOpen && (
diff --git a/frontend/src/contexts/AuthContext.tsx b/frontend/src/contexts/AuthContext.tsx index 78b6d25..4b34852 100644 --- a/frontend/src/contexts/AuthContext.tsx +++ b/frontend/src/contexts/AuthContext.tsx @@ -8,12 +8,12 @@ import { export type User = { name: string; - email: string; + pass: string; }; export type AuthContextProps = { getUser: () => User | null; - login: () => void; + login: (arg0: string, arg1: string) => void; logout: () => void; isLoggedIn: () => boolean; }; @@ -30,14 +30,14 @@ export function useAuth() { } export function AuthProvider({ children }: { children: ReactElement }) { - const [user, setUser] = useState(null); + const [user, setUser] = useState(null); - const login = useCallback(() => { + const login = useCallback((name: string, pass: string) => { setUser({ - name: 'John Doe', - email: '', + name: name, + pass: pass, }); - }, []); + }, [setUser]); const logout = useCallback(() => { setUser(null); diff --git a/frontend/src/library/button.tsx b/frontend/src/library/button.tsx index c9ba58a..af848eb 100644 --- a/frontend/src/library/button.tsx +++ b/frontend/src/library/button.tsx @@ -15,6 +15,7 @@ type ButtonProps = { fontSize?: string; rounded?: string; buttonSize?: string; + type?: "submit" | "reset" | "button" borderColor?: string; hoverColor?: string; hoverScale?: boolean; @@ -36,11 +37,13 @@ const Button: React.FC = ({ borderColor = 'border-black', hoverColor = 'hover:bg-lime-600', hoverScale = true, + type = 'button', onClick, className = '', }) => { return (
+
*/} +
+
+
+
+ +
+
+ +
+
+
+
+
+
+
+ Don't have an account? Sign up +
+
+
+ + + + + + ); +}; + +export default Login; \ No newline at end of file diff --git a/frontend/src/pages/SignUp.tsx b/frontend/src/pages/SignUp.tsx new file mode 100644 index 0000000..993931e --- /dev/null +++ b/frontend/src/pages/SignUp.tsx @@ -0,0 +1,142 @@ +import React, { useState, useEffect } from 'react'; +import Button from '../library/button'; +import Input from '../library/input'; +import Navbar from '../Navbar'; +import { Link } from 'react-router-dom'; +import { useAuth } from '../contexts/AuthContext'; +import '../index.css'; + +const SignUp: React.FC = () => { + const [name, setName] = useState(''); + const [password, setPassword] = useState(''); + const [confirmPass, setConfirmPass] = useState(''); + const [isLoggedIn, setIsLoggedIn] = useState(false); + const [username, setUsername] = useState(''); + const auth = useAuth(); + + const handleLogin = () => { + setUsername('Username_7'); + setIsLoggedIn(true); + }; + + const handleLogout = () => { + setUsername(''); + setIsLoggedIn(false); + }; + + const handelConfirmPassChange = (event: React.ChangeEvent) => { + setConfirmPass(event.target.value); + }; + + const handleNameChange = (event: React.ChangeEvent) => { + setName(event.target.value); + }; + + const handlePasswordChange = (event: React.ChangeEvent) => { + setPassword(event.target.value); + }; + + + const handleSubmit = (event: React.FormEvent) => { + event.preventDefault(); + console.log('Form submitted'); + if(password === confirmPass){ + auth.login(name, password) + console.log("successfull signup") + }else{ + console.log("failed signup"); + } + }; + + useEffect(() => { + const user = auth.getUser(); + console.log(user); + setIsLoggedIn(auth.isLoggedIn()); + console.log(isLoggedIn); + }, [auth, auth.getUser()]); + + return ( + <> +
+ +
+
+ {/*
+
+
*/} +
+
+
+
+ +
+
+ +
+
+ +
+
+
+
+
+
+
+ Already have an account? Login +
+
+
+
+
+
+ + + ); +}; + +export default SignUp; \ No newline at end of file