Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

login shows redirect error in console #282

Open
nikulvnnovate opened this issue Feb 23, 2021 · 6 comments
Open

login shows redirect error in console #282

nikulvnnovate opened this issue Feb 23, 2021 · 6 comments

Comments

@nikulvnnovate
Copy link

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)

@nikulvnnovate nikulvnnovate changed the title login shows redirect issue in console login shows redirect error in console Feb 23, 2021
@tuandm
Copy link
Owner

tuandm commented Feb 24, 2021

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

@nikolaynizruhin
Copy link
Contributor

@tuandm You probably meant @nikulvnnovate

@nikulvnnovate
Copy link
Author

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

@tuandm
Copy link
Owner

tuandm commented Mar 1, 2021

@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
Copy link
Owner

tuandm commented Mar 1, 2021

@nikolaynizruhin Sorry, I was careless.

@nikulvnnovate
Copy link
Author

nikulvnnovate commented Mar 2, 2021

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants