Skip to content

Commit

Permalink
feat: refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
HereEast committed Oct 6, 2024
1 parent d40c8fe commit 1e14e2d
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 6 deletions.
12 changes: 8 additions & 4 deletions client/src/components/layouts/Header.tsx
Original file line number Diff line number Diff line change
@@ -1,28 +1,32 @@
import Link from "next/link";
// import { useRouter } from "next/router";
import { useRouter } from "next/router";

import { Button } from "../ui/Button";
import { useAuthContext } from "~/hooks";

export function Header() {
// const router = useRouter();
const router = useRouter();

const { user, isAuth, setIsAuth } = useAuthContext();

// Log out
function handleLogout() {
localStorage.removeItem("token");
setIsAuth(false);

router.push("/");
}

return (
<div className="fixed flex h-16 w-full items-center justify-between bg-stone-300 px-10">
<h1 className="font-medium">Habit Tracker</h1>
<h1 className="font-medium hover:opacity-50">
<Link href="/">Habit Tracker</Link>
</h1>
<div className="flex items-center gap-2">
<div>
{isAuth ? (
<div className="flex items-center gap-4">
<span className="text-sm font-medium">👋 {user}</span>
{user && <span className="text-sm font-medium">👋 {user}</span>}
<Button onClick={handleLogout}>Log out</Button>
</div>
) : (
Expand Down
2 changes: 1 addition & 1 deletion client/src/components/layouts/Login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export function LoginForm() {
const decodedUser = jwt.decode(token) as JwtPayload;

if (decodedUser) {
router.replace(`/${decodedUser.username}`);
router.replace("/app");
}
}
} catch (err) {
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion client/src/pages/login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default function LoginPage() {

useEffect(() => {
if (!isAuthLoading && isAuth) {
router.replace("/");
router.replace("/app");
}
}, [isAuth, router, isAuthLoading]);

Expand Down

0 comments on commit 1e14e2d

Please sign in to comment.