在nuxt.js中登录后重定向到上一个URL

问题描述 投票:1回答:2

我基本上想要在用户成功登录时重定向到上一个URL。

我使用前面的网址(例如/login?redirect=/page1/page2)重定向到登录页面。

我希望当用户进行身份验证时重定向回该URL。我在这里使用auth-modulehttps://auth.nuxtjs.org/

我如何登录用户。

methods: {
    async submit() {
      await this.$auth.loginWith('local', {
        data: this.form
      })
    }
}

我在文档中唯一能找到的是:https://auth.nuxtjs.org/getting-started/options#redirect

然而,它只重定向到特定页面而不是查询中的上一页。

关于如何实现这一点的任何想法?

vue.js nuxt.js nuxt
2个回答
4
投票

你可以这样做

  this.$router.back()

它回到了最后一条路线。

程序化导航| Vue Router https://router.vuejs.org/guide/essentials/navigation.html

谢谢。


0
投票

有一个相当详细的讨论in github关于Nuxt在你直接点击受保护页面时遇到重定向问题。重定向转到默认页面重定向,而不是之前命中的页面。正确的行为应该是存储redirect,然后在使用正确的凭据进行身份验证(登录)后继续执行。

3天前(2019年4月14日),MathiasCiarloPR回购中提交了auth-module以解决此问题。重定向“丢失”的根本原因与SSR模式下不允许将重定向值的状态设置为cookie有关。他的代码影响storage.js文件,特别是setCookie()方法。我在这里包含了这个改变的方法仅供参考。

setCookie (key, value, options = {}) {
    if (!this.options.cookie) {
      return
    }

    const _key = this.options.cookie.prefix + key

    const _options = Object.assign({}, this.options.cookie.options, options)

    if (isUnset(value)) {
      Cookies.remove(_key, _options)
    } else {

      // Support server set cookies
      if (process.server) {
        this.ctx.res.setHeader('Set-Cookie', [_key + '=' + value])
      } else {
        Cookies.set(_key, value, _options)
      }
    }

    return value
  }

我本人只是自己修改了我的npm,但你可能会分叉回购并暂时使用分叉的npm。或者你可以等到PR合并到auth-module回购的主线。

© www.soinside.com 2019 - 2024. All rights reserved.