Skip to content

Commit

Permalink
Frontend: Improved Auth UX
Browse files Browse the repository at this point in the history
auth context was used to use the username on the navbar and sidepanel

integrated AuthContext

added Signup page

implemented Login and SignUp

error fixes

error fixes

eslint fix

fixes
  • Loading branch information
PrathamX595 committed Jun 14, 2024
1 parent d0c5a4e commit fbb5f97
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 11 deletions.
2 changes: 1 addition & 1 deletion frontend/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const router = createBrowserRouter([
{ path: '/about', element: <About /> },
{ path: '/error', element: <Error /> },
{ path: '/login', element: <Login /> },
{ path: '/signup', element: <SignUp /> },
{ path: '/register', element: <SignUp /> },
],
},
]);
Expand Down
23 changes: 21 additions & 2 deletions frontend/src/Navbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Button from './library/button';
import './index.css';
import { useNavigate } from 'react-router-dom';
import RulesModal from './library/rulesModal';
import { useAuth } from './contexts/AuthContext';

type NavbarProps = {
isLoggedIn?: boolean;
Expand All @@ -22,12 +23,20 @@ const Navbar: React.FC<NavbarProps> = ({
const [isOpen, setIsOpen] = useState(false);
const [showRulesModal, setShowRulesModal] = useState(false);

const auth = useAuth();

const user = auth.getUser();

const navigate = useNavigate();

const toggleMenu = () => {
setIsOpen(!isOpen);
};

const goToLogin = () => {
navigate('/login');
};

return (
<div>
<div className="flex justify-between items-center p-4 ml-4 mt-4 mr-2 relative z-10">
Expand All @@ -40,8 +49,17 @@ const Navbar: React.FC<NavbarProps> = ({
/>
</button>
</div>
{isLoggedIn ? (
<></>
{auth.isLoggedIn() ? (
<>
<div className="text-xl font-bold mt-2">
<Button
text={user ? user.name : 'Noname'}
buttonSize="w-56 h-11"
className="border-4"
rounded="rounded-2xl"
/>
</div>
</>
) : (
<>
<div className="text-xl font-bold mt-2">
Expand All @@ -50,6 +68,7 @@ const Navbar: React.FC<NavbarProps> = ({
buttonSize="w-56 h-11"
className="border-4"
rounded="rounded-2xl"
onClick={goToLogin}
/>
</div>
</>
Expand Down
17 changes: 10 additions & 7 deletions frontend/src/pages/Login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,26 @@ import Input from '../library/input';
import Navbar from '../Navbar';
import { Link } from 'react-router-dom';
import { useAuth } from '../contexts/AuthContext';
import { useNavigate } from 'react-router-dom';
import '../index.css';

const Login: React.FC = () => {
const [name, setName] = useState('');
const [password, setPassword] = useState('');
const [isLoggedIn, setIsLoggedIn] = useState(false);
const [username, setUsername] = useState('');
const navigate = useNavigate();
const auth = useAuth();
const user = auth.getUser();

const handleLogin = () => {
setUsername('Username_7');
setIsLoggedIn(true);
setUsername(username);
console.log(auth.isLoggedIn());
};

const handleLogout = () => {
setUsername('');
setIsLoggedIn(false);
auth.logout();
};

const handleNameChange = (event: React.ChangeEvent<HTMLInputElement>) => {
Expand All @@ -36,9 +39,9 @@ const Login: React.FC = () => {
const handleSubmit = (event: React.FormEvent<HTMLFormElement>) => {
event.preventDefault();
console.log('Form submitted');
const user = auth.getUser();
if (user?.name == name && user.pass == password) {
console.log('Successful Login');
navigate('/home');
} else {
console.log('Failed Login');
}
Expand All @@ -47,13 +50,13 @@ const Login: React.FC = () => {
useEffect(() => {
setIsLoggedIn(auth.isLoggedIn());
console.log(isLoggedIn);
}, [auth, auth.getUser()]);
}, [auth, user, isLoggedIn]);

return (
<>
<div className="min-h-screen bg-uno-bg bg-cover bg-center flex flex-col relative">
<Navbar
isLoggedIn={true} //true here so that navbar does not render signin/login button
isLoggedIn={auth.isLoggedIn()} //true here so that navbar does not render signin/login button
username={username}
onLogin={handleLogin}
onLogout={handleLogout}
Expand Down Expand Up @@ -113,7 +116,7 @@ const Login: React.FC = () => {
<div className="font-kavoon flex justify-center items-center my-3">
Don't have an account?{' '}
<Link
to="/signup"
to="/register"
className=" text-blue-700 "
>
{' '}
Expand Down
6 changes: 5 additions & 1 deletion frontend/src/pages/SignUp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Input from '../library/input';
import Navbar from '../Navbar';
import { Link } from 'react-router-dom';
import { useAuth } from '../contexts/AuthContext';
import { useNavigate } from 'react-router-dom';
import '../index.css';

const SignUp: React.FC = () => {
Expand All @@ -12,7 +13,9 @@ const SignUp: React.FC = () => {
const [confirmPass, setConfirmPass] = useState('');
const [isLoggedIn, setIsLoggedIn] = useState(false);
const [username, setUsername] = useState('');
const navigate = useNavigate();
const auth = useAuth();
const user = auth.getUser();

const handleLogin = () => {
setUsername('Username_7');
Expand Down Expand Up @@ -46,6 +49,7 @@ const SignUp: React.FC = () => {
if (password === confirmPass) {
auth.login(name, password);
console.log('successfull signup');
navigate('/');
} else {
console.log('failed signup');
}
Expand All @@ -56,7 +60,7 @@ const SignUp: React.FC = () => {
console.log(user);
setIsLoggedIn(auth.isLoggedIn());
console.log(isLoggedIn);
}, [auth, auth.getUser()]);
}, [auth, user, isLoggedIn]);

return (
<>
Expand Down

0 comments on commit fbb5f97

Please sign in to comment.