Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect to previous url after login in nuxt.js

Tags:

vue.js

nuxt.js

I basically want to redirect to the previous url when a user has successfully logged in.

I redirect to the login page with the previous url such as /login?redirect=/page1/page2.

And I want when a user authenticates to be redirected back to that url. I am using the auth-module here: https://auth.nuxtjs.org/

How I login the user.

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

The only thing that I could found in the docs is this: https://auth.nuxtjs.org/getting-started/options#redirect

which however only redirects to a specific page instead of the previous page in the query.

Any ideas on how to achieve this?

like image 243
mauxtin Avatar asked Feb 09 '19 11:02

mauxtin


People also ask

How to disable @nuxtjs/Auth redirect when redirecting to saved path?

Declare the middleware in the nuxt config and disable @nuxtjs/auth redirect option as it will be done in the new middleware. Use the query params for redirect after a successful login to the saved path if exists.

How to redirect to previous page after authentication in Node JS?

To redirect to previous page after authentication in Node.js using Passport.js, we can use our own middleware to do the redirect depending on authentication status.

What happens when you hit a protected page in nuxt?

There is a fairly detailed discussion in github about Nuxt having an issue with a redirect when you are hitting a protected page directly. The redirect goes to the default page redirect rather than the previously hit page.

How do I redirect a page to another page?

If you click the page link that requires authentication and the login page appears, save the path to the previous page in the redirect token and return to the previous page if you successfully log in. This works exactly.


2 Answers

You Can Do This

  this.$router.back()

And its go back to the last route.

Programmatic Navigation | Vue Router https://router.vuejs.org/guide/essentials/navigation.html

Thanks.

like image 105
Birante Avatar answered Oct 08 '22 14:10

Birante


There is a fairly detailed discussion in github about Nuxt having an issue with a redirect when you are hitting a protected page directly. The redirect goes to the default page redirect rather than the previously hit page. The correct behavior should be to store the redirect and then proceed to it after authentication (login) with correct credentials.

3 days ago (Apr 14, 2019), MathiasCiarlo submitted a PR on the auth-module repo to fix this. The base reason why the redirect was "lost" has to do with the state of the redirect value not being allowed to be set as a cookie in SSR mode. His code impacts the storage.js file, in particular the setCookie() method. I've included that changed method here just for reference.

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
  }

I've personally just altered my npm myself, but you could probably fork the repo and use that forked npm for the time being. Or you could wait until the PR is merged into the mainline of the auth-module repo.

like image 34
treejanitor Avatar answered Oct 08 '22 15:10

treejanitor