score:2

you have to use history in reactjs to get previous path and need to save in sessionstorage.

may be this will help you :

const saved_uri = 'app_plu';
const forbidden_uris = ['/login-response', '/'];
const default_uri = '/';

function setpostloginuri() {
  // using just the pathname for demo, should be more detailed in production to
  // include query params, hash bangs, etc
  const currentpath = window.location.pathname;
  const saveduri = sessionstorage.getitem(saved_uri);

  if (forbidden_uris.includes(currentpath) || saveduri) {
    return;
  }

  sessionstorage.setitem(saved_uri, currentpath);
}

function getpostloginuri(retain) {
  const saveduri = sessionstorage.getitem(saved_uri);

  if (!retain) {
    sessionstorage.removeitem(saved_uri);
  }

  return saveduri || default_uri;
}

export default {
  get: getpostloginuri,
  set: setpostloginuri
};

and set in the app.js

and then in your login response page add this code ,

function loginresponse({ history, setuser }) {
  const [error, seterror] = usestate(null);

  useeffect(() => {
    // the login redirect has been completed and we call the
    // signinredirectcallback to fetch the user data
    usermanager.signinredirectcallback().then(user => {
      // received the user data so we set it in the app state and push the
      // router to the secure or bookmarked route
      setuser(user);
      history.push(postloginuri.get());
    }, ({ message }) => {
      // usermanager throws if someone tries to access the route directly or if
      // they refresh on a failed request so we just send them to the app root
      if (message && redirecterrors.includes(message)) {
        history.push('/');
        return;
      }

      // for all other errors just display the message in production it would be
      // a good idea to initiate a sign out after a countdown
      seterror(message);
    });
  }, []);

  return error;
}

export default loginresponse;

Related Query

More Query from same tag