main.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. import Vue from "vue";
  2. import VueRouter from "vue-router";
  3. import store from "./store";
  4. import App from "./App.vue";
  5. import auth from "./auth";
  6. import io from "./io";
  7. Vue.use(VueRouter);
  8. const router = new VueRouter({
  9. mode: "history",
  10. routes: [
  11. {
  12. path: "/",
  13. component: () => import("./components/pages/Home.vue")
  14. },
  15. {
  16. path: "*",
  17. component: () => import("./components/404.vue")
  18. },
  19. {
  20. path: "/404",
  21. component: () => import("./components/404.vue")
  22. },
  23. {
  24. path: "/terms",
  25. component: () => import("./components/pages/Terms.vue")
  26. },
  27. {
  28. path: "/privacy",
  29. component: () => import("./components/pages/Privacy.vue")
  30. },
  31. {
  32. path: "/team",
  33. component: () => import("./components/pages/Team.vue")
  34. },
  35. {
  36. path: "/news",
  37. component: () => import("./components/pages/News.vue")
  38. },
  39. {
  40. path: "/about",
  41. component: () => import("./components/pages/About.vue")
  42. },
  43. {
  44. name: "profile",
  45. path: "/u/:username",
  46. component: () => import("./components/User/Show.vue")
  47. },
  48. {
  49. path: "/settings",
  50. component: () => import("./components/User/Settings.vue"),
  51. loginRequired: true
  52. },
  53. {
  54. path: "/reset_password",
  55. component: () => import("./components/User/ResetPassword.vue")
  56. },
  57. {
  58. path: "/login",
  59. component: () => import("./components/Modals/Login.vue")
  60. },
  61. {
  62. path: "/register",
  63. component: () => import("./components/Modals/Register.vue")
  64. },
  65. {
  66. path: "/admin",
  67. component: () => import("./components/pages/Admin.vue"),
  68. adminRequired: true
  69. },
  70. {
  71. path: "/admin/:page",
  72. component: () => import("./components/pages/Admin.vue"),
  73. adminRequired: true
  74. },
  75. {
  76. name: "official",
  77. path: "/official/:id",
  78. alias: "/:id",
  79. component: () => import("./components/Station/Station.vue"),
  80. officialRequired: true
  81. },
  82. {
  83. name: "community",
  84. path: "/community/:id",
  85. component: () => import("./components/Station/Station.vue"),
  86. communityRequired: true
  87. }
  88. ]
  89. });
  90. lofig.folder = "../config/default.json";
  91. lofig.get("serverDomain", res => {
  92. io.init(res);
  93. io.getSocket(socket => {
  94. socket.on("ready", (status, role, username, userId) => {
  95. auth.data(status, role, username, userId);
  96. });
  97. socket.on("keep.event:banned", ban => {
  98. auth.setBanned(ban);
  99. });
  100. });
  101. });
  102. router.beforeEach((to, from, next) => {
  103. if (window.stationInterval) {
  104. clearInterval(window.stationInterval);
  105. window.stationInterval = 0;
  106. }
  107. if (window.socket) io.removeAllListeners();
  108. io.clear();
  109. if (to.loginRequired || to.adminRequired) {
  110. auth.getStatus((authenticated, role) => {
  111. if (to.loginRequired && !authenticated) next({ path: "/login" });
  112. else if (to.adminRequired && role !== "admin") next({ path: "/" });
  113. else next();
  114. });
  115. } else next();
  116. if (from.name === "community" || from.name === "official") {
  117. document.title = "Musare";
  118. }
  119. if (to.officialRequired) {
  120. io.getSocket(socket => {
  121. socket.emit("stations.findByName", to.params.id, res => {
  122. if (res.status === "success") {
  123. if (res.data.type === "community")
  124. next({ path: `/community/${to.params.id}` });
  125. else next();
  126. }
  127. });
  128. });
  129. }
  130. if (to.communityRequired) {
  131. io.getSocket(socket => {
  132. socket.emit("stations.findByName", to.params.id, res => {
  133. if (res.status === "success") {
  134. if (res.data.type === "official")
  135. next({ path: `/official/${to.params.id}` });
  136. else next();
  137. }
  138. });
  139. });
  140. }
  141. });
  142. router.afterEach(to => {
  143. ga("set", "page", to.path);
  144. ga("send", "pageview");
  145. });
  146. // eslint-disable-next-line no-new
  147. new Vue({
  148. router,
  149. store,
  150. el: "#root",
  151. render: wrapper => wrapper(App)
  152. });