Account.vue 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. <template>
  2. <div class="content account-tab">
  3. <h4 class="section-title">Change account details</h4>
  4. <p class="section-description">Keep these details up-to-date</p>
  5. <hr class="section-horizontal-rule" />
  6. <p class="control is-expanded margin-top-zero">
  7. <label for="username">Username</label>
  8. <input
  9. class="input"
  10. id="username"
  11. type="text"
  12. placeholder="Enter username here..."
  13. v-model="modifiedUser.username"
  14. maxlength="32"
  15. autocomplete="off"
  16. @keypress="onInput('username')"
  17. @paste="onInput('username')"
  18. />
  19. <span v-if="modifiedUser.username" class="character-counter"
  20. >{{ modifiedUser.username.length }}/32</span
  21. >
  22. </p>
  23. <transition name="fadein-helpbox">
  24. <input-help-box
  25. :entered="validation.username.entered"
  26. :valid="validation.username.valid"
  27. :message="validation.username.message"
  28. />
  29. </transition>
  30. <p class="control is-expanded">
  31. <label for="email">Email</label>
  32. <input
  33. class="input"
  34. id="email"
  35. type="text"
  36. placeholder="Enter email address here..."
  37. v-if="modifiedUser.email"
  38. v-model="modifiedUser.email.address"
  39. @keypress="onInput('email')"
  40. @paste="onInput('email')"
  41. autocomplete="off"
  42. />
  43. </p>
  44. <transition name="fadein-helpbox">
  45. <input-help-box
  46. :entered="validation.email.entered"
  47. :valid="validation.email.valid"
  48. :message="validation.email.message"
  49. />
  50. </transition>
  51. <save-button ref="saveButton" @clicked="saveChanges()" />
  52. <div class="section-margin-bottom" />
  53. <h4 class="section-title">Remove any data we hold on you</h4>
  54. <p class="section-description">
  55. Permanently remove your account and/or data we store on you
  56. </p>
  57. <hr class="section-horizontal-rule" />
  58. <div class="row">
  59. <quick-confirm @confirm="removeActivities()">
  60. <a class="button is-warning">
  61. <i class="material-icons icon-with-button">cancel</i>
  62. Clear my activities
  63. </a>
  64. </quick-confirm>
  65. <a class="button is-danger" @click="openModal('removeAccount')">
  66. <i class="material-icons icon-with-button">delete</i>
  67. Remove my account
  68. </a>
  69. </div>
  70. </div>
  71. </template>
  72. <script>
  73. import { mapState, mapActions, mapGetters } from "vuex";
  74. import Toast from "toasters";
  75. import InputHelpBox from "@/components/InputHelpBox.vue";
  76. import SaveButton from "@/components/SaveButton.vue";
  77. import validation from "@/validation";
  78. import QuickConfirm from "@/components/QuickConfirm.vue";
  79. export default {
  80. components: {
  81. InputHelpBox,
  82. SaveButton,
  83. QuickConfirm
  84. },
  85. data() {
  86. return {
  87. validation: {
  88. username: {
  89. entered: false,
  90. valid: false,
  91. message: "Please enter a valid username."
  92. },
  93. email: {
  94. entered: false,
  95. valid: false,
  96. message: "Please enter a valid email address."
  97. }
  98. }
  99. };
  100. },
  101. computed: {
  102. ...mapState({
  103. userId: state => state.user.auth.userId,
  104. originalUser: state => state.settings.originalUser,
  105. modifiedUser: state => state.settings.modifiedUser
  106. }),
  107. ...mapGetters({
  108. socket: "websockets/getSocket"
  109. })
  110. },
  111. watch: {
  112. // prettier-ignore
  113. // eslint-disable-next-line func-names
  114. "modifiedUser.username": function (value) {
  115. if (!validation.isLength(value, 2, 32)) {
  116. this.validation.username.message =
  117. "Username must have between 2 and 32 characters.";
  118. this.validation.username.valid = false;
  119. } else if (
  120. !validation.regex.azAZ09_.test(value) &&
  121. value !== this.originalUser.username // Sometimes a username pulled from GitHub won't succeed validation
  122. ) {
  123. this.validation.username.message =
  124. "Invalid format. Allowed characters: a-z, A-Z, 0-9 and _.";
  125. this.validation.username.valid = false;
  126. } else if (value.replaceAll(/[_]/g, "").length === 0) {
  127. this.validation.username.message =
  128. "Invalid format. Allowed characters: a-z, A-Z, 0-9 and _, and there has to be at least one letter or number.";
  129. this.validation.username.valid = false;
  130. } else {
  131. this.validation.username.message = "Everything looks great!";
  132. this.validation.username.valid = true;
  133. }
  134. },
  135. // prettier-ignore
  136. // eslint-disable-next-line func-names
  137. "modifiedUser.email.address": function (value) {
  138. if (!validation.isLength(value, 3, 254)) {
  139. this.validation.email.message =
  140. "Email must have between 3 and 254 characters.";
  141. this.validation.email.valid = false;
  142. } else if (
  143. value.indexOf("@") !== value.lastIndexOf("@") ||
  144. !validation.regex.emailSimple.test(value)
  145. ) {
  146. this.validation.email.message = "Invalid format.";
  147. this.validation.email.valid = false;
  148. } else {
  149. this.validation.email.message = "Everything looks great!";
  150. this.validation.email.valid = true;
  151. }
  152. }
  153. },
  154. mounted() {
  155. if (
  156. this.$route.query.removeAccount === "relinked-github" &&
  157. !localStorage.getItem("github_redirect")
  158. ) {
  159. this.openModal("removeAccount");
  160. setTimeout(() => {
  161. const modal = this.$parent.$children.find(
  162. child => child.name === "RemoveAccount"
  163. );
  164. modal.confirmGithubLink();
  165. }, 50);
  166. }
  167. },
  168. methods: {
  169. onInput(inputName) {
  170. this.validation[inputName].entered = true;
  171. },
  172. saveChanges() {
  173. const usernameChanged =
  174. this.modifiedUser.username !== this.originalUser.username;
  175. const emailAddressChanged =
  176. this.modifiedUser.email.address !==
  177. this.originalUser.email.address;
  178. if (usernameChanged) this.changeUsername();
  179. if (emailAddressChanged) this.changeEmail();
  180. if (!usernameChanged && !emailAddressChanged) {
  181. this.$refs.saveButton.handleFailedSave();
  182. new Toast("Please make a change before saving.");
  183. }
  184. },
  185. changeEmail() {
  186. const email = this.modifiedUser.email.address;
  187. if (!validation.isLength(email, 3, 254))
  188. return new Toast(
  189. "Email must have between 3 and 254 characters."
  190. );
  191. if (
  192. email.indexOf("@") !== email.lastIndexOf("@") ||
  193. !validation.regex.emailSimple.test(email)
  194. )
  195. return new Toast("Invalid email format.");
  196. this.$refs.saveButton.saveStatus = "disabled";
  197. return this.socket.dispatch(
  198. "users.updateEmail",
  199. this.userId,
  200. email,
  201. res => {
  202. if (res.status !== "success") {
  203. new Toast(res.message);
  204. this.$refs.saveButton.handleFailedSave();
  205. } else {
  206. new Toast("Successfully changed email address");
  207. this.updateOriginalUser({
  208. property: "email.address",
  209. value: email
  210. });
  211. this.$refs.saveButton.handleSuccessfulSave();
  212. }
  213. }
  214. );
  215. },
  216. changeUsername() {
  217. const { username } = this.modifiedUser;
  218. if (!validation.isLength(username, 2, 32))
  219. return new Toast(
  220. "Username must have between 2 and 32 characters."
  221. );
  222. if (!validation.regex.azAZ09_.test(username))
  223. return new Toast(
  224. "Invalid username format. Allowed characters: a-z, A-Z, 0-9 and _."
  225. );
  226. if (username.replaceAll(/[_]/g, "").length === 0)
  227. return new Toast(
  228. "Invalid username format. Allowed characters: a-z, A-Z, 0-9 and _, and there has to be at least one letter or number."
  229. );
  230. this.$refs.saveButton.saveStatus = "disabled";
  231. return this.socket.dispatch(
  232. "users.updateUsername",
  233. this.userId,
  234. username,
  235. res => {
  236. if (res.status !== "success") {
  237. new Toast(res.message);
  238. this.$refs.saveButton.handleFailedSave();
  239. } else {
  240. new Toast("Successfully changed username");
  241. this.updateOriginalUser({
  242. property: "username",
  243. value: username
  244. });
  245. this.$refs.saveButton.handleSuccessfulSave();
  246. }
  247. }
  248. );
  249. },
  250. removeActivities() {
  251. this.socket.dispatch("activities.removeAllForUser", res => {
  252. new Toast(res.message);
  253. });
  254. },
  255. ...mapActions("settings", ["updateOriginalUser"]),
  256. ...mapActions("modalVisibility", ["openModal"])
  257. }
  258. };
  259. </script>
  260. <style lang="less" scoped>
  261. .control {
  262. margin-bottom: 2px !important;
  263. }
  264. .row {
  265. display: flex;
  266. }
  267. </style>