Is there a way to import the server url in react application

23 views Asked by At

I'm building a Mern application, and deployed the backend to a server, so users won't be making requests to the localhost. I have this piece of code:

  const login = () => {
    setLoginError(null);
    axios
      .post("server-url/api/users/login", { email, password })
      .then((data) => {
        localStorage.setItem("user", JSON.stringify(data.data));
        navigate("/myportal");
        setLoggedIn(true);
      })
      .catch((err) => {
        setLoginError(err.response.data.error);
      });
  };

Now, i tried to import the url from a .env file:

const URL = process.env.URL;

`${URL}/api/users/login`

but that did not work. When i deployed the frontend and tried to login i got "undefined/api/users/login".

Am i doing something wrong with the .env or is there a better way to achieve it if there is any?

0

There are 0 answers