1

I am implementing a share post in my react web app. I am using react-share library. And I am passing the current URL using const shareUrl = window.location.href;

But the user tries to view the post using that URL which I shared. He is asked to log in first. .. This is because I have written Axios interceptor.

 axiosRequest.interceptors.response.use(
      (response) =>
     new Promise((resolve, reject) => {
  resolve(response);
}),
    (error) => {
if (!error.response) {
  return new Promise((resolve, reject) => {
    reject(error);
  });
}

if (error.response.status === 401) {
  sessionStorage.removeItem("token");

  window.location = "/";
} else {
  return new Promise((resolve, reject) => {
    reject(error);
      });
          }
      }
  );

But when the user is successfully logged in he redirects to the feeds page because

   dispatch(login(dataToSubmit))
      .then((response) => {
        window.sessionStorage.setItem("token", response.payload.token);
        window.location.replace("/feeds");
        // setData({...Data,email:values.email, password:values.password, 
      otp:response.data.result.otp})
      })

How can I direct the user to the URL which he was trying to access before he logged in.. I researched that I can store the previous route in local storage but how to implement that in react functional component,?

  • Take a review of the [redirect auth workflow](https://reactrouter.com/web/example/auth-workflow) demo to get an idea how to "save" a referrer and redirect back to the original URL the user was attempting to access prior to authenticating. Other than this I'm wondering why you are using the `window.location` object to do any navigation versus using the tools provided by react-router-dom. – Drew Reese Jun 12 '21 at 06:37
  • const shareUrl = window.location.href; this gives entire url .. In case of react-router, it only gives the path name. – Nidhi S Bhat Jun 12 '21 at 06:48
  • I used history.goBack(); after login but takes back to previous url without id.. how to go back with parameter? – Nidhi S Bhat Jun 12 '21 at 08:23
  • You should be using a series of redirects, forwarding a referrer (i.e. target url) each time instead of a back navigation. The final redirect after authorization is to the original target route. The redirects are required so if at any time the user *does* hit their back button they will navigate back to the screen/page prior to where they were originally attempting to access. – Drew Reese Jun 12 '21 at 08:32

0 Answers0