index.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import { defineRouter } from "#q-app/wrappers";
  2. import {
  3. createRouter,
  4. createMemoryHistory,
  5. createWebHistory,
  6. createWebHashHistory,
  7. } from "vue-router";
  8. import routes from "./routes";
  9. import { Notify } from "quasar";
  10. import { permissionStore } from "src/stores/permission";
  11. import { i18n } from "src/boot/i18n";
  12. import { userStore } from "src/stores/user";
  13. import { useAuth } from "src/composables/useAuth";
  14. import { Capacitor } from "@capacitor/core";
  15. import { App as CapacitorApp } from "@capacitor/app";
  16. /*
  17. * If not building with SSR mode, you can
  18. * directly export the Router instantiation;
  19. *
  20. * The function below can be async too; either use
  21. * async/await or return a Promise which resolves
  22. * with the Router instance.
  23. */
  24. // Telas acessíveis sem autenticação (login, recuperação de senha e primeiro acesso).
  25. const PUBLIC_ROUTES = [
  26. "LoginPage",
  27. "ForgotPasswordPage",
  28. "VerifyEmailPage",
  29. "VerifyCodePage",
  30. "ResetPasswordPage",
  31. "FirstAccessPage",
  32. "FirstAccessFormPage",
  33. ];
  34. export default defineRouter(function (/* { store, ssrContext } */) {
  35. const createHistory = process.env.SERVER
  36. ? createMemoryHistory
  37. : process.env.VUE_ROUTER_MODE === "history"
  38. ? createWebHistory
  39. : createWebHashHistory;
  40. const Router = createRouter({
  41. scrollBehavior: () => ({ left: 0, top: 0 }),
  42. routes,
  43. // Leave this as is and make changes in quasar.conf.js instead!
  44. // quasar.conf.js -> build -> vueRouterMode
  45. // quasar.conf.js -> build -> publicPath
  46. history: createHistory(process.env.VUE_ROUTER_BASE),
  47. });
  48. let refreshed = false;
  49. Router.beforeEach(async (to, from, next) => {
  50. if (userStore().accessToken == null && !refreshed) {
  51. try {
  52. await useAuth().refresh();
  53. } catch {
  54. refreshed = true;
  55. if (!PUBLIC_ROUTES.includes(to.name)) {
  56. return next({ name: "LoginPage" });
  57. }
  58. return next();
  59. }
  60. }
  61. if (userStore().accessToken) {
  62. if (PUBLIC_ROUTES.includes(to.name)) {
  63. return next({ name: "CarteirinhaPage" });
  64. }
  65. }
  66. if (to.meta.requiredPermission) {
  67. const { getAccess } = permissionStore();
  68. const permission = getAccess(to.meta.requiredPermission, "view");
  69. if (!permission) {
  70. Notify.create({
  71. message: i18n.global.t("validation.permissions.view"),
  72. type: "negative",
  73. });
  74. return next(from);
  75. }
  76. }
  77. return next();
  78. });
  79. if (Capacitor.isNativePlatform()) {
  80. CapacitorApp.addListener("backButton", ({ canGoBack }) => {
  81. if (!canGoBack) {
  82. CapacitorApp.exitApp();
  83. } else {
  84. Router.go(-1);
  85. }
  86. });
  87. }
  88. return Router;
  89. });