App.vue 6.8 KB

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