Settings.vue 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <template>
  2. <main-header></main-header>
  3. <div class="container">
  4. <!--Implement Validation-->
  5. <label class="label">Username</label>
  6. <div class="control is-grouped">
  7. <p class="control is-expanded has-icon has-icon-right">
  8. <input class="input" type="text" placeholder="Change username" v-model="user.username">
  9. <!--Remove validation if it's their own without changing-->
  10. </p>
  11. <p class="control">
  12. <button class="button is-success" @click="changeUsername()">Save changes</button>
  13. </p>
  14. </div>
  15. <label class="label">Email</label>
  16. <div class="control is-grouped" v-if="user.email">
  17. <p class="control is-expanded has-icon has-icon-right">
  18. <input class="input" type="text" placeholder="Change email address" v-model="user.email.address">
  19. <!--Remove validation if it's their own without changing-->
  20. </p>
  21. <p class="control is-expanded">
  22. <button class="button is-success" @click="changeEmail()">Save changes</button>
  23. </p>
  24. </div>
  25. <label class="label" v-if="password">Change Password</label>
  26. <div class="control is-grouped" v-if="password">
  27. <p class="control is-expanded has-icon has-icon-right">
  28. <input class="input" type="password" placeholder="Change password" v-model="newPassword">
  29. </p>
  30. <p class="control is-expanded">
  31. <button class="button is-success" @click="changePassword()">Change password</button>
  32. </p>
  33. </div>
  34. <label class="label" v-if="!password">Add password</label>
  35. <div class="control is-grouped" v-if="!password">
  36. <button class="button is-success" @click="requestPassword()" v-if="passwordStep === 1">Request password email</button><br>
  37. <p class="control is-expanded has-icon has-icon-right" v-if="passwordStep === 2">
  38. <input class="input" type="text" placeholder="Code" v-model="passwordCode">
  39. </p>
  40. <p class="control is-expanded" v-if="passwordStep === 2">
  41. <button class="button is-success" @click="verifyCode()">Verify code</button>
  42. </p>
  43. <p class="control is-expanded has-icon has-icon-right" v-if="passwordStep === 3">
  44. <input class="input" type="password" placeholder="New password" v-model="setNewPassword">
  45. </p>
  46. <p class="control is-expanded" v-if="passwordStep === 3">
  47. <button class="button is-success" @click="setPassword()">Set password</button>
  48. </p>
  49. </div>
  50. <a href="#" v-if="passwordStep === 1 && !password" @click="passwordStep = 2">Skip this step</a>
  51. <a class="button is-github" v-if="!github" :href='$parent.serverDomain + "/auth/github/link"'>
  52. <div class='icon'>
  53. <img class='invert' src='/assets/social/github.svg'/>
  54. </div>
  55. &nbsp; Link GitHub to account
  56. </a>
  57. <button class="button is-danger" @click="unlinkPassword()" v-if="password && github">Remove logging in with password</button>
  58. <button class="button is-danger" @click="unlinkGitHub()" v-if="password && github">Remove logging in with GitHub</button>
  59. </div>
  60. <main-footer></main-footer>
  61. </template>
  62. <script>
  63. import { Toast } from 'vue-roaster';
  64. import MainHeader from '../MainHeader.vue';
  65. import MainFooter from '../MainFooter.vue';
  66. import LoginModal from '../Modals/Login.vue'
  67. import io from '../../io'
  68. import validation from '../../validation';
  69. export default {
  70. data() {
  71. return {
  72. user: {},
  73. newPassword: '',
  74. password: false,
  75. github: false,
  76. setNewPassword: '',
  77. passwordStep: 1,
  78. passwordCode: ''
  79. }
  80. },
  81. ready: function() {
  82. let _this = this;
  83. io.getSocket(socket => {
  84. _this.socket = socket;
  85. _this.socket.emit('users.findBySession', res => {
  86. if (res.status == 'success') {
  87. _this.user = res.data;
  88. _this.password = _this.user.password;
  89. _this.github = _this.user.github;
  90. } else {
  91. _this.$parent.isLoginActive = true;
  92. Toast.methods.addToast('Your are currently not signed in', 3000);
  93. }
  94. });
  95. _this.socket.on('event:user.username.changed', username => {
  96. _this.$parent.username = username;
  97. });
  98. _this.socket.on('event:user.linkPassword', () => {console.log(1);
  99. _this.password = true;
  100. });
  101. _this.socket.on('event:user.linkGitHub', () => {console.log(2);
  102. _this.github = true;
  103. });
  104. _this.socket.on('event:user.unlinkPassword', () => {console.log(3);
  105. _this.password = false;
  106. });
  107. _this.socket.on('event:user.unlinkGitHub', () => {console.log(4);
  108. _this.github = false;
  109. });
  110. });
  111. },
  112. methods: {
  113. changeEmail: function () {
  114. const email = this.user.email.address;
  115. if (!validation.isLength(email, 3, 254)) return Toast.methods.addToast('Email must have between 3 and 254 characters.', 8000);
  116. if (email.indexOf('@') !== email.lastIndexOf('@') || !validation.regex.emailSimple.test(email)) return Toast.methods.addToast('Invalid email format.', 8000);
  117. this.socket.emit('users.updateEmail', this.$parent.userId, email, res => {
  118. if (res.status !== 'success') Toast.methods.addToast(res.message, 8000);
  119. else Toast.methods.addToast('Successfully changed email address', 4000);
  120. });
  121. },
  122. changeUsername: function () {
  123. const username = this.user.username;
  124. if (!validation.isLength(username, 2, 32)) return Toast.methods.addToast('Username must have between 2 and 32 characters.', 8000);
  125. if (!validation.regex.azAZ09_.test(username)) return Toast.methods.addToast('Invalid username format. Allowed characters: a-z, A-Z, 0-9 and _.', 8000);
  126. this.socket.emit('users.updateUsername', this.$parent.userId, username, res => {
  127. if (res.status !== 'success') Toast.methods.addToast(res.message, 8000);
  128. else Toast.methods.addToast('Successfully changed username', 4000);
  129. });
  130. },
  131. changePassword: function () {
  132. const newPassword = this.newPassword;
  133. if (!validation.isLength(newPassword, 6, 200)) return Toast.methods.addToast('Password must have between 6 and 200 characters.', 8000);
  134. if (!validation.regex.password.test(newPassword)) return Toast.methods.addToast('Invalid password format. Must have one lowercase letter, one uppercase letter, one number and one special character.', 8000);
  135. this.socket.emit('users.updatePassword', newPassword, res => {
  136. if (res.status !== 'success') Toast.methods.addToast(res.message, 8000);
  137. else Toast.methods.addToast('Successfully changed password', 4000);
  138. });
  139. },
  140. requestPassword: function() {
  141. this.socket.emit('users.requestPassword', res => {
  142. Toast.methods.addToast(res.message, 8000);
  143. if (res.status === 'success') {
  144. this.passwordStep = 2;
  145. }
  146. });
  147. },
  148. verifyCode: function () {
  149. if (!this.passwordCode) return Toast.methods.addToast('Code cannot be empty', 8000);
  150. this.socket.emit('users.verifyPasswordCode', this.passwordCode, res => {
  151. Toast.methods.addToast(res.message, 8000);
  152. if (res.status === 'success') {
  153. this.passwordStep = 3;
  154. }
  155. });
  156. },
  157. setPassword: function () {
  158. const newPassword = this.setNewPassword;
  159. if (!validation.isLength(newPassword, 6, 200)) return Toast.methods.addToast('Password must have between 6 and 200 characters.', 8000);
  160. if (!validation.regex.password.test(newPassword)) return Toast.methods.addToast('Invalid password format. Must have one lowercase letter, one uppercase letter, one number and one special character.', 8000);
  161. this.socket.emit('users.changePasswordWithCode', this.passwordCode, newPassword, res => {
  162. Toast.methods.addToast(res.message, 8000);
  163. });
  164. },
  165. unlinkPassword: function () {
  166. this.socket.emit('users.unlinkPassword', res => {
  167. Toast.methods.addToast(res.message, 8000);
  168. });
  169. },
  170. unlinkGitHub: function () {
  171. this.socket.emit('users.unlinkGitHub', res => {
  172. Toast.methods.addToast(res.message, 8000);
  173. });
  174. }
  175. },
  176. components: { MainHeader, MainFooter, LoginModal }
  177. }
  178. </script>
  179. <style lang="scss" scoped>
  180. .container {
  181. padding: 25px;
  182. }
  183. a { color: #029ce3 !important; }
  184. </style>