Comments (8)
@37108 I had the same problem, the issue ended up being that I was using the latest 5.x version of history with react-router 5.
Try using history ^4.10.1
"history": "^4.10.1",
"react-dom": "^16.13.1",
"react-router-dom": "^5.2.0",
from auth0-react.
@adamjmcgrath thanks for a quick response.
Passing a pathname as a string to history.replace will replace the whole relative url, including removing the url's query params.
I'll check replace behavior and test my application again.
This is probably my problem.
I can't show whole of code because the repository is private. So, I show my environment and related code.
I expect like below, but browser shows like /secure?code=xxx&state=xxxx
.
- anonymous user access to
/secure
- redirect to Universal Login
- success authentication and redirect buck to
/secure
- browser only shows
/secure
environment
- "history": "^5.0.0",
- "react": "^16.13.1",
- "react-dom": "^16.13.1",
- "react-router-dom": "^5.2.0",
// protected.tsx
import { withAuthenticationRequired } from '@auth0/auth0-react';
import React from 'react';
import { Route } from 'react-router-dom';
// eslint-disable-next-line
export function ProtectedRoute({ component, ...args }) {
return (
<Route component={withAuthenticationRequired(component, {})} {...args} />
);
}
// src/index.tsx
import React from 'react';
import ReactDOM from 'react-dom';
import { Auth0Provider, useAuth0 } from '@auth0/auth0-react';
import { createBrowserHistory } from 'history';
import { Route, Router, Switch } from 'react-router-dom';
import { ProtectedRoute } from './protected';
import { Header } from './components/header';
import { Secure } from './secure';
import { Welcome } from './welcome';
export const history = createBrowserHistory();
// working not exptected
// const onRedirectCallback = async appState => {
// history.replace(pathname: appState?.returnTo || window.location.pathname);
// };
// working well if I write this
const onRedirectCallback = async appState => {
history.replace({
pathname: appState?.returnTo || window.location.pathname,
search: '',
});
};
function App() {
const { isLoading } = useAuth0();
if (isLoading) {
return <p></p>;
}
return (
<Router history={history}>
<div className="w-screen h-screen">
<Header />
<Switch>
<Route path="/" exact component={Welcome} />
<ProtectedRoute path="/secure" component={Secure} />
</Switch>
</div>
</Router>
);
}
ReactDOM.render(
<Auth0Provider
domain="DOMAIN"
clientId="CLIENT_ID"
redirectUri={window.location.origin}
onRedirectCallback={onRedirectCallback}>
<App />
</Auth0Provider>,
document.querySelector('#root')
);
from auth0-react.
np, thanks @37108 - will close. But please feel free to reopen if you have any other questions
from auth0-react.
Hi @37108 - thanks for raising this
Passing a pathname as a string to history.replace
will replace the whole relative url, including removing the url's query params. I've just verified this on our react-router
example app (https://github.com/auth0/auth0-react/tree/master/examples/cra-react-router) which uses the same logic.
Do you have an example I can look at where this is not working as expected?
from auth0-react.
// history.replace(pathname: appState?.returnTo || window.location.pathname);
history.replace(appState?.returnTo || window.location.pathname);
should work - let me know if it doesn't
from auth0-react.
should work - let me know if it doesn't
I tried this code, but did not work.
If there is no problem router, I think Webpack or Webpack Devserver are not working well...
@adamjmcgrath
Thanks for helping a lot! maybe this is not problem about examples environment, about my environment.
If you don't need to action, please close this issue.
from auth0-react.
I'm just commenting for anyone that is running into the same issue and is using react-router-6 and history v5. The solution to remove the code and state parameters from the browser location bar is to use the navigate
API instead of the history
API. This was mentioned in the react-router-6 migrating docs here.
from auth0-react.
I had the problem of the url parameters being replaced, even after they were cleared.
This solution worked. It forces a page reload and the code
and state
url parameters cannot be reset as they were being before.
history.replace(
appState && appState?.returnTo
? appState?.returnTo
: window.location.pathname
);
// code and state url parameters are reset unless page reload is triggered
history.go(0);
};
from auth0-react.
Related Issues (20)
- loginWithRedirect behaving different between dev and prod with nextjs HOT 8
- Unable to handle errors when POST call to /oauth/token fails, when the call is made by @auth0/auth0-react SDK HOT 13
- Can not get the access token on Firefox browser HOT 6
- checkJwt middleware doesn't work on API example HOT 2
- Support using existing Auth0Client to improve performance HOT 2
- Add warning when calling `useAuth0` outside of `Auth0Provider` HOT 1
- Use '.well-known/openid-configuration' to configure Auth0Provider (Auth0Client) calls HOT 1
- Chrome Web Store Violation: Having obfuscated code in the package HOT 3
- Add Support to Instantiate the Client Outside And Pass as a Prop (mainly for Redux middleware) HOT 1
- login_hint is not working if set using global state or session storage. HOT 4
- Auth0Provider does not accept CustomContext made from CustomUser
- ReferenceError: window is not defined with next.js app HOT 2
- Allow user to instanciate the auth0 client themselves and pass it to the provider HOT 1
- Uncaught TypeError: l.current is null when using react-dom 18.3.1 HOT 2
- CacheKey clientId field type does not match string | undefined HOT 2
- Calling getAccessTokenSilently after async code doesn't update token details HOT 5
- Auth0provider not compatible with useNavigate hook HOT 1
- Missing Parameters in AccessToken with CacheMode "off" and Refresh Token Rotation Enabled HOT 3
- Facing Issue while Use getAccessTokenSilently
- access_type offline implementation HOT 17
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from auth0-react.