Skip to content
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

Locale not included in withPageAuthRequired redirect #353

Closed
davemaier opened this issue Mar 29, 2021 · 4 comments
Closed

Locale not included in withPageAuthRequired redirect #353

davemaier opened this issue Mar 29, 2021 · 4 comments
Labels
bug Something isn't working

Comments

@davemaier
Copy link

Describe the problem

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.

What was the expected behavior?

I expect that after login, the user gets redirected to the page with the locale she was using the website before.

Reproduction

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.

Environment

  • Version of this library used: "@auth0/nextjs-auth0": "^1.2.0"
  • Which framework are you using, if applicable: "next": "10.0.9"
  • Other modules/plugins/libraries that might be involved: "i18next": "^20.1.0"
  • Any other relevant information you think would be useful:

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

@adamjmcgrath
Copy link
Contributor

Thanks for reporting @davemaier - will investigate

@adamjmcgrath adamjmcgrath added the needs investigation This needs to be investigated further before proceeding label Mar 29, 2021
@adamjmcgrath
Copy link
Contributor

adamjmcgrath commented Mar 29, 2021

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

@adamjmcgrath adamjmcgrath added bug Something isn't working and removed needs investigation This needs to be investigated further before proceeding labels Mar 29, 2021
@adamjmcgrath
Copy link
Contributor

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);
};

@adamjmcgrath
Copy link
Contributor

Closing this, feel free to ping me if you'd like me to reopen

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants