App.vue 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <template>
  2. <div>
  3. <router-view></router-view>
  4. </div>
  5. </template>
  6. <script>
  7. export default {
  8. replace: false,
  9. data() {
  10. return {
  11. register: {
  12. email: "",
  13. username: "",
  14. password: ""
  15. },
  16. login: {
  17. email: "",
  18. password: ""
  19. },
  20. likes: [],
  21. dislikes: [],
  22. loggedIn: false,
  23. stations: {
  24. official: [],
  25. community: []
  26. }
  27. }
  28. },
  29. methods: {
  30. logout: function () {
  31. this.socket.emit('users.logout');
  32. document.cookie = 'SID=;expires=Thu, 01 Jan 1970 00:00:01 GMT;';
  33. location.reload();
  34. }
  35. },
  36. ready: function () {
  37. lofig.get('socket.url', function(res) {
  38. let socket = this.socket = io(window.location.protocol + '//' + res);
  39. socket.on("ready", status => this.loggedIn = status);
  40. socket.emit("stations.index", data => {
  41. if (data.status === "success") data.stations.forEach(station => {
  42. if (station.type == 'official') this.stations.official.push(station);
  43. else this.stations.community.push(station);
  44. });
  45. });
  46. });
  47. },
  48. events: {
  49. 'register': function () {
  50. let { register: { email, username, password } } = this;
  51. this.socket.emit('users.register', email, username, password, grecaptcha.getResponse(), (result) => {
  52. // Need to somehow execute this on Home.vue
  53. // Toast.methods.addToast(`User ${username} has been registered`, 2000);
  54. setTimeout(location.reload(), 2500);
  55. });
  56. },
  57. 'login': function () {
  58. let { login: { email, password } } = this;
  59. this.socket.emit('users.login', email, password, (result) => {
  60. console.log(result);
  61. if (result.status === 'success') {
  62. let date = new Date();
  63. date.setTime(new Date().getTime() + (2*365*24*60*60*1000));
  64. document.cookie = "SID=" + result.sessionId + "; expires="+ date.toGMTString() +"; path=/";
  65. location.reload();
  66. } else {
  67. //TODO Error toast
  68. }
  69. });
  70. },
  71. 'joinStation': function (id) {
  72. let mergedStations = this.stations.community.concat(this.stations.official);
  73. this.socket.emit('stations.join', id, result => {
  74. mergedStations.find(station => station.id === id).users = result.userCount;
  75. });
  76. },
  77. 'leaveStation': function () {
  78. this.socket.emit('stations.leave', result => {
  79. //this.stations.find(station => station.id === id).users = result.userCount;
  80. });
  81. }
  82. }
  83. }
  84. </script>