source: frontend/src/pages/LandingPage/components/NavbarLanding.jsx@ 42da64d

Last change on this file since 42da64d was 42da64d, checked in by Andrej <asumanovski@…>, 8 weeks ago

Fixed auth issue

  • Property mode set to 100644
File size: 2.0 KB
Line 
1import React, { useEffect, useState } from "react";
2import { Link, useNavigate } from "react-router-dom";
3import logo from "../../../assets/logo.png";
4
5const NavbarLanding = () => {
6 const navigate = useNavigate();
7 const [isAuthed, setIsAuthed] = useState(() =>
8 Boolean(localStorage.getItem("authToken")),
9 );
10
11 useEffect(() => {
12 const onStorage = (e) => {
13 if (e.key === "authToken") {
14 setIsAuthed(Boolean(e.newValue));
15 }
16 };
17 window.addEventListener("storage", onStorage);
18 return () => window.removeEventListener("storage", onStorage);
19 }, []);
20
21 const onLogout = () => {
22 localStorage.removeItem("authToken");
23 localStorage.removeItem("authUser");
24 setIsAuthed(false);
25 navigate("/", { replace: true });
26 };
27
28 return (
29 <div className="navbar bg-primary text-primary-content shadow-sm px-4 sm:px-6">
30 <div className="navbar-start flex-1 min-w-0">
31 <Link
32 className="btn btn-ghost text-primary-content"
33 aria-label="Trekr home"
34 to="/"
35 >
36 <img src={logo} alt="Trekr" className="h-12 w-auto rounded-2xl" />
37 </Link>
38 </div>
39 <div className="navbar-end flex-none">
40 {isAuthed ? (
41 <div className="flex flex-wrap items-center justify-end gap-2 sm:gap-3">
42 <Link
43 className="btn btn-sm sm:btn-md btn-outline border-primary-content text-primary-content"
44 to="/dashboard"
45 >
46 Dashboard
47 </Link>
48 <button
49 className="btn btn-sm sm:btn-md btn-outline border-primary-content text-primary-content"
50 onClick={onLogout}
51 >
52 Logout
53 </button>
54 </div>
55 ) : (
56 <Link
57 className="btn btn-sm sm:btn-md btn-tertiary text-blue-200! px-6 sm:px-8"
58 to="/login"
59 >
60 Start Tracking
61 </Link>
62 )}
63 </div>
64 </div>
65 );
66};
67
68export default NavbarLanding;
Note: See TracBrowser for help on using the repository browser.