'Nuxt : Check route is redirected from other routes or directly load?

I'm face up with a problem when login.

If user is authenticated, I will let them redirect to home page.

And if they come from another protected route (homepage, other routes) and after logined, I want it redirect to exactly the route they came from.

My solution is:

  • In auth.js middleware, I will save to cookie the route they come from like this

    app.$cookie.set("historyRoute", app.router.history.current.path)
    
  • In login.vue I will check if exist historyRoute to let them redirect then remove historyRoute from cookie.

But problem is that user will come from other protected routes and they do not take login action at that time, they make refreshing Login page many times and historyRoute will still exist in cookie.

So I want to know how to determine when visitors directly reload Login page to remove historyRoute in cookie.

Or if you have another solution, please suggest me know. Thanks.

Update flow:

Visitor access link /users/setting

-> check Auth
-> if Not Auth 
   -> redirect 302 to /Login
   -> After login (I need to know they directly come from login page or from /user/setting then redirect to login page)
   -> redirect back to users/setting


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source