Security.vue 7.6 KB

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