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;
Source: stackoverflow.com
Related Query
- Redirect to same URL at client side after authentication from token server (identity server 4)
- How to Get Param Id from URL in express/mongo/mongoose on server side, axios/react/redux on client side
- Is there a cleaner way of getting current URL in both client and server side in isomorphic react apps?
- How to server side redirect to other page without reloading the page and still keeping the url in Nextjs app?
- how to get the ip address of the client from server side in next.js app?
- How to load JWT token on to React during initial download after authentication from Multi Page Application?
- How to download pdf from puppeteer using Nest js as Server Side and React in Client Side?
- how to validate session on react client after successful authentication from express session
- How to handle authentication from a Node Express redirect to React Client (react-router-dom and useContext)
- React Router with Meteor: How to remove code param from URL after redirect from OAuth without reload
- React JS : Migrate from server rendering to pure client side
- ReactJS: Apollo Client set Authentication Token after User is Authenticated
- How could i use the routing in reactjs to redirect from one page after loading to another url
- Redirect url to www in reactjs nextjs as a server side 301
- React server side rendering is not updating after client side route change
- Trying to migrate from React router v3 to v5 , Unable to figure out how to pass 'props.children' and redirect after authentication
- Calling server side method from client returns undefined
- Consistent url routing on client side React app served from Express
- How to remove duplicate slashes from URL in Next Js.How to intercept and modify a request on the server side getServerSideProps?
- Pass value from server side to client side in Next JS using getInitialProps
- Setting up JWT - Passing token from server to client in header, but cannot parse header in client
- Different markup generated on server and client from the same JSX code
- Client side state is not hydrated from server on page reload
- showing an alert box from server side to client side in express js, react js
- Sending form data from react client side to node.js server side
- React_Display Image from server side into Client
- How to invoke a React-router client side redirect to a new url in JS without using <Link>
- NextJS redirect user back to the router came from after authentication
- Display image at client side (react) from mongoDB (that has a path to a folder that stores the images at server side)
- Using react, axios and django, i'm trying to send data from my client side to my server but I can't get the data
More Query from same tag
- How to match a template string in switch statement with js?
- Timer does not countdown in ReactJS
- ReactJS + Material UI weird behaviour
- Regular expression to not accept . at the end of the email address in React
- Conditional useNavigate(), within button, based on error response
- req.user not being sent to react
- Enzyme - how to get an instance of a function from a container, to pass as a prop to a component?
- Image URL doesn't exist while using Sitecore with React JS?
- Can I safe navigate within useMemo
- global.HermesInternal - Property 'HermesInternal' does not exist on type 'Global & typeof globalThis'
- ReactJS: How to have multiple SPA's on the same website
- How to use React for individual site components
- passing a Url.Action to a react JSX file?
- How to enable/add anchor tag to the string in ReactJS?
- Error "SyntaxError: Cannot use import statement outside a module" when deploying React app with Netlify Functions
- Component not rendering while using route in ReactJS
- Custom React Hook on Ctrl+S keydown
- How to send JSON object to a component in React?
- Input type number not updating value on enter
- CartWidget to show products on hover
- ReactJS - Change CSS Class Properties
- How to show/hide elements on boolean value on react,js
- How prevent duplicated list object in react redux
- React Select options data from ajax
- Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object in ReactJS
- React: Give back the child new inserted element to parent for update the state
- Typeerror : failed to fetch while Uploading file in SharePoint using pnpjs
- How to make pure components in react js using functional approach i.e. non class based
- React function return only one <option>
- Access elements inside element with ref