App.vue 8.0 KB

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