how to get user id after token authentication - REACT

Viewed 503

I'm new at coding so hoping this is something simple. I am trying to create delete/update functions for the user when they go to their profile. To delete and update my api needs the user id for:

fetch(${process.env.REACT_APP_API_URL}users/${id}/,

Token authentication works and is stored in the localhost but I can't seem to access the id of the user after login. How can I store the id and call it in this function?

**Notes: the 'userData' const was an attempt at calling the api by: fetch(${process.env.REACT_APP_API_URL}users/${userData.id}/, (didn't work)

my ProfilePage code:

const Profile = () => {
const [userData, setUserData] = useState({
    username: "",
    email: "",
    password: "",
  });
  const { id } = useParams();
  const navigate = useNavigate();

  const EditAccount = () => {
    navigate("/edit-account");
  };

  const addCar = () => {
    navigate("/home");
  };
  useEffect(() => {
    fetch(`${process.env.REACT_APP_API_URL}users/${id}/`)
      .then((results) => {
        console.log("results", results);
        return results.json();
      })
      .then((data) => {
        setUserData(data);
      });
  });

  const Logout = () => {
    localStorage.clear();
    window.location.href = "/";
  };

  const DeleteUser = async () => {
    fetch(`${process.env.REACT_APP_API_URL}users/${id}/`, {
      method: "delete",
      headers: {
        Authorization: `Token ${localStorage.getItem("token")}`,
      },
    });
    navigate("/");
  };
0 Answers
Related