Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vuejs - Redirect from login/register to home if already loggedin, Redirect from other pages to login if not loggedin in vue-router

Tags:

I want to redirect user to home page if logged-in and wants to access login/register page and redirect user to login page if not logged-in and wants to access other pages. Some pages are excluded that means there is no need to be logged in so my code is right below:

router.beforeEach((to, from, next) => {     if(         to.name == 'About' ||         to.name == 'ContactUs' ||         to.name == 'Terms'     )     {         next();     }     else     {         axios.get('/already-loggedin')             .then(response => {                 // response.data is true or false                 if(response.data)                 {                     if(to.name == 'Login' || to.name == 'Register')                     {                         next({name: 'Home'});                     }                     else                     {                         next();                     }                 }                 else                 {                     next({name: 'Login'});                 }             })             .catch(error => {                 // console.log(error);             });     } }); 

But the problem is that it gets into an infinite loop and for example each time login page loads and user not logged-in, it redirects user to login page and again to login page and...

How can I fix this?

like image 361
kodfire Avatar asked Oct 04 '18 18:10

kodfire


People also ask

How do I redirect after login in Vue?

Redirect to login from router beforeEach() The requested path ( to. fullPath ) is assigned to the Pinia auth store property auth. returnUrl so the login page can redirect the user back their desired page after successful login. For more information on Vue routing see https://router.vuejs.org/.

How do I redirect one page to another in Vue?

You can use $router. push({ name: "yourroutename"}) or just router. push("yourroutename") now to redirect.

How do I warn a user of unsaved changes before leaving a page in Vue?

1. You can use vue lifecycle method - "beforeDestroy " hook as well and add a popup/dailog box before leaving page. 2 . You can use Vue-router BeforerouteLeave or use watch property on path.

How can I get previous route name in Vuejs?

As an example you could use beforeRouteEnter , an in-component navigation guard, to get the previous route and store it in your data .. ... data() { return { ... prevRoute: null } }, beforeRouteEnter(to, from, next) { next(vm => { vm.


1 Answers

Here's what I'm doing. First I'm using a meta data for the routes, so I don't need to manually put all routes that are not requiring login:

routes: [   {     name: 'About' // + path, component, etc   },   {     name: 'Dashboard', // + path, component, etc     meta: {       requiresAuth: true     }   } ] 

Then, I have a global guard like this:

router.beforeEach((to, from, next) => {   if (to.matched.some(record => record.meta.requiresAuth)) {     // this route requires auth, check if logged in     // if not, redirect to login page.     if (!store.getters.isLoggedIn) {       next({ name: 'Login' })     } else {       next() // go to wherever I'm going     }   } else {     next() // does not require auth, make sure to always call next()!   } }) 

Here I am storing if the user is logged in or not, and not making a new request.

In your example, you have forgotten to include Login into the list of pages that "don't need authentication". So if the user is trying to go to let's say Dashboard, you make the request, turns out he's not logged in. Then he goes to Login, BUT your code checks, sees it's not part of the 3 "auth not required" list, and makes another call :)

Therefore skipping this "list" is crucial! ;)

Good luck!

like image 165
Andrey Popov Avatar answered Sep 18 '22 11:09

Andrey Popov