Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assignment 3 #6

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add token in local storage
  • Loading branch information
david-eom committed Oct 25, 2023
commit 82eeecb669fd9466ac0bd4747a3f753a9c81fe07
8 changes: 3 additions & 5 deletions frontend/src/components/Header/Header.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { IconButton, Menu, MenuButton, MenuItem, MenuList } from "@chakra-ui/react";
import { Button, IconButton, Menu, MenuButton, MenuItem, MenuList } from "@chakra-ui/react";

import { HamburgerIcon } from "@chakra-ui/icons";
import Link from "next/link";
import { logoutUser } from "@/utils/auth";
import styles from "./Header.module.css";
import { useRouter } from "next/router";

Expand All @@ -16,9 +17,6 @@ export default function Header(): JSX.Element {

{/* Desktop navbar */}
<nav className={styles.nav_links}>
<Link href="/register" className={styles.nav_link}>
Register
</Link>
<Link href="/questions" className={styles.nav_link}>
Questions
</Link>
Expand All @@ -28,14 +26,14 @@ export default function Header(): JSX.Element {
<Link href="#" className={styles.nav_link}>
Practice for interviews
</Link>
<Button onClick={logoutUser}>Logout</Button>
</nav>

{/* Mobile navbar */}
<div className={`${styles.hidden}`}>
<Menu>
<MenuButton as={IconButton} aria-label="Options" icon={<HamburgerIcon />} variant="outline" />
<MenuList>
<MenuItem onClick={() => router.push("/register")}>Register</MenuItem>
<MenuItem onClick={() => router.push("/questions")}>Questions</MenuItem>
<MenuItem onClick={() => router.push("/profiles")}>View Profiles</MenuItem>
<MenuItem onClick={() => router.push("#")}>Practice for interviews</MenuItem>
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/components/Users/Login/LoginForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export default function LoginForm(): JSX.Element {
try {
const token = await loginUser(userForm);
localStorage.setItem("token", token);
window.location.href = `/`;
window.location.href = `/questions`;
} catch (error: any) {
toast({
title: error.message,
Expand Down
27 changes: 8 additions & 19 deletions frontend/src/pages/index.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,11 @@
import Head from "next/head";
import Layout from "@/components/Layout/Layout";
import QuestionsTable from "@/components/Questions/QuestionsTable/QuestionsTable";
import Login from "./login";
import Questions from "./questions";
import { useToken } from "@/utils/auth";

// TODO: get user and decide to go to either questions or login
export default function Home() {
return (
<>
<Head>
<title>PeerPrep</title>
<meta name="description" content="Question bank to ace your technical interviews" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="icon" href="/favicon.ico" />
</Head>
<main>
<Layout>
<QuestionsTable />
</Layout>
</main>
</>
);
const token = useToken();
if (token) {
window.location.href = "/questions";
}
return <Login />;
}
20 changes: 18 additions & 2 deletions frontend/src/utils/auth.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { useEffect, useState } from "react";

import { LoginForm } from "@/interfaces";

const LOGIN_USER_API = "http://localhost:8000/users/login";
Expand All @@ -19,6 +21,20 @@ export async function loginUser(loginForm: LoginForm): Promise<string> {
return fetchDataOrThrowError(LOGIN_USER_API, requestOptions);
}

export function getToken() {
return localStorage.getItem("token");
export function logoutUser(): void {
if (typeof window !== "undefined" && window.localStorage) {
localStorage.removeItem("token");
window.location.href = "/";
}
}

export function useToken() {
const [token, setToken] = useState<string>("");
useEffect(() => {
const token = localStorage.getItem("token");
if (token) {
setToken(token);
}
}, []);
return token;
}
1 change: 0 additions & 1 deletion frontend/src/utils/userApi.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { LoginForm, UpdateUserProfileForm, User, UserForm } from "@/interfaces";

const CREATE_USER_API = "http://localhost:8000/users/register";

const USERS_API = "http://localhost:8000/users";

async function fetchDataOrThrowError(api: string, requestOptions = {}): Promise<any> {
Expand Down