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

next() function is not working well #104

Open
Neakit opened this issue Jun 25, 2021 · 1 comment
Open

next() function is not working well #104

Neakit opened this issue Jun 25, 2021 · 1 comment

Comments

@Neakit
Copy link

Neakit commented Jun 25, 2021

Getting "page not found" when next called

my router.js

...
import { routeMetaPrepare } from './routeMetaPrepare';

import routesAuth from './group/auth';
....
Vue.use(Router);

const routeList = [
  routesAuth,
  ...
];

for (let i = 0; i < routeList.length; i++) {
  for (let j = 0; j < routeList[i].length; j++) {
     routes.push(routeMetaPrepare(routeList[i][j]));
   }
}

export function createRouter() {
  return new Router({
    mode: 'history',
    routes
  });
}

routeMetaPrepare has logic to check auth state and pass or redirect user

export const ifAuthenticated = (to, from, next) => {
  const { authSuccess } = useAuth();
  if (authSuccess.value) {
    next();
    return;
  }
  next('/signin');
};

export const routeMetaPrepare = (routeInfo) => {
  const meta = routeInfo.meta || {};

  switch (meta.auth) {
    case 'AUTH':
      routeInfo.beforeEnter = ifAuthenticated;
      break;
    default:
      break;
  }

  const children = routeInfo.children || [];
  for (let i = 0; i < children.length; i++) {
    routeInfo.children[i] = routeMetaPrepare(children[i]);
  }

  routeInfo.meta = meta;
  return routeInfo;
};

if a user has no access then next func will be called. But next will redirecting to 404. It seems like there is no signin page.

@Neakit
Copy link
Author

Neakit commented Jun 25, 2021

I'm using nuxt redirect function in 'auth' middleware as a temporary solution.

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

1 participant