App.vue 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <template>
  2. <div>
  3. <h1 v-if="!socketConnected" class="socketNotConnected">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. <create-community-station v-if='isCreateCommunityStationActive'></create-community-station>
  10. </div>
  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 CreateCommunityStation from './components/Modals/CreateCommunityStation.vue';
  18. import auth from './auth';
  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. isCreateCommunityStationActive: false,
  39. serverDomain: '',
  40. socketConnected: true
  41. }
  42. },
  43. methods: {
  44. logout: function () {
  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) b(); return false;
  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. lofig.get('serverDomain', res => {
  66. _this.serverDomain = res;
  67. });
  68. },
  69. events: {
  70. 'register': function () {
  71. let { register: { email, username, password } } = this;
  72. let _this = this;
  73. this.socket.emit('users.register', username, email, password, grecaptcha.getResponse(), result => {
  74. if (result.status === 'success') {
  75. Toast.methods.addToast(`You have successfully registered.`, 4000);
  76. setTimeout(() => {
  77. if (result.SID) {
  78. let date = new Date();
  79. date.setTime(new Date().getTime() + (2 * 365 * 24 * 60 * 60 * 1000));
  80. document.cookie = `SID=${result.SID}; expires=${date.toGMTString()}; path=/`;
  81. location.reload();
  82. } else _this.$router.go('/login');
  83. }, 4000);
  84. } else Toast.methods.addToast(result.message, 8000);
  85. });
  86. },
  87. 'login': function () {
  88. let { login: { email, password } } = this;
  89. let _this = this;
  90. this.socket.emit('users.login', email, password, result => {
  91. if (result.status === 'success') {
  92. let date = new Date();
  93. date.setTime(new Date().getTime() + (2 * 365 * 24 * 60 * 60 * 1000));
  94. document.cookie = `SID=${result.SID}; expires=${date.toGMTString()}; path=/`;
  95. Toast.methods.addToast(`You have been successfully logged in`, 2000);
  96. _this.$router.go('/');
  97. location.reload();
  98. } else Toast.methods.addToast(result.message, 2000);
  99. });
  100. },
  101. 'toggleModal': function (type) {
  102. switch(type) {
  103. case 'register':
  104. this.isRegisterActive = !this.isRegisterActive;
  105. break;
  106. case 'login':
  107. this.isLoginActive = !this.isLoginActive;
  108. break;
  109. case 'createCommunityStation':
  110. this.isCreateCommunityStationActive = !this.isCreateCommunityStationActive;
  111. break;
  112. }
  113. },
  114. 'handleSocketConnection': function () {
  115. this.socketConnected = window.socketConnected;
  116. console.log(123332222111);
  117. this.$broadcast('handleSocketConnection');
  118. },
  119. 'closeModal': function () {
  120. this.$broadcast('closeModal');
  121. }
  122. },
  123. components: { Toast, WhatIsNew, LoginModal, RegisterModal, CreateCommunityStation }
  124. }
  125. </script>
  126. <style type='scss'>
  127. #toast-container { z-index: 10000 !important; }
  128. .socketNotConnected {
  129. padding: 20px;
  130. color: white;
  131. background-color: red;
  132. position: fixed;
  133. top: 50px;
  134. right: 50px;
  135. font-size: 2em;
  136. border-radius: 5px;
  137. z-index: 10000000;
  138. }
  139. </style>