RemoveAccount.vue 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. <script setup lang="ts">
  2. import { ref, onMounted } from "vue";
  3. import { useRoute } from "vue-router";
  4. import Toast from "toasters";
  5. import { storeToRefs } from "pinia";
  6. import { useSettingsStore } from "@/stores/settings";
  7. import { useWebsocketsStore } from "@/stores/websockets";
  8. import { useRemoveAccountStore } from "@/stores/removeAccount";
  9. import { useModalsStore } from "@/stores/modals";
  10. const props = defineProps({
  11. modalUuid: { type: String, default: "" }
  12. });
  13. const settingsStore = useSettingsStore();
  14. const route = useRoute();
  15. const { socket } = useWebsocketsStore();
  16. const removeAccountStore = useRemoveAccountStore(props);
  17. const { githubLinkConfirmed } = storeToRefs(removeAccountStore);
  18. const { isPasswordLinked, isGithubLinked } = settingsStore;
  19. const { closeCurrentModal } = useModalsStore();
  20. const step = ref("confirm-identity");
  21. const apiDomain = ref("");
  22. const accountRemovalMessage = ref("");
  23. const password = ref({
  24. value: "",
  25. visible: false
  26. });
  27. const passwordElement = ref();
  28. const githubAuthentication = ref(false);
  29. const checkForAutofill = (cb, event) => {
  30. if (
  31. event.target.value !== "" &&
  32. event.inputType === undefined &&
  33. event.data === undefined &&
  34. event.dataTransfer === undefined &&
  35. event.isComposing === undefined
  36. )
  37. cb();
  38. };
  39. const submitOnEnter = (cb, event) => {
  40. if (event.which === 13) cb();
  41. };
  42. const togglePasswordVisibility = () => {
  43. if (passwordElement.value.type === "password") {
  44. passwordElement.value.type = "text";
  45. password.value.visible = true;
  46. } else {
  47. passwordElement.value.type = "password";
  48. password.value.visible = false;
  49. }
  50. };
  51. const confirmPasswordMatch = () =>
  52. socket.dispatch("users.confirmPasswordMatch", password.value.value, res => {
  53. if (res.status === "success") step.value = "remove-account";
  54. else new Toast(res.message);
  55. });
  56. const confirmGithubLink = () =>
  57. socket.dispatch("users.confirmGithubLink", res => {
  58. if (res.status === "success") {
  59. if (res.data.linked) step.value = "remove-account";
  60. else {
  61. new Toast(
  62. `Your GitHub account isn't linked. Please re-link your account and try again.`
  63. );
  64. step.value = "relink-github";
  65. }
  66. } else new Toast(res.message);
  67. });
  68. const relinkGithub = () => {
  69. localStorage.setItem(
  70. "github_redirect",
  71. `${window.location.pathname + window.location.search}${
  72. !route.query.removeAccount ? "&removeAccount=relinked-github" : ""
  73. }`
  74. );
  75. };
  76. const remove = () =>
  77. socket.dispatch("users.remove", res => {
  78. if (res.status === "success") {
  79. return socket.dispatch("users.logout", () =>
  80. lofig.get("cookie").then(cookie => {
  81. document.cookie = `${cookie.SIDname}=;expires=Thu, 01 Jan 1970 00:00:01 GMT;`;
  82. closeCurrentModal();
  83. window.location.href = "/";
  84. })
  85. );
  86. }
  87. return new Toast(res.message);
  88. });
  89. onMounted(async () => {
  90. apiDomain.value = await lofig.get("backend.apiDomain");
  91. accountRemovalMessage.value = await lofig.get("messages.accountRemoval");
  92. githubAuthentication.value = await lofig.get(
  93. "siteSettings.githubAuthentication"
  94. );
  95. if (githubLinkConfirmed.value === true) confirmGithubLink();
  96. });
  97. </script>
  98. <template>
  99. <modal
  100. title="Confirm Account Removal"
  101. class="confirm-account-removal-modal"
  102. >
  103. <template #body>
  104. <div id="steps">
  105. <p
  106. class="step"
  107. :class="{ selected: step === 'confirm-identity' }"
  108. >
  109. 1
  110. </p>
  111. <span class="divider"></span>
  112. <p
  113. class="step"
  114. :class="{
  115. selected:
  116. (isPasswordLinked && step === 'export-data') ||
  117. step === 'relink-github'
  118. }"
  119. >
  120. 2
  121. </p>
  122. <span class="divider"></span>
  123. <p
  124. class="step"
  125. :class="{
  126. selected:
  127. (isPasswordLinked && step === 'remove-account') ||
  128. step === 'export-data'
  129. }"
  130. >
  131. 3
  132. </p>
  133. <span class="divider" v-if="!isPasswordLinked"></span>
  134. <p
  135. class="step"
  136. :class="{ selected: step === 'remove-account' }"
  137. v-if="!isPasswordLinked"
  138. >
  139. 4
  140. </p>
  141. </div>
  142. <div
  143. class="content-box"
  144. id="password-linked"
  145. v-if="
  146. step === 'confirm-identity' &&
  147. (isPasswordLinked || !githubAuthentication)
  148. "
  149. >
  150. <h2 class="content-box-title">Enter your password</h2>
  151. <p class="content-box-description">
  152. Confirming your password will let us verify your identity.
  153. </p>
  154. <p class="content-box-optional-helper">
  155. <router-link id="forgot-password" to="/reset_password">
  156. Forgot password?
  157. </router-link>
  158. </p>
  159. <div class="content-box-inputs">
  160. <div class="control is-grouped input-with-button">
  161. <div id="password-visibility-container">
  162. <input
  163. class="input"
  164. type="password"
  165. placeholder="Enter password here..."
  166. autofocus
  167. ref="passwordElement"
  168. v-model="password.value"
  169. @input="
  170. checkForAutofill(
  171. confirmPasswordMatch,
  172. $event
  173. )
  174. "
  175. @keypress="
  176. submitOnEnter(confirmPasswordMatch, $event)
  177. "
  178. />
  179. <a @click="togglePasswordVisibility()">
  180. <i class="material-icons">
  181. {{
  182. !password.visible
  183. ? "visibility"
  184. : "visibility_off"
  185. }}
  186. </i>
  187. </a>
  188. </div>
  189. <p class="control">
  190. <button
  191. class="button is-info"
  192. @click="confirmPasswordMatch()"
  193. >
  194. Check
  195. </button>
  196. </p>
  197. </div>
  198. </div>
  199. </div>
  200. <div
  201. class="content-box"
  202. v-else-if="
  203. githubAuthentication &&
  204. isGithubLinked &&
  205. step === 'confirm-identity'
  206. "
  207. >
  208. <h2 class="content-box-title">Verify your GitHub</h2>
  209. <p class="content-box-description">
  210. Check your account is still linked to remove your account.
  211. </p>
  212. <div class="content-box-inputs">
  213. <a class="button is-github" @click="confirmGithubLink()">
  214. <div class="icon">
  215. <img
  216. class="invert"
  217. src="/assets/social/github.svg"
  218. />
  219. </div>
  220. &nbsp; Check GitHub is linked
  221. </a>
  222. </div>
  223. </div>
  224. <div
  225. class="content-box"
  226. v-if="githubAuthentication && step === 'relink-github'"
  227. >
  228. <h2 class="content-box-title">Re-link GitHub</h2>
  229. <p class="content-box-description">
  230. Re-link your GitHub account in order to verify your
  231. identity.
  232. </p>
  233. <div class="content-box-inputs">
  234. <a
  235. class="button is-github"
  236. @click="relinkGithub()"
  237. :href="`${apiDomain}/auth/github/link`"
  238. >
  239. <div class="icon">
  240. <img
  241. class="invert"
  242. src="/assets/social/github.svg"
  243. />
  244. </div>
  245. &nbsp; Re-link GitHub to account
  246. </a>
  247. </div>
  248. </div>
  249. <div v-if="step === 'export-data'">
  250. DOWNLOAD A BACKUP OF YOUR DATA BEFORE ITS PERMENATNELY DELETED
  251. </div>
  252. <div
  253. class="content-box"
  254. id="remove-account-container"
  255. v-if="step === 'remove-account'"
  256. >
  257. <h2 class="content-box-title">Remove your account</h2>
  258. <p class="content-box-description">
  259. {{ accountRemovalMessage }}
  260. </p>
  261. <div class="content-box-inputs">
  262. <quick-confirm placement="right" @confirm="remove()">
  263. <button class="button">
  264. <i class="material-icons">delete</i>
  265. &nbsp;Remove Account
  266. </button>
  267. </quick-confirm>
  268. </div>
  269. </div>
  270. </template>
  271. </modal>
  272. </template>
  273. <style lang="less">
  274. .confirm-account-removal-modal {
  275. .modal-card {
  276. width: 650px;
  277. }
  278. }
  279. </style>
  280. <style lang="less" scoped>
  281. h2 {
  282. margin: 0;
  283. }
  284. .content-box {
  285. margin-top: 20px;
  286. max-width: unset;
  287. }
  288. #steps {
  289. margin-top: 0;
  290. }
  291. #password-linked {
  292. #password-visibility-container {
  293. width: 100%;
  294. }
  295. > a {
  296. color: var(--primary-color);
  297. }
  298. }
  299. .control {
  300. margin-bottom: 0 !important;
  301. }
  302. #remove-account-container .content-box-inputs {
  303. width: fit-content;
  304. }
  305. </style>