index.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import { route } from "quasar/wrappers";
  2. import {
  3. createRouter,
  4. createMemoryHistory,
  5. createWebHistory,
  6. createWebHashHistory,
  7. } from "vue-router";
  8. import routes from "./routes";
  9. import { Cookies } from "quasar";
  10. /*
  11. * If not building with SSR mode, you can
  12. * directly export the Router instantiation;
  13. *
  14. * The function below can be async too; either use
  15. * async/await or return a Promise which resolves
  16. * with the Router instance.
  17. */
  18. export default route(function (/* { store, ssrContext } */) {
  19. const createHistory = process.env.SERVER
  20. ? createMemoryHistory
  21. : process.env.VUE_ROUTER_MODE === "history"
  22. ? createWebHistory
  23. : createWebHashHistory;
  24. const Router = createRouter({
  25. scrollBehavior: () => ({ left: 0, top: 0 }),
  26. routes,
  27. // Leave this as is and make changes in quasar.conf.js instead!
  28. // quasar.conf.js -> build -> vueRouterMode
  29. // quasar.conf.js -> build -> publicPath
  30. history: createHistory(process.env.VUE_ROUTER_BASE),
  31. });
  32. Router.beforeEach(async (to, from, next) => {
  33. const access_token = Cookies.get("access_token");
  34. if (to.meta.requireAuth && !access_token) {
  35. return next({ name: "Login" });
  36. }
  37. return next();
  38. });
  39. return Router;
  40. });