Skip to content

Commit

Permalink
fix navbar authstatus
Browse files Browse the repository at this point in the history
  • Loading branch information
ankushchk committed Jul 24, 2024
1 parent b83c62e commit 3979eae
Show file tree
Hide file tree
Showing 5 changed files with 215 additions and 29,484 deletions.
38 changes: 23 additions & 15 deletions frontend/src/components/Navbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,34 +13,41 @@ import { User } from "../types/types";

const Navbar: React.FC = () => {
const [authStatus, setAuthStatus] = useRecoilState(authState);
const [user, setUser] = useRecoilState<User | null>(userState); // Updated state to match type
const [user, setUser] = useRecoilState<User | null>(userState);
const navigate = useNavigate();

async function getAdminRouteMe() {
const token = localStorage.getItem("token");
if (token && token !== "null") {
const res = await axios.get("https://api.alchemists.life", {
headers: {
Authorization: "Bearer " + token,
},
});
const data = res.data;
if (data.username) {
setAuthStatus(true);
setUser({ username: data.username });
localStorage.setItem(
"user",
JSON.stringify({ username: data.username })
);
try {
const apiUrl = "https://api.alchemists.life/admin/me";
const res = await axios.get(apiUrl, {
headers: {
Authorization: "Bearer " + token,
},
});
const data = res.data;
if (data.username) {
console.log("Setting auth status and user");
setAuthStatus(true);
setUser({ username: data.username });
localStorage.setItem(
"user",
JSON.stringify({ username: data.username })
);
}
} catch (error) {
console.error("API Error:", error);
}
}
}

// Load user data from localStorage when the component mounts
useEffect(() => {
const savedUser = localStorage.getItem("user");
console.log("Saved User from localStorage:", savedUser);
if (savedUser) {
const userData = JSON.parse(savedUser);
console.log("Parsed User Data:", userData);
setUser(userData);
setAuthStatus(true);
} else {
Expand All @@ -50,6 +57,7 @@ const Navbar: React.FC = () => {

// Handle user logout
const handleLogout = () => {
console.log("Logging out user");
localStorage.setItem("token", "null");
localStorage.removeItem("user");
setAuthStatus(false);
Expand Down
2 changes: 1 addition & 1 deletion server/dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const user_1 = __importDefault(require("./routes/user"));
const cors_1 = __importDefault(require("cors"));
const path_1 = __importDefault(require("path"));
const app = (0, express_1.default)();
const port = "3000";
const port = "3001";
app.use((0, cors_1.default)());
app.use(express_1.default.json());
app.use("/admin", admin_1.default);
Expand Down
Loading

0 comments on commit 3979eae

Please sign in to comment.