'React-router HashRouter When redirecting root URL with query parameters, route gets appended after params

I'm having the following issue. I am implementing SSO which, after successful sign in, redirects the user to the root uri with the access_token as a query param https://example.com/?token=access_token_jwt

On the root Route, if this token is present, it is verified over api and stored in sessionStorage. After the validation, a redirect is done to "/dashboard". Yet, the redirected url is https://example.com/?token=access_token_jwt/#/dashboard (I'm using HashRouter and is key for the application)

How can I make sure the query params are cleared before the redirect?

I've searched for a long time but haven't found even anyone experiencing this issue.

Thanks for helping!



Solution 1:[1]

You need to force browser to clean-up the URL.

I fixed this by doing a quick check for URL format in my App.tsx file:

if (window.location.pathname !== '/') {
    window.location.pathname = '/'
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 watr