index.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import { createRouter, createWebHistory } from "vue-router";
  2. import PaginaInicialView from "../views/pagina-inicial/PaginaInicialView.vue";
  3. import ConversasHistoricoView from "../views/conversas/ConversasHistoricoView.vue";
  4. import ConversasPesquisarView from "../views/conversas/ConversasPesquisarView.vue";
  5. import ConfiguracoesView from "../views/configuracoes/ConfiguracoesView.vue";
  6. import UsuariosView from "../views/usuarios/UsuariosView.vue";
  7. import LoginView from "../views/auth/LoginView.vue";
  8. import { useAuth } from "../composables/useAuth.js";
  9. export const router = createRouter({
  10. history: createWebHistory(import.meta.env.BASE_URL),
  11. routes: [
  12. { path: "/login", name: "login", component: LoginView, meta: { guestOnly: true } },
  13. { path: "/", name: "home", component: PaginaInicialView, meta: { requiresAuth: true } },
  14. { path: "/conversas/historico", name: "conversas-historico", component: ConversasHistoricoView, meta: { requiresAuth: true } },
  15. { path: "/conversas/pesquisar", name: "conversas-pesquisar", component: ConversasPesquisarView, meta: { requiresAuth: true } },
  16. { path: "/configuracoes", name: "configuracoes", component: ConfiguracoesView, meta: { requiresAuth: true } },
  17. { path: "/usuarios", name: "usuarios", component: UsuariosView, meta: { requiresAuth: true } },
  18. { path: "/:pathMatch(.*)*", redirect: "/" }
  19. ]
  20. });
  21. router.beforeEach((to) => {
  22. const { isAuthenticated } = useAuth();
  23. if (to.meta?.guestOnly && isAuthenticated.value) {
  24. return { name: "home" };
  25. }
  26. if (to.meta?.requiresAuth && !isAuthenticated.value) {
  27. return {
  28. name: "login",
  29. query: to.fullPath && to.fullPath !== "/" ? { redirect: to.fullPath } : {}
  30. };
  31. }
  32. return true;
  33. });