App.vue 6.9 KB

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