tuandm / laravue

Admin dashboard for enterprise Laravel applications built by VueJS and Element UI https://laravue.dev
https://laravue.dev
MIT License
2.21k stars 655 forks source link

login shows redirect error in console #282

Open nikulvnnovate opened 3 years ago

nikulvnnovate commented 3 years ago

System logs in successfully but it shows below error in console. anyone has idea how to solve this. thanks

Uncaught (in promise) Error: Redirected when going from "/login" to "/dashboard" via a navigation guard. at createRouterError (vue-router.esm.js?8c4f:2065) at createNavigationRedirectedError (vue-router.esm.js?8c4f:2024) at eval (vue-router.esm.js?8c4f:2371) at _callee$ (permission.js?1827:88) at tryCatch (runtime.js?98b8:63) at Generator.invoke [as _invoke] (runtime.js?98b8:293) at Generator.eval [as next] (runtime.js?98b8:118) at asyncGeneratorStep (asyncToGenerator.js?c973:3) at _next (asyncToGenerator.js?c973:25)

tuandm commented 3 years ago

@nikolaynizruhin have you made any modification on source code, especially permission.js ?

nikolaynizruhin commented 3 years ago

@tuandm You probably meant @nikulvnnovate

nikulvnnovate commented 3 years ago

Yes, I did changes in permission.js I have added forgot-password and reset-password route in whitelist

tuandm commented 3 years ago

@nikulvnnovate Can you please post your code here so everyone can check it? Since you made some changes, the new code may not work as expected.

tuandm commented 3 years ago

@nikolaynizruhin Sorry, I was careless.

nikulvnnovate commented 3 years ago

this is my code for permission.js

import router from './router'; import store from './store'; import { Message } from 'element-ui'; import NProgress from 'nprogress'; // progress bar import 'nprogress/nprogress.css'; // progress bar style import { isLogged } from '@/utils/auth'; import getPageTitle from '@/utils/get-page-title';

NProgress.configure({ showSpinner: false }); // NProgress Configuration

const whiteList = ['/login', '/forgot-password', '/password/reset/:token', '/auth-redirect']; // no redirect whitelist

router.beforeEach(async(to, from, next) => { // start progress bar NProgress.start(); // set page title document.title = getPageTitle(to.meta.title);

// determine whether the user has logged in const isUserLogged = isLogged();

if (isUserLogged) { if (to.path === '/login' || to.path === '/forgot-password' || to.path === '/password/reset/:token') { // if is logged in, redirect to the home page next({ path: '/' }); NProgress.done(); } else { // determine whether the user has obtained his permission roles through getInfo const hasRoles = store.getters.roles && store.getters.roles.length > 0; if (hasRoles) { next(); } else { try { // get user info // note: roles must be a object array! such as: ['admin'] or ,['manager','editor'] const { roles, permissions } = await store.dispatch('user/getInfo');

      // generate accessible routes map based on roles
      const accessRoutes = await store.dispatch('permission/generateRoutes', { roles, permissions });

      accessRoutes.forEach(element => router.addRoute(element));

      next({ ...to, replace: true });
    } catch (error) {
      // remove token and go to login page to re-login
      await store.dispatch('user/resetToken');
      Message.error(error.message || 'Has Error');
      next(`/login`);
      NProgress.done();
    }
  }
}

} else { / has no token/

if (whiteList.indexOf(to.matched[0] ? to.matched[0].path : '') !== -1) {
  // in the free login whitelist, go directly
  next();
} else {
  // other pages that do not have permission to access are redirected to the login page.
  next(`/login`);
  NProgress.done();
}

} });

router.afterEach(() => { // finish progress bar NProgress.done(); });

I have fixed this issue by redirecting user with router-link after login but is this proper solution? thanks