Security.vue 7.6 KB

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