App.vue 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. <template>
  2. <div>
  3. <banned v-if="banned"></banned>
  4. <div v-else>
  5. <h1 v-if="!socketConnected" class="alert">Could not connect to the server.</h1>
  6. <!-- should be a persistant toast -->
  7. <router-view></router-view>
  8. <toast></toast>
  9. <what-is-new></what-is-new>
  10. <mobile-alert></mobile-alert>
  11. <login-modal v-if="modals.header.login"></login-modal>
  12. <register-modal v-if="modals.header.register"></register-modal>
  13. </div>
  14. </div>
  15. </template>
  16. <script>
  17. import { mapState, mapActions } from "vuex";
  18. import { Toast } from "vue-roaster";
  19. import Banned from "./components/pages/Banned.vue";
  20. import WhatIsNew from "./components/Modals/WhatIsNew.vue";
  21. import MobileAlert from "./components/Modals/MobileAlert.vue";
  22. import LoginModal from "./components/Modals/Login.vue";
  23. import RegisterModal from "./components/Modals/Register.vue";
  24. import auth from "./auth";
  25. import io from "./io";
  26. import validation from "./validation";
  27. export default {
  28. replace: false,
  29. data() {
  30. return {
  31. banned: false,
  32. ban: {},
  33. register: {
  34. email: "",
  35. username: "",
  36. password: ""
  37. },
  38. login: {
  39. email: "",
  40. password: ""
  41. },
  42. loggedIn: false,
  43. role: "",
  44. username: "",
  45. userId: "",
  46. serverDomain: "",
  47. socketConnected: true,
  48. userIdMap: {},
  49. currentlyGettingUsernameFrom: {}
  50. };
  51. },
  52. computed: mapState({
  53. modals: state => state.modals.modals,
  54. currentlyActive: state => state.modals.currentlyActive
  55. }),
  56. methods: {
  57. logout: function() {
  58. let _this = this;
  59. _this.socket.emit("users.logout", result => {
  60. if (result.status === "success") {
  61. document.cookie = "SID=;expires=Thu, 01 Jan 1970 00:00:01 GMT;";
  62. location.reload();
  63. } else Toast.methods.addToast(result.message, 4000);
  64. });
  65. },
  66. submitOnEnter: (cb, event) => {
  67. if (event.which == 13) cb();
  68. },
  69. getUsernameFromId: function(userId) {
  70. // refactor
  71. if (
  72. typeof this.userIdMap[userId] !== "string" &&
  73. !this.currentlyGettingUsernameFrom[userId]
  74. ) {
  75. this.currentlyGettingUsernameFrom[userId] = true;
  76. io.getSocket(socket => {
  77. socket.emit("users.getUsernameFromId", userId, res => {
  78. if (res.status === "success") {
  79. this.$set(this.userIdMap, `Z${userId}`, res.data);
  80. }
  81. this.currentlyGettingUsernameFrom[userId] = false;
  82. });
  83. });
  84. }
  85. },
  86. ...mapActions("modals", ["closeCurrentModal"])
  87. },
  88. mounted: function() {
  89. document.onkeydown = event => {
  90. event = event || window.event;
  91. if (
  92. event.keyCode === 27 &&
  93. Object.keys(this.currentlyActive).length !== 0
  94. )
  95. this.closeCurrentModal();
  96. };
  97. let _this = this;
  98. if (localStorage.getItem("github_redirect")) {
  99. this.$router.go(localStorage.getItem("github_redirect"));
  100. localStorage.removeItem("github_redirect");
  101. }
  102. auth.isBanned((banned, ban) => {
  103. _this.ban = ban;
  104. _this.banned = banned;
  105. });
  106. auth.getStatus((authenticated, role, username, userId) => {
  107. _this.socket = window.socket;
  108. _this.loggedIn = authenticated;
  109. _this.role = role;
  110. _this.username = username;
  111. _this.userId = userId;
  112. });
  113. io.onConnect(true, () => {
  114. _this.socketConnected = true;
  115. });
  116. io.onConnectError(true, () => {
  117. _this.socketConnected = false;
  118. });
  119. io.onDisconnect(true, () => {
  120. _this.socketConnected = false;
  121. });
  122. lofig.get("serverDomain", res => {
  123. _this.serverDomain = res;
  124. });
  125. if (_this.$route.query.err) {
  126. let err = _this.$route.query.err;
  127. err = err
  128. .replace(new RegExp("<", "g"), "&lt;")
  129. .replace(new RegExp(">", "g"), "&gt;");
  130. Toast.methods.addToast(err, 20000);
  131. }
  132. io.getSocket(true, socket => {
  133. socket.on("keep.event:user.session.removed", () => {
  134. location.reload();
  135. });
  136. });
  137. },
  138. events: {
  139. register: function(recaptchaId) {
  140. let {
  141. register: { email, username, password }
  142. } = this;
  143. let _this = this;
  144. if (!email || !username || !password)
  145. return Toast.methods.addToast("Please fill in all fields", 8000);
  146. if (!validation.isLength(email, 3, 254))
  147. return Toast.methods.addToast(
  148. "Email must have between 3 and 254 characters.",
  149. 8000
  150. );
  151. if (
  152. email.indexOf("@") !== email.lastIndexOf("@") ||
  153. !validation.regex.emailSimple.test(email)
  154. )
  155. return Toast.methods.addToast("Invalid email format.", 8000);
  156. if (!validation.isLength(username, 2, 32))
  157. return Toast.methods.addToast(
  158. "Username must have between 2 and 32 characters.",
  159. 8000
  160. );
  161. if (!validation.regex.azAZ09_.test(username))
  162. return Toast.methods.addToast(
  163. "Invalid username format. Allowed characters: a-z, A-Z, 0-9 and _.",
  164. 8000
  165. );
  166. if (!validation.isLength(password, 6, 200))
  167. return Toast.methods.addToast(
  168. "Password must have between 6 and 200 characters.",
  169. 8000
  170. );
  171. if (!validation.regex.password.test(password))
  172. return Toast.methods.addToast(
  173. "Invalid password format. Must have one lowercase letter, one uppercase letter, one number and one special character.",
  174. 8000
  175. );
  176. this.socket.emit(
  177. "users.register",
  178. username,
  179. email,
  180. password,
  181. grecaptcha.getResponse(recaptchaId),
  182. result => {
  183. if (result.status === "success") {
  184. Toast.methods.addToast(`You have successfully registered.`, 4000);
  185. if (result.SID) {
  186. lofig.get("cookie", cookie => {
  187. let date = new Date();
  188. date.setTime(
  189. new Date().getTime() + 2 * 365 * 24 * 60 * 60 * 1000
  190. );
  191. let secure = cookie.secure ? "secure=true; " : "";
  192. document.cookie = `SID=${
  193. result.SID
  194. }; expires=${date.toGMTString()}; domain=${
  195. cookie.domain
  196. }; ${secure}path=/`;
  197. location.reload();
  198. });
  199. } else _this.$router.go("/login");
  200. } else Toast.methods.addToast(result.message, 8000);
  201. }
  202. );
  203. },
  204. login: function() {
  205. let {
  206. login: { email, password }
  207. } = this;
  208. let _this = this;
  209. this.socket.emit("users.login", email, password, result => {
  210. if (result.status === "success") {
  211. lofig.get("cookie", cookie => {
  212. let date = new Date();
  213. date.setTime(new Date().getTime() + 2 * 365 * 24 * 60 * 60 * 1000);
  214. let secure = cookie.secure ? "secure=true; " : "";
  215. let domain = "";
  216. if (cookie.domain !== "localhost")
  217. domain = ` domain=${cookie.domain};`;
  218. document.cookie = `SID=${
  219. result.SID
  220. }; expires=${date.toGMTString()}; ${domain}${secure}path=/`;
  221. Toast.methods.addToast(
  222. `You have been successfully logged in`,
  223. 2000
  224. );
  225. location.reload();
  226. });
  227. } else Toast.methods.addToast(result.message, 2000);
  228. });
  229. }
  230. },
  231. components: {
  232. Toast,
  233. WhatIsNew,
  234. MobileAlert,
  235. LoginModal,
  236. RegisterModal,
  237. Banned
  238. }
  239. };
  240. </script>
  241. <style lang='scss'>
  242. .center {
  243. text-align: center;
  244. }
  245. #toast-container {
  246. z-index: 10000 !important;
  247. }
  248. html {
  249. overflow: auto !important;
  250. }
  251. .modal-card {
  252. margin: 0 !important;
  253. }
  254. .absolute-a {
  255. width: 100%;
  256. height: 100%;
  257. position: absolute;
  258. top: 0;
  259. left: 0;
  260. }
  261. .alert {
  262. padding: 20px;
  263. color: white;
  264. background-color: red;
  265. position: fixed;
  266. top: 50px;
  267. right: 50px;
  268. font-size: 2em;
  269. border-radius: 5px;
  270. z-index: 10000000;
  271. }
  272. .tooltip {
  273. position: relative;
  274. &:after {
  275. position: absolute;
  276. min-width: 80px;
  277. margin-left: -75%;
  278. text-align: center;
  279. padding: 7.5px 6px;
  280. border-radius: 2px;
  281. background-color: #323232;
  282. font-size: 0.9em;
  283. color: #fff;
  284. content: attr(data-tooltip);
  285. opacity: 0;
  286. transition: all 0.2s ease-in-out 0.1s;
  287. visibility: hidden;
  288. }
  289. &:hover:after {
  290. opacity: 1;
  291. visibility: visible;
  292. }
  293. }
  294. .tooltip-top {
  295. &:after {
  296. bottom: 150%;
  297. }
  298. &:hover {
  299. &:after {
  300. bottom: 120%;
  301. }
  302. }
  303. }
  304. .tooltip-bottom {
  305. &:after {
  306. top: 155%;
  307. }
  308. &:hover {
  309. &:after {
  310. top: 125%;
  311. }
  312. }
  313. }
  314. .tooltip-left {
  315. &:after {
  316. bottom: -10px;
  317. right: 130%;
  318. min-width: 100px;
  319. }
  320. &:hover {
  321. &:after {
  322. right: 110%;
  323. }
  324. }
  325. }
  326. .tooltip-right {
  327. &:after {
  328. bottom: -10px;
  329. left: 190%;
  330. min-width: 100px;
  331. }
  332. &:hover {
  333. &:after {
  334. left: 200%;
  335. }
  336. }
  337. }
  338. .button:focus,
  339. .button:active {
  340. border-color: #dbdbdb !important;
  341. }
  342. .input:focus,
  343. .input:active {
  344. border-color: #03a9f4 !important;
  345. }
  346. button.delete:focus {
  347. background-color: rgba(10, 10, 10, 0.3);
  348. }
  349. .tag {
  350. padding-right: 6px !important;
  351. }
  352. .button.is-success {
  353. background-color: #00b16a !important;
  354. }
  355. </style>