skip to Main Content

I’m finishing my Full Stack Web app (with JWT token authentication based on roles), but im struggling with the front-end. I need to know what is the best practice for obtaining the UserID in the front-end so i can make fetches like "GetObservationsByUserID". Notice that the backend is fully functional.

 const fetchObservations = async (userID) => {
    try {
      const response = await fetch(`${API_URL}/api/GetObservationsByUserID/${userID}`, {
        method: 'GET',
      });

      if (response.ok) {
        const responseData = await response.json();
        if (Array.isArray(responseData.data)) {
          setObservations(responseData.data);
        } else {
          console.error('Observations data is not in the expected format:', responseData);
        }
      } else {
        console.error('Failed to fetch observations:', response.statusText);
      }
    } catch (error) {
      console.error('An error occurred while fetching observations:', error);
    }
  };

PS: nevermind my useEffect. Still learning react

2

Answers


  1. in my job we are using the Jason Tailor template for Clean Architecture.
    https://github.com/jasontaylordev/CleanArchitecture

    And to retrive the current user ( the user that is logged), you can use the https://github.com/jasontaylordev/CleanArchitecture/tree/main/src/Application/Common/Interfaces/ICurrentUserService.cs

    So, if you need to get some information about the logged user, instead of try to pass from your front end, you could use directly from your back-end, as a dependency injection.

    I not sure if I understand de purpose to retrieve the userId on frontend, if I don’t please tell me

    Login or Signup to reply.
  2. You should be using react-router-dom. It’d suggest useParams()

    import { useParams } from 'react-router-dom';
    
    function YourComponent {
      let { userID } = useParams();
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search