I'm using internationalized routing and if I wrap a page in the withPageAuthRequired HOC, the locale isn't included in the redirect. This leads to the problem that if a user enters the page, selects a language (or a language is determined based on the browser locale), visits a protected page and fills the login form, she gets redirected to the correct page, but with the default locale.
For example /de/events/ becomes /events after redirect.
I expect that after login, the user gets redirected to the page with the locale she was using the website before.
Setup a website that uses internationalized routing and wrap some page in withPageAuthRequired HOC. Then visit the page with some locale that isn't the default locale. After login, you will be directed to the page with the default locale.
The problem seems to be in with-page-auth-required.tsx where the redirect url is built as follows:
returnTo = `${router.basePath ?? ''}${router.asPath}`.
I think this should be changed to something like this:
returnTo = `${router.basePath ? `${router.basePath}/` : ''}${router.locale}${router.asPath}`
This issue is closely related to #351
Thanks for reporting @davemaier - will investigate
looks like we need to add locale to the CSR side withPageAuthRequired and basePath and locale to the SSR withPageAuthRequired
Since i18n routing can be Domain Routing as well as sub path, prefixing the locale to the path wont work for all cases.
We may have to use window.location.pathname in the CSR one here https://github.com/auth0/nextjs-auth0/blob/main/src/frontend/with-page-auth-required.tsx#L87
I'm not sure what the equivalent is going to be for the SSR one, will raise some work internally to investigate
This has been fixed for CSR withPageAuthRequired in v1.3.1
For SSR withPageAuthRequired, this is non trivial - for now the recommended work around would be to derive the correct return to URL in your application and pass it to withPageAuthRequired at request time, eg something like
export const getServerSideProps = (ctx) => {
return withPageAuthRequired({ returnTo: `${baseUrl}/${locale}${ctx.req.url}` })(ctx);
};
Closing this, feel free to ping me if you'd like me to reopen
Most helpful comment
looks like we need to add
localeto the CSR side withPageAuthRequired andbasePathandlocaleto the SSR withPageAuthRequiredSince i18n routing can be Domain Routing as well as sub path, prefixing the locale to the path wont work for all cases.
We may have to use
window.location.pathnamein the CSR one here https://github.com/auth0/nextjs-auth0/blob/main/src/frontend/with-page-auth-required.tsx#L87I'm not sure what the equivalent is going to be for the SSR one, will raise some work internally to investigate