Security.vue 7.6 KB

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