main.js 3.5 KB

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