Security.vue 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. <template>
  2. <div class="content security-tab">
  3. <div v-if="isPasswordLinked">
  4. <h4 class="section-title">Change password</h4>
  5. <p class="section-description">
  6. You will need to know your previous password
  7. </p>
  8. <hr class="section-horizontal-rule" />
  9. <p class="control is-expanded margin-top-zero">
  10. <label for="old-password">Previous password</label>
  11. </p>
  12. <div id="password-visibility-container">
  13. <input
  14. class="input"
  15. id="old-password"
  16. ref="oldPassword"
  17. type="password"
  18. placeholder="Enter your old password here..."
  19. v-model="validation.oldPassword.value"
  20. />
  21. <a @click="togglePasswordVisibility('oldPassword')">
  22. <i class="material-icons">
  23. {{
  24. !validation.oldPassword.visible
  25. ? "visibility"
  26. : "visibility_off"
  27. }}
  28. </i>
  29. </a>
  30. </div>
  31. <p class="control is-expanded">
  32. <label for="new-password">New password</label>
  33. </p>
  34. <div id="password-visibility-container">
  35. <input
  36. class="input"
  37. id="new-password"
  38. type="password"
  39. ref="newPassword"
  40. placeholder="Enter new password here..."
  41. v-model="validation.newPassword.value"
  42. @keyup.enter="changePassword()"
  43. @keypress="onInput('newPassword')"
  44. @paste="onInput('newPassword')"
  45. />
  46. <a @click="togglePasswordVisibility('newPassword')">
  47. <i class="material-icons">
  48. {{
  49. !validation.newPassword.visible
  50. ? "visibility"
  51. : "visibility_off"
  52. }}
  53. </i>
  54. </a>
  55. </div>
  56. <transition name="fadein-helpbox">
  57. <input-help-box
  58. :entered="validation.newPassword.entered"
  59. :valid="validation.newPassword.valid"
  60. :message="validation.newPassword.message"
  61. />
  62. </transition>
  63. <p class="control">
  64. <button
  65. id="change-password-button"
  66. class="button is-success"
  67. @click.prevent="changePassword()"
  68. >
  69. Change password
  70. </button>
  71. </p>
  72. <div class="section-margin-bottom" />
  73. </div>
  74. <div v-if="!isPasswordLinked">
  75. <h4 class="section-title">Add a password</h4>
  76. <p class="section-description">
  77. Add a password, as an alternative to signing in with GitHub
  78. </p>
  79. <hr class="section-horizontal-rule" />
  80. <router-link to="/set_password" class="button is-default" href="#"
  81. ><i class="material-icons icon-with-button">create</i>Set
  82. Password
  83. </router-link>
  84. <div class="section-margin-bottom" />
  85. </div>
  86. <div v-if="!isGithubLinked">
  87. <h4 class="section-title">Link your GitHub account</h4>
  88. <p class="section-description">
  89. Link your Musare account with GitHub
  90. </p>
  91. <hr class="section-horizontal-rule" />
  92. <a class="button is-github" :href="`${apiDomain}/auth/github/link`">
  93. <div class="icon">
  94. <img class="invert" src="/assets/social/github.svg" />
  95. </div>
  96. &nbsp; Link GitHub to account
  97. </a>
  98. <div class="section-margin-bottom" />
  99. </div>
  100. <div v-if="isPasswordLinked && isGithubLinked">
  101. <h4 class="section-title">Remove login methods</h4>
  102. <p class="section-description">
  103. Remove your password as a login method or unlink GitHub
  104. </p>
  105. <hr class="section-horizontal-rule" />
  106. <div class="row">
  107. <confirm v-if="isPasswordLinked" @confirm="unlinkPassword()">
  108. <a class="button is-danger">
  109. <i class="material-icons icon-with-button">close</i>
  110. Remove password
  111. </a>
  112. </confirm>
  113. <confirm v-if="isGithubLinked" @confirm="unlinkGitHub()">
  114. <a class="button is-danger">
  115. <i class="material-icons icon-with-button">link_off</i>
  116. Remove GitHub from account
  117. </a>
  118. </confirm>
  119. </div>
  120. <div class="section-margin-bottom" />
  121. </div>
  122. <div>
  123. <h4 class="section-title">Log out everywhere</h4>
  124. <p class="section-description">
  125. Remove all currently logged-in sessions for your account
  126. </p>
  127. <hr class="section-horizontal-rule" />
  128. <div class="row">
  129. <confirm @confirm="removeSessions()">
  130. <a class="button is-warning">
  131. <i class="material-icons icon-with-button"
  132. >exit_to_app</i
  133. >
  134. Logout everywhere
  135. </a>
  136. </confirm>
  137. </div>
  138. </div>
  139. </div>
  140. </template>
  141. <script>
  142. import Toast from "toasters";
  143. import { mapGetters, mapState } from "vuex";
  144. import InputHelpBox from "@/components/InputHelpBox.vue";
  145. import validation from "@/validation";
  146. import Confirm from "@/components/Confirm.vue";
  147. export default {
  148. components: { InputHelpBox, Confirm },
  149. data() {
  150. return {
  151. apiDomain: "",
  152. validation: {
  153. oldPassword: {
  154. value: "",
  155. visible: false
  156. },
  157. newPassword: {
  158. value: "",
  159. visible: false,
  160. valid: false,
  161. entered: false,
  162. message:
  163. "Include at least one lowercase letter, one uppercase letter, one number and one special character."
  164. }
  165. }
  166. };
  167. },
  168. computed: {
  169. ...mapGetters({
  170. isPasswordLinked: "settings/isPasswordLinked",
  171. isGithubLinked: "settings/isGithubLinked",
  172. socket: "websockets/getSocket"
  173. }),
  174. ...mapState({
  175. userId: state => state.user.auth.userId
  176. })
  177. },
  178. watch: {
  179. // eslint-disable-next-line func-names
  180. "validation.newPassword.value": function (value) {
  181. if (!validation.isLength(value, 6, 200)) {
  182. this.validation.newPassword.message =
  183. "Password must have between 6 and 200 characters.";
  184. this.validation.newPassword.valid = false;
  185. } else if (!validation.regex.password.test(value)) {
  186. this.validation.newPassword.message =
  187. "Include at least one lowercase letter, one uppercase letter, one number and one special character.";
  188. this.validation.newPassword.valid = false;
  189. } else {
  190. this.validation.newPassword.message = "Everything looks great!";
  191. this.validation.newPassword.valid = true;
  192. }
  193. }
  194. },
  195. async mounted() {
  196. this.apiDomain = await lofig.get("backend.apiDomain");
  197. },
  198. methods: {
  199. togglePasswordVisibility(ref) {
  200. if (this.$refs[ref].type === "password") {
  201. this.$refs[ref].type = "text";
  202. this.validation[ref].visible = true;
  203. } else {
  204. this.$refs[ref].type = "password";
  205. this.validation[ref].visible = false;
  206. }
  207. },
  208. onInput(inputName) {
  209. this.validation[inputName].entered = true;
  210. },
  211. changePassword() {
  212. const newPassword = this.validation.newPassword.value;
  213. if (this.validation.oldPassword.value === "")
  214. return new Toast("Please enter your previous password.");
  215. if (!this.validation.newPassword.valid)
  216. return new Toast("Please enter a valid new password.");
  217. return this.socket.dispatch(
  218. "users.updatePassword",
  219. this.validation.oldPassword.value,
  220. newPassword,
  221. res => {
  222. if (res.status !== "success") new Toast(res.message);
  223. else {
  224. this.validation.prevPassword.value = "";
  225. this.validation.newPassword.value = "";
  226. new Toast("Successfully changed password.");
  227. }
  228. }
  229. );
  230. },
  231. unlinkPassword() {
  232. this.socket.dispatch("users.unlinkPassword", res => {
  233. new Toast(res.message);
  234. });
  235. },
  236. unlinkGitHub() {
  237. this.socket.dispatch("users.unlinkGitHub", res => {
  238. new Toast(res.message);
  239. });
  240. },
  241. removeSessions() {
  242. this.socket.dispatch(`users.removeSessions`, this.userId, res => {
  243. new Toast(res.message);
  244. });
  245. }
  246. }
  247. };
  248. </script>
  249. <style lang="scss" scoped>
  250. #change-password-button {
  251. margin-top: 10px;
  252. }
  253. .control {
  254. margin-bottom: 2px !important;
  255. }
  256. .row {
  257. display: flex;
  258. }
  259. </style>