nuxt-modules / i18n

I18n module for Nuxt
https://i18n.nuxtjs.org
MIT License
1.69k stars 474 forks source link

Localized Paths reachable from other languages when using differentDomains #389

Open seltsam23 opened 5 years ago

seltsam23 commented 5 years ago

Version

v5.3.0

Reproduction link

http://jsfiddle.com

Steps to reproduce



### What is expected ?

*de subdomain*
http://de.website.com/anmelden -> 200
http://de.website.com/login -> 404

*en subdomain*
http://en.website.com/login -> 200
http://en.website.com/anmelden -> 404

### What is actually happening?

*de subdomain*
http://de.website.com/anmelden -> 200
http://de.website.com/login -> 200 // should be 404

*en subdomain*
http://en.website.com/login -> 200
http://en.website.com/anmelden -> 200  // should be 404

### Additional comments?

It is working as expected when not using custom domains:

*de subdomain*
http://website.com/de/anmelden -> 200
http://website.com/de/login -> 404 // works

*en subdomain*
http://website.com/en/login -> 200
http://website.com/en/anmelden -> 404 // works

<!--cmty--><!--cmty_prevent_hook-->
<div align="right"><sub><em>This bug report is available on <a href="https://cmty.app/nuxt">Nuxt</a> community (<a href="https://cmty.app/nuxt/nuxt-i18n/issues/c281">#c281</a>)</em></sub></div>
stale[bot] commented 4 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] commented 4 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] commented 3 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

nikolashaag commented 2 years ago

I am having the same problem of paths being accessible from other locales.

Is there any known workaround to this problem?

Kapcash commented 2 years ago

One workaroud I can think of is to create a global middleware that checks the current path matches the locale, and if not, redirects to the correct one:

const localePathsRedirectsOnLocalizedDomains: Middleware = ({ route, redirect, app }) => {
  /* get the localized path from the current route path
     if route.path === '/anmelden' but current locale is 'en' -> it will return '/login'
   */
  const expectedPath = app.localePath(route.path)

  // If the current path doesn't match the localized one, then we're not on the right path.
  if (route.path !== expectedPath) {
    // redirect to correct one
    redirect(301, expectedPath)
    // or send 404 page
    error({ statusCode: 404 })
  }
}

export default localePathsRedirectsOnLocalizedDomains
nikolashaag commented 2 years ago

Thanks for the quick response @Kapcash . I came up with a similar solution but was manually checking the routes against the router. I didn't know about app.localePath 🙌 .

Can confirm that above workaround works.