main.js 3.4 KB

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