Javascript 如何让Nuxt auth和Nuxt-i18n成为朋友

Javascript 如何让Nuxt auth和Nuxt-i18n成为朋友,javascript,authentication,vue.js,internationalization,nuxt.js,Javascript,Authentication,Vue.js,Internationalization,Nuxt.js,我想用nuxt@auth模块和nuxt-i18n连接在一起。当我想为登录页面设置不同的路由时,就会出现问题。例如: pages: { login: { en: '/authorization', fr: '/autorisation' } } 路线运行良好,但当我尝试使用nuxt@auth在所有页面受限的情况下,默认的重定向请求/login页面。在nuxt.config.js文件中,我可以重写重定向路由,但我只能设置一个重定向 auth: { redirect: { logi

我想用nuxt@auth模块和nuxt-i18n连接在一起。当我想为登录页面设置不同的路由时,就会出现问题。例如:

pages: {
 login: {
  en: '/authorization',
  fr: '/autorisation'
 }
}
路线运行良好,但当我尝试使用nuxt@auth在所有页面受限的情况下,默认的重定向请求/login页面。在nuxt.config.js文件中,我可以重写重定向路由,但我只能设置一个重定向

auth: {
 redirect: {
  login: '/fr/autorisation'
 }
}

我如何向auth模块显示所选语言的路由?提前谢谢

这个问题似乎有解决方案,但在当前版本中,我无法访问
onRedirect
方法

我做了一个变通办法。我添加了
auth lang redirect.js
插件,它覆盖了
numxt.config.js
文件中定义的
redirect
选项

export default ({ app }) => {
  var redirect = app.$auth.$storage.options.redirect
  for (var key in redirect) {
    redirect[key] = '/' + app.i18n.locale + redirect[key]
  }
  app.$auth.$storage.options.redirect = redirect
}
请注意,我没有使用
nuxt-i18n
模块,但您应该明白这一点。 您必须在
numxt.config.js
中注册此插件,如下所示:

auth: {
    strategies: { ... },
    redirect: {
      login: '/login',
      logout: '/',
      callback: '/login',
      home: '/user/profile'
    },
    plugins: ['@/plugins/auth-lang-redirect.js']
  },

希望对某人有帮助=)


谢谢,我已经解决了这个问题,我的解决方案没有,但是做了同样的事情。这是正确的解决方案。除了默认值(默认语言没有前缀)之外,它尊重前缀,并且可以完美地处理单页行为(切换语言->登录),这是插件还是中间件?@Rozkalns它就是插件
export default ({ app, $auth }) => {
 $auth.onRedirect((to, from) => {
   return app.localePath(to)
  })
}
export default function({ app, $auth }) {
  const redirect = { ...$auth.$storage.options.redirect };

  const localizeRedirects = () => {
    for (const key in redirect) {
      $auth.$storage.options.redirect[key] = app.localePath(redirect[key]);
    }
  };

  localizeRedirects();

  app.i18n.onLanguageSwitched = () => {
    localizeRedirects();
  };
}