App.vue 6.8 KB

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