Security.vue 7.6 KB

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