App.vue 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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() {
  37. let _this = this;
  38. lofig.get('socket.url', function(res) {
  39. let socket = _this.socket = io(window.location.protocol + '//' + res);
  40. socket.on("ready", status => _this.loggedIn = status);
  41. socket.emit("stations.index", data => {
  42. if (data.status === "success") data.stations.forEach(station => {
  43. if (station.type == 'official') _this.stations.official.push(station);
  44. else _this.stations.community.push(station);
  45. });
  46. });
  47. });
  48. },
  49. events: {
  50. 'register': function () {
  51. let { register: { email, username, password } } = this;
  52. this.socket.emit('users.register', email, username, password, grecaptcha.getResponse(), (result) => {
  53. // Need to somehow execute this on Home.vue
  54. // Toast.methods.addToast(`User ${username} has been registered`, 2000);
  55. setTimeout(location.reload(), 2500);
  56. });
  57. },
  58. 'login': function () {
  59. let { login: { email, password } } = this;
  60. this.socket.emit('users.login', email, password, (result) => {
  61. console.log(result);
  62. if (result.status === 'success') {
  63. let date = new Date();
  64. date.setTime(new Date().getTime() + (2*365*24*60*60*1000));
  65. document.cookie = "SID=" + result.sessionId + "; expires="+ date.toGMTString() +"; path=/";
  66. location.reload();
  67. } else {
  68. //TODO Error toast
  69. }
  70. });
  71. },
  72. 'joinStation': function (id) {
  73. let mergedStations = this.stations.community.concat(this.stations.official);
  74. this.socket.emit('stations.join', id, result => {
  75. mergedStations.find(station => station.id === id).users = result.userCount;
  76. });
  77. },
  78. 'leaveStation': function () {
  79. this.socket.emit('stations.leave', result => {
  80. //this.stations.find(station => station.id === id).users = result.userCount;
  81. });
  82. }
  83. }
  84. }
  85. </script>