App.vue 7.1 KB

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