Security.vue 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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"
  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. <quick-confirm
  108. v-if="isPasswordLinked"
  109. @confirm="unlinkPassword()"
  110. >
  111. <a class="button is-danger">
  112. <i class="material-icons icon-with-button">close</i>
  113. Remove password
  114. </a>
  115. </quick-confirm>
  116. <quick-confirm v-if="isGithubLinked" @confirm="unlinkGitHub()">
  117. <a class="button is-danger">
  118. <i class="material-icons icon-with-button">link_off</i>
  119. Remove GitHub from account
  120. </a>
  121. </quick-confirm>
  122. </div>
  123. <div class="section-margin-bottom" />
  124. </div>
  125. <div>
  126. <h4 class="section-title">Log out everywhere</h4>
  127. <p class="section-description">
  128. Remove all currently logged-in sessions for your account
  129. </p>
  130. <hr class="section-horizontal-rule" />
  131. <div class="row">
  132. <quick-confirm @confirm="removeSessions()">
  133. <a class="button is-warning">
  134. <i class="material-icons icon-with-button"
  135. >exit_to_app</i
  136. >
  137. Logout everywhere
  138. </a>
  139. </quick-confirm>
  140. </div>
  141. </div>
  142. </div>
  143. </template>
  144. <script>
  145. import Toast from "toasters";
  146. import { mapGetters, mapState } from "vuex";
  147. import InputHelpBox from "@/components/InputHelpBox.vue";
  148. import validation from "@/validation";
  149. import QuickConfirm from "@/components/QuickConfirm.vue";
  150. export default {
  151. components: { InputHelpBox, QuickConfirm },
  152. data() {
  153. return {
  154. apiDomain: "",
  155. validation: {
  156. oldPassword: {
  157. value: "",
  158. visible: false
  159. },
  160. newPassword: {
  161. value: "",
  162. visible: false,
  163. valid: false,
  164. entered: false,
  165. message:
  166. "Include at least one lowercase letter, one uppercase letter, one number and one special character."
  167. }
  168. }
  169. };
  170. },
  171. computed: {
  172. ...mapGetters({
  173. isPasswordLinked: "settings/isPasswordLinked",
  174. isGithubLinked: "settings/isGithubLinked",
  175. socket: "websockets/getSocket"
  176. }),
  177. ...mapState({
  178. userId: state => state.user.auth.userId
  179. })
  180. },
  181. watch: {
  182. // eslint-disable-next-line func-names
  183. "validation.newPassword.value": function (value) {
  184. if (!validation.isLength(value, 6, 200)) {
  185. this.validation.newPassword.message =
  186. "Password must have between 6 and 200 characters.";
  187. this.validation.newPassword.valid = false;
  188. } else if (!validation.regex.password.test(value)) {
  189. this.validation.newPassword.message =
  190. "Include at least one lowercase letter, one uppercase letter, one number and one special character.";
  191. this.validation.newPassword.valid = false;
  192. } else {
  193. this.validation.newPassword.message = "Everything looks great!";
  194. this.validation.newPassword.valid = true;
  195. }
  196. }
  197. },
  198. async mounted() {
  199. this.apiDomain = await lofig.get("backend.apiDomain");
  200. },
  201. methods: {
  202. togglePasswordVisibility(ref) {
  203. if (this.$refs[ref].type === "password") {
  204. this.$refs[ref].type = "text";
  205. this.validation[ref].visible = true;
  206. } else {
  207. this.$refs[ref].type = "password";
  208. this.validation[ref].visible = false;
  209. }
  210. },
  211. onInput(inputName) {
  212. this.validation[inputName].entered = true;
  213. },
  214. changePassword() {
  215. const newPassword = this.validation.newPassword.value;
  216. if (this.validation.oldPassword.value === "")
  217. return new Toast("Please enter your previous password.");
  218. if (!this.validation.newPassword.valid)
  219. return new Toast("Please enter a valid new password.");
  220. return this.socket.dispatch(
  221. "users.updatePassword",
  222. this.validation.oldPassword.value,
  223. newPassword,
  224. res => {
  225. if (res.status !== "success") new Toast(res.message);
  226. else {
  227. this.validation.oldPassword.value = "";
  228. this.validation.newPassword.value = "";
  229. new Toast("Successfully changed password.");
  230. }
  231. }
  232. );
  233. },
  234. unlinkPassword() {
  235. this.socket.dispatch("users.unlinkPassword", res => {
  236. new Toast(res.message);
  237. });
  238. },
  239. unlinkGitHub() {
  240. this.socket.dispatch("users.unlinkGitHub", res => {
  241. new Toast(res.message);
  242. });
  243. },
  244. removeSessions() {
  245. this.socket.dispatch(`users.removeSessions`, this.userId, res => {
  246. new Toast(res.message);
  247. });
  248. }
  249. }
  250. };
  251. </script>
  252. <style lang="less" scoped>
  253. #change-password-button {
  254. margin-top: 10px;
  255. }
  256. .control {
  257. margin-bottom: 2px !important;
  258. }
  259. .row {
  260. display: flex;
  261. }
  262. </style>