I know this isn’t the best way of managing authentication, but I haven’t used React in years and don’t understand why this doesn’t work. The log in useEffect always returns null. Why doesn’t it respect async/await?
Thanks in advance!
“`javascript const AppComponent = () => { const [ currentUser, setCurrentUser] = useState(null);
const getCurrentUser = async () => { const token = localStorage.getItem(“token”); const response = await axios.get( “/api/users/currentuser”, {}, { headers: { Authorization: `Bearer ${token}` } } ); setCurrentUser(response.data.currentUser); } useEffect(() => { getCurrentUser(); console.log(currentUser) }, []); return (
<div> <Header currentUser={currentUser}/> … “`
submitted by /u/ridgekuhn
[link] [comments]