how to show the logged in’s profile in a react?

I’m new in developing using react and I want to build a profile page that show a logged in user through it token stored in local storage and i always end up with a problem.

My model have a Name, email (unique), password and role
i want to know if i have to should add token in my model or not and how to implement it in my code.
Either i can’t read the token or i cant implement it in front page (i’m still learning)


exports.profile = (req, res) => {
    try {
        const user = req.user; // User object attached by the middleware
        console.log('aabb')
        // Return the user's profile data
        res.json(user.profil());
    } catch (error) {
        console.log('Error during loading profil:', error.message);
        return res.status(402).json({ error: 'Internal profil error' });
    }
}


//middleware
module.exports = asyncHandler(async (req, res, next) => {
  const getSecretFromUser = async (userId) => {
    try {
      console.log('aaabbb ')
      const user = await User.findOne(userId);
      if (user) {
        console.log(user.jwtSecret)
        return user.jwtSecret; // Assuming the user document has a jwtSecret field
      }
    } catch (error) {
      console.error('Error fetching JWT secret:', error);
    }
    return null;
  };

  • I’m going to try my best to help you, your English is hard to understand. I think you are asking about JWT(json web token) is that correct? If not you should look into that.

    – 

Leave a Comment