简体   繁体   English

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

[英]How to make Nuxt-auth and Nuxt-i18n to be friends

I want to use nuxt@auth module and nuxt-i18n together.我想一起使用 nuxt@auth 模块和 nuxt-i18n。 The problem appears when I want to have different routes for login page.当我想为登录页面设置不同的路由时,就会出现问题。 For example:例如:

pages: {
 login: {
  en: '/authorization',
  fr: '/autorisation'
 }
}

Routes are working well, but when I try to use nuxt@auth with all page restricted, the default redirect asks for /login page.路由运行良好,但是当我尝试在所有页面受限的情况下使用 nuxt@auth 时,默认重定向会要求 /login 页面。 In nuxt.config.js file I can rewrite redirect route, but I can set only one redirect.在 nuxt.config.js 文件中,我可以重写重定向路由,但我只能设置一个重定向。

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

How can I show to auth module to ask for route of selected language?如何向 auth 模块显示以询问所选语言的路径? Thanks in advance!提前致谢!

Hope it'll be helpful for somebody =)希望它对某人有帮助=)

export default ({ app, $auth }) => {
 $auth.onRedirect((to, from) => {
   return app.localePath(to)
  })
}

It seems that there was a solution for that problem https://github.com/nuxt-community/auth-module/pull/185 , but I can't access to onRedirect method in the current release.似乎该问题有一个解决方案https://github.com/nuxt-community/auth-module/pull/185 ,但我无法访问当前版本中的onRedirect方法。

I did a workaround.我做了一个解决方法。 I added auth-lang-redirect.js plugin, which overrides redirect option defined in the nuxt.config.js file.我添加了auth-lang-redirect.js插件,它覆盖了nuxt.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
}

Notice that I don't use nuxt-i18n module, but you should get the point.请注意,我没有使用nuxt-i18n模块,但您应该明白这一点。 You have to register this plugin in nuxt.config.js like this:你必须像这样在nuxt.config.js注册这个插件:

auth: {
    strategies: { ... },
    redirect: {
      login: '/login',
      logout: '/',
      callback: '/login',
      home: '/user/profile'
    },
    plugins: ['@/plugins/auth-lang-redirect.js']
  },
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();
  };
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM