<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Hashim KHAN</title>
    <description>The latest articles on DEV Community by Hashim KHAN (@hashim_khan_86fa8907af167).</description>
    <link>https://dev.to/hashim_khan_86fa8907af167</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1921619%2F865d7946-1ade-4022-97f0-5ee09a25c448.jpg</url>
      <title>DEV Community: Hashim KHAN</title>
      <link>https://dev.to/hashim_khan_86fa8907af167</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/hashim_khan_86fa8907af167"/>
    <language>en</language>
    <item>
      <title>MERN Stack: User Data Not Available in Context After Login</title>
      <dc:creator>Hashim KHAN</dc:creator>
      <pubDate>Tue, 13 Aug 2024 06:41:16 +0000</pubDate>
      <link>https://dev.to/hashim_khan_86fa8907af167/mern-stack-user-data-not-available-in-context-after-login-h9p</link>
      <guid>https://dev.to/hashim_khan_86fa8907af167/mern-stack-user-data-not-available-in-context-after-login-h9p</guid>
      <description>&lt;p&gt;I'm building a contact form in my MERN stack application, but I'm having trouble accessing the user data from the authentication context. I can see the token is being stored in localStorage, and the user is logged in, but the user data is undefined when I try to use it in my ContactMe component.&lt;/p&gt;

&lt;p&gt;Here's the relevant part of my code:&lt;/p&gt;

&lt;p&gt;Auth.jsx (Authentication Context):&lt;/p&gt;

&lt;p&gt;import { createContext, useContext, useState, useEffect } from "react";&lt;/p&gt;

&lt;p&gt;export const AuthContext = createContext();&lt;/p&gt;

&lt;p&gt;export const AuthProvider = ({ children }) =&amp;gt; {&lt;br&gt;
  const [token, setToken] = useState(localStorage.getItem("token"));&lt;br&gt;
  const [user, setUser] = useState("");&lt;/p&gt;

&lt;p&gt;const userAuthentication = async () =&amp;gt; {&lt;br&gt;
    try {&lt;br&gt;
      const response = await fetch("&lt;a href="http://localhost:5000/api/auth/user" rel="noopener noreferrer"&gt;http://localhost:5000/api/auth/user&lt;/a&gt;", {&lt;br&gt;
        method: "GET",&lt;br&gt;
        headers: {&lt;br&gt;
          Authorization: &lt;code&gt;Bearer ${token}&lt;/code&gt;,&lt;br&gt;
        },&lt;br&gt;
      });&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  if (response.ok) {
    const data = await response.json();
    setUser(data.userData);
  } else {
    console.error("Error fetching user data");
  }
} catch (error) {
  console.log(error);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;};&lt;/p&gt;

&lt;p&gt;useEffect(() =&amp;gt; {&lt;br&gt;
    userAuthentication();&lt;br&gt;
  }, []);&lt;/p&gt;

&lt;p&gt;return (&lt;br&gt;
    &lt;br&gt;
      {children}&lt;br&gt;
    &lt;br&gt;
  );&lt;br&gt;
};&lt;/p&gt;

&lt;p&gt;export const useAuth = () =&amp;gt; {&lt;br&gt;
  return useContext(AuthContext);&lt;br&gt;
};`&lt;/p&gt;

&lt;p&gt;ContactMe.jsx (Contact Form Component):&lt;/p&gt;

&lt;p&gt;`import React, { useState } from 'react';&lt;br&gt;
import { useAuth } from "../store/Auth";&lt;/p&gt;

&lt;p&gt;const defaultContactFormData = {&lt;br&gt;
    username: "",&lt;br&gt;
    email: "",&lt;br&gt;
    message: "",&lt;br&gt;
};&lt;/p&gt;

&lt;p&gt;export const ContactMe = () =&amp;gt; {&lt;br&gt;
    const { user } = useAuth();&lt;br&gt;
    const [data, setData] = useState(defaultContactFormData);&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if (user &amp;amp;&amp;amp; user.email) {
    console.log("User email:", user.email);
} else {
    console.log("User data is not available or email is missing");
}

// ...rest of your component code
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;}; &lt;br&gt;
Problem: When the ContactMe component renders, the user data is undefined, and I get the console log stating "User data is not available or email is missing." I expected the user data to be available after successful authentication.&lt;/p&gt;

&lt;p&gt;What I Tried: I checked the token is being stored correctly in localStorage and is being used to fetch user data from the backend. I verified that the userAuthentication function is called in the useEffect hook to fetch user data after the component mounts. I confirmed that the backend is returning the user data correctly. What I Expected: I expected the user object to be populated with user data (including email) in the ContactMe component after a successful login, but it remains undefined, leading to the message "User data is not available or email is missing."&lt;/p&gt;

&lt;p&gt;Question: Why is the user data not available in the ContactMe component? How can I ensure that the user data is correctly fetched and passed to components that need it? Any insights or suggestions would be greatly appreciated!&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
