App.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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() {
  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()*/null, result => {
  74. Toast.methods.addToast(`You have successfully registered.`, 4000);
  75. setTimeout(() => {
  76. if (result.SID) {
  77. let date = new Date();
  78. date.setTime(new Date().getTime() + (2 * 365 * 24 * 60 * 60 * 1000));
  79. document.cookie = `SID=${result.SID}; expires=${date.toGMTString()}; path=/`;
  80. location.reload();
  81. } else {
  82. _this.$router.go('/login');
  83. }
  84. }, 4000);
  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 {
  99. Toast.methods.addToast(result.message, 2000);
  100. }
  101. });
  102. },
  103. 'toggleModal': function (type) {
  104. switch(type) {
  105. case 'register':
  106. this.isRegisterActive = !this.isRegisterActive;
  107. break;
  108. case 'login':
  109. this.isLoginActive = !this.isLoginActive;
  110. break;
  111. case 'createCommunityStation':
  112. this.isCreateCommunityStationActive = !this.isCreateCommunityStationActive;
  113. break;
  114. }
  115. },
  116. 'handleSocketConnection': function() {
  117. this.socketConnected = window.socketConnected;
  118. this.$broadcast('handleSocketConnection');
  119. }
  120. },
  121. components: { Toast, WhatIsNew, LoginModal, RegisterModal, CreateCommunityStation }
  122. }
  123. </script>
  124. <style type='scss'>
  125. #toast-container { z-index: 10000 !important; }
  126. .socketNotConnected {
  127. padding: 20px;
  128. color: white;
  129. background-color: red;
  130. position: fixed;
  131. top: 50px;
  132. right: 50px;
  133. font-size: 2em;
  134. border-radius: 5px;
  135. z-index: 10000000;
  136. }
  137. </style>