Next.js Discord

Discord Forum

handling

Unanswered
American posted this in #help-forum
Open in Discord
AmericanOP
import React, { useEffect, useState } from 'react';
import axios from 'axios';
import { useNavigate } from 'react-router-dom';

const Navbar = () => {
  const [auth, setAuth] = useState(null); // Change initial state to null
  const [loading, setLoading] = useState(true); // Add loading state
  const navigate = useNavigate();

  useEffect(() => {
    axios.get('http://localhost:8801/', { withCredentials: true })
      .then(res => {
        if (res.data.Status === "Success") {
          setAuth(true);
        } else {
          setAuth(false);
        }
        setLoading(false); 
      })
      .catch(err => {
        console.error('Error fetching auth status:', err);
        setAuth(false);
        setLoading(false); // Update loading state after fetching auth status
      });
  }, []); // Remove auth from dependency array to prevent infinite loop

  const handleLogout = () => {
    axios.get('http://localhost:8801/logout', { withCredentials: true })
      .then(res => {
        if (res.data.Status === "Success") {
          setAuth(false);
          navigate('/signin');
          window.alert('You have logged out. Please sign in to continue.');
        } else {
          console.error('Logout failed:', res.data.Error || 'Unknown error');
        }
      })
      .catch(err => {
        console.error('Error logging out:', err);
      });
  };

  if (loading || !auth) {
    return null;
  }

  return (
    {Navbar implemented here had to delete due to text limit}

  );
};

export default Navbar;

18 Replies

AmericanOP
so basically i
the navbar only appears
after i reload
how can i make it appear like instantly when you log in without having the client side to reload the page
Asian paper wasp
You added this yourself...
if (loading || !auth) {
  return null;
}
@Asian paper wasp You added this yourself... ts if (loading || !auth) { return null; }
AmericanOP
yeah because i dont want the navbar visisble
on the sign in page
i only want it to be visisble when you log in
is there any fix for this cuz im kinda confused how to make it automatically render in
w out the user having to reload
@American i only want it to be visisble when you log in
import { useLocation } from 'react-router-dom';

const location = useLocation();

if (location.pathname === '/signin' || !auth) {
  return null
}
AmericanOP
oh
wait thanks dawg
bro
but like its visisble
but i have to reload the page
in order to see it