-
Notifications
You must be signed in to change notification settings - Fork 69
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
withAuthenticationRequired
doesn't redirect to previous route after successful login
#1324
Comments
You will need to pass |
Can you be more specific, @pamapa ? const onSigninCallback = (_user: User | void): void => {
window.history.replaceState(
{},
document.title,
window.location.pathname
)
} copied from your comments in AuthProvider.tsx . createRoot(document.getElementById("root")!).render(
<AuthProvider {...oidcConfig} onSigninCallback={onSigninCallback}>
<RelayEnvironmentProvider environment={RelayEnvironment}>
<React.Suspense>
<React.StrictMode>
<RouterProvider router={router}/>
</React.StrictMode>
</React.Suspense>
</RelayEnvironmentProvider>
</AuthProvider>
); import {withAuthenticationRequired} from 'react-oidc-context';
export const AuthGuard = ({component}) => {
const Component = withAuthenticationRequired(component);
return <Component/>;
} {path: 'create-ad', element: <AuthGuard component={PostCreate}></AuthGuard>}, I think this is a pretty common issue, shouldn't it work like that out of the box? Ok, so I modified the AuthGuard import {withAuthenticationRequired} from 'react-oidc-context';
export const AuthGuard = ({component}) => {
const Component = withAuthenticationRequired(component, {signinRedirectArgs: { redirect_uri: window.location.href }});
return <Component/>;
} But I had to explicitly add the Well I guess that's solved. |
In react-oidc-context/src/withAuthenticationRequired.tsx Lines 16 to 19 in 4d3afe2
After the authentication process is finished |
import {withAuthenticationRequired} from 'react-oidc-context';
export const AuthGuard = ({component}) => {
const Component = withAuthenticationRequired(
component, {
onBeforeSignin: () => {
sessionStorage.setItem('pp', window.location.pathname + window.location.search + window.location.hash)
}
}
);
return <Component/>;
} const onSigninCallback = (_user: User | void): void => {
const pp = sessionStorage.getItem('pp')
if (pp) {
sessionStorage.removeItem('pp')
window.location.replace(pp)
} else {
window.history.replaceState(
{},
document.title,
window.location.pathname
)
}
} |
According to this component's documentation:
"When you wrap your components in this higher-order component and an anonymous user visits your component, they will be redirected to the login page; after logging in, they will return to the page from which they were redirected."
When trying to access a protected route with no authentication, i'm being redirected to the log-in page as expected.
However, after successful log-in, the user is not redirected to the previous route, but to the homepage (just as a normal log-in).
Using
signinRedirectArgs: { redirect_uri: window.location.href }
partially resolved the issue, user is redirected back to the route he came from, but this adds some unwanted side effects (query params are added to the url).Current behaviour:
navigate to domain.com/settings -> Not authenticated -> redirect to log-in -> credentials -> redirect to homepage
Expected behaviour:
navigate to domain.com/settings -> Not authenticated -> redirect to log-in -> credentials -> redirect to domain.com/settings
Shouldn't redirect to previous route work out of the box for
withAuthenticationRequired
?The text was updated successfully, but these errors were encountered: