ResetPassword.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. <template>
  2. <div>
  3. <page-metadata
  4. :title="mode === 'reset' ? 'Reset password' : 'Set password'"
  5. />
  6. <main-header />
  7. <div class="container">
  8. <div class="content-wrapper">
  9. <h1 id="title" class="has-text-centered page-title">
  10. {{ mode === "reset" ? "Reset" : "Set" }} your password
  11. </h1>
  12. <div id="steps">
  13. <p class="step" :class="{ selected: step === 1 }">1</p>
  14. <span class="divider"></span>
  15. <p class="step" :class="{ selected: step === 2 }">2</p>
  16. <span class="divider"></span>
  17. <p class="step" :class="{ selected: step === 3 }">3</p>
  18. </div>
  19. <div class="content-box-wrapper">
  20. <transition-group name="steps-fade" mode="out-in">
  21. <div class="content-box">
  22. <!-- Step 1 -- Enter email address -->
  23. <div v-if="step === 1" key="1">
  24. <h2 class="content-box-title">
  25. Enter your email address
  26. </h2>
  27. <p class="content-box-description">
  28. We will send a code to your email address to
  29. verify your identity.
  30. </p>
  31. <p class="content-box-optional-helper">
  32. <a @click="step = 2"
  33. >Already have a code?</a
  34. >
  35. </p>
  36. <div class="content-box-inputs">
  37. <div
  38. class="
  39. control
  40. is-grouped
  41. input-with-button
  42. "
  43. >
  44. <p class="control is-expanded">
  45. <input
  46. class="input"
  47. type="email"
  48. placeholder="Enter email address here..."
  49. autofocus
  50. v-model="email.value"
  51. @keyup.enter="submitEmail()"
  52. @keypress="onInput('email')"
  53. @paste="onInput('email')"
  54. />
  55. </p>
  56. <p class="control">
  57. <button
  58. class="button is-info"
  59. @click="submitEmail()"
  60. >
  61. <i
  62. class="
  63. material-icons
  64. icon-with-button
  65. "
  66. >mail</i
  67. >Request
  68. </button>
  69. </p>
  70. </div>
  71. <transition name="fadein-helpbox">
  72. <input-help-box
  73. :entered="email.entered"
  74. :valid="email.valid"
  75. :message="email.message"
  76. />
  77. </transition>
  78. </div>
  79. </div>
  80. <!-- Step 2 -- Enter code -->
  81. <div v-if="step === 2" key="2">
  82. <h2 class="content-box-title">
  83. Enter the code sent to your email
  84. </h2>
  85. <p
  86. class="content-box-description"
  87. v-if="!email.hasBeenSentAlready"
  88. >
  89. A code has been sent to
  90. <strong>{{ email.value }}.</strong>
  91. </p>
  92. <p class="content-box-optional-helper">
  93. <a
  94. @click="
  95. email.value
  96. ? submitEmail()
  97. : (step = 1)
  98. "
  99. >Request another code</a
  100. >
  101. </p>
  102. <div class="content-box-inputs">
  103. <div
  104. class="
  105. control
  106. is-grouped
  107. input-with-button
  108. "
  109. >
  110. <p class="control is-expanded">
  111. <input
  112. class="input"
  113. type="text"
  114. placeholder="Enter code here..."
  115. autofocus
  116. v-model="code"
  117. @keyup.enter="verifyCode()"
  118. />
  119. </p>
  120. <p class="control">
  121. <button
  122. class="button is-info"
  123. @click="verifyCode()"
  124. >
  125. <i
  126. class="
  127. material-icons
  128. icon-with-button
  129. "
  130. >vpn_key</i
  131. >Verify
  132. </button>
  133. </p>
  134. </div>
  135. </div>
  136. </div>
  137. <!-- Step 3 -- Set new password -->
  138. <div v-if="step === 3" key="3">
  139. <h2 class="content-box-title">
  140. Set a new password
  141. </h2>
  142. <p class="content-box-description">
  143. Create a new password for your account.
  144. </p>
  145. <div class="content-box-inputs">
  146. <p class="control is-expanded">
  147. <label for="new-password"
  148. >New password</label
  149. >
  150. </p>
  151. <div id="password-visibility-container">
  152. <input
  153. class="input"
  154. id="new-password"
  155. type="password"
  156. ref="password"
  157. placeholder="Enter password here..."
  158. v-model="password.value"
  159. @keypress="onInput('password')"
  160. @paste="onInput('password')"
  161. />
  162. <a
  163. @click="
  164. togglePasswordVisibility(
  165. 'password'
  166. )
  167. "
  168. >
  169. <i class="material-icons">
  170. {{
  171. !password.visible
  172. ? "visibility"
  173. : "visibility_off"
  174. }}
  175. </i>
  176. </a>
  177. </div>
  178. <transition name="fadein-helpbox">
  179. <input-help-box
  180. :entered="password.entered"
  181. :valid="password.valid"
  182. :message="password.message"
  183. />
  184. </transition>
  185. <p
  186. id="new-password-again-input"
  187. class="control is-expanded"
  188. >
  189. <label for="new-password-again"
  190. >New password again</label
  191. >
  192. </p>
  193. <div id="password-visibility-container">
  194. <input
  195. class="input"
  196. id="new-password-again"
  197. type="password"
  198. ref="passwordAgain"
  199. placeholder="Enter password here..."
  200. v-model="passwordAgain.value"
  201. @keyup.enter="changePassword()"
  202. @keypress="onInput('passwordAgain')"
  203. @paste="onInput('passwordAgain')"
  204. />
  205. <a
  206. @click="
  207. togglePasswordVisibility(
  208. 'passwordAgain'
  209. )
  210. "
  211. >
  212. <i class="material-icons">
  213. {{
  214. !passwordAgain.visible
  215. ? "visibility"
  216. : "visibility_off"
  217. }}
  218. </i>
  219. </a>
  220. </div>
  221. <transition name="fadein-helpbox">
  222. <input-help-box
  223. :entered="passwordAgain.entered"
  224. :valid="passwordAgain.valid"
  225. :message="passwordAgain.message"
  226. />
  227. </transition>
  228. <button
  229. id="change-password-button"
  230. class="button is-success"
  231. @click="changePassword()"
  232. >
  233. Change password
  234. </button>
  235. </div>
  236. </div>
  237. <div
  238. class="reset-status-box"
  239. v-if="step === 4"
  240. key="4"
  241. >
  242. <i class="material-icons success-icon"
  243. >check_circle</i
  244. >
  245. <h2>Password successfully {{ mode }}</h2>
  246. <router-link
  247. class="button is-dark"
  248. to="/settings"
  249. ><i class="material-icons icon-with-button"
  250. >undo</i
  251. >Return to Settings</router-link
  252. >
  253. </div>
  254. <div
  255. class="reset-status-box"
  256. v-if="step === 5"
  257. key="5"
  258. >
  259. <i class="material-icons error-icon">error</i>
  260. <h2>
  261. Password {{ mode }} failed, please try again
  262. later
  263. </h2>
  264. <router-link
  265. class="button is-dark"
  266. to="/settings"
  267. ><i class="material-icons icon-with-button"
  268. >undo</i
  269. >Return to Settings</router-link
  270. >
  271. </div>
  272. </div>
  273. </transition-group>
  274. </div>
  275. </div>
  276. </div>
  277. <main-footer />
  278. </div>
  279. </template>
  280. <script>
  281. import Toast from "toasters";
  282. import { mapGetters, mapState } from "vuex";
  283. import MainHeader from "@/components/layout/MainHeader.vue";
  284. import MainFooter from "@/components/layout/MainFooter.vue";
  285. import InputHelpBox from "@/components/InputHelpBox.vue";
  286. import validation from "@/validation";
  287. export default {
  288. components: { MainHeader, MainFooter, InputHelpBox },
  289. props: {
  290. mode: {
  291. default: "reset",
  292. enum: ["reset", "set"],
  293. type: String
  294. }
  295. },
  296. data() {
  297. return {
  298. code: "",
  299. email: {
  300. value: "",
  301. hasBeenSentAlready: true,
  302. entered: false,
  303. valid: false,
  304. message: "Please enter a valid email address."
  305. },
  306. password: {
  307. value: "",
  308. visible: false,
  309. entered: false,
  310. valid: false,
  311. message:
  312. "Include at least one lowercase letter, one uppercase letter, one number and one special character."
  313. },
  314. passwordAgain: {
  315. value: "",
  316. visible: false,
  317. entered: false,
  318. valid: false,
  319. message: "This password must match."
  320. },
  321. step: 1
  322. };
  323. },
  324. computed: {
  325. ...mapGetters({
  326. socket: "websockets/getSocket"
  327. }),
  328. ...mapState({
  329. accountEmail: state => state.user.auth.email
  330. })
  331. },
  332. watch: {
  333. "email.value": function watchEmail(value) {
  334. if (!value) return;
  335. if (
  336. value.indexOf("@") !== value.lastIndexOf("@") ||
  337. !validation.regex.emailSimple.test(value)
  338. ) {
  339. this.email.message = "Please enter a valid email address.";
  340. this.email.valid = false;
  341. } else {
  342. this.email.message = "Everything looks great!";
  343. this.email.valid = true;
  344. }
  345. },
  346. "password.value": function watchPassword(value) {
  347. if (!value) return;
  348. this.checkPasswordMatch(value, this.passwordAgain.value);
  349. if (!validation.isLength(value, 6, 200)) {
  350. this.password.message =
  351. "Password must have between 6 and 200 characters.";
  352. this.password.valid = false;
  353. } else if (!validation.regex.password.test(value)) {
  354. this.password.message =
  355. "Include at least one lowercase letter, one uppercase letter, one number and one special character.";
  356. this.password.valid = false;
  357. } else {
  358. this.password.message = "Everything looks great!";
  359. this.password.valid = true;
  360. }
  361. },
  362. "passwordAgain.value": function watchPasswordAgain(value) {
  363. if (!value) return;
  364. this.checkPasswordMatch(this.password.value, value);
  365. }
  366. },
  367. mounted() {
  368. this.email.value = this.accountEmail;
  369. },
  370. methods: {
  371. togglePasswordVisibility(ref) {
  372. if (this.$refs[ref].type === "password") {
  373. this.$refs[ref].type = "text";
  374. this[ref].visible = true;
  375. } else {
  376. this.$refs[ref].type = "password";
  377. this[ref].visible = false;
  378. }
  379. },
  380. checkPasswordMatch(password, passwordAgain) {
  381. if (passwordAgain !== password) {
  382. this.passwordAgain.message = "This password must match.";
  383. this.passwordAgain.valid = false;
  384. } else {
  385. this.passwordAgain.message = "Everything looks great!";
  386. this.passwordAgain.valid = true;
  387. }
  388. },
  389. onInput(inputName) {
  390. this[inputName].entered = true;
  391. },
  392. submitEmail() {
  393. if (
  394. this.email.value.indexOf("@") !==
  395. this.email.value.lastIndexOf("@") ||
  396. !validation.regex.emailSimple.test(this.email.value)
  397. )
  398. return new Toast("Invalid email format.");
  399. if (!this.email.value) return new Toast("Email cannot be empty");
  400. this.email.hasBeenSentAlready = false;
  401. if (this.mode === "set") {
  402. return this.socket.dispatch("users.requestPassword", res => {
  403. new Toast(res.message);
  404. if (res.status === "success") this.step = 2;
  405. });
  406. }
  407. return this.socket.dispatch(
  408. "users.requestPasswordReset",
  409. this.email.value,
  410. res => {
  411. new Toast(res.message);
  412. if (res.status === "success") {
  413. this.code = ""; // in case: already have a code -> request another code
  414. this.step = 2;
  415. } else this.step = 5;
  416. }
  417. );
  418. },
  419. verifyCode() {
  420. if (!this.code) return new Toast("Code cannot be empty");
  421. return this.socket.dispatch(
  422. this.mode === "set"
  423. ? "users.verifyPasswordCode"
  424. : "users.verifyPasswordResetCode",
  425. this.code,
  426. res => {
  427. new Toast(res.message);
  428. if (res.status === "success") this.step = 3;
  429. }
  430. );
  431. },
  432. changePassword() {
  433. if (this.password.valid && !this.passwordAgain.valid)
  434. return new Toast("Please ensure the passwords match.");
  435. if (!this.password.valid)
  436. return new Toast("Please enter a valid password.");
  437. return this.socket.dispatch(
  438. this.mode === "set"
  439. ? "users.changePasswordWithCode"
  440. : "users.changePasswordWithResetCode",
  441. this.code,
  442. this.password.value,
  443. res => {
  444. new Toast(res.message);
  445. if (res.status === "success") this.step = 4;
  446. else this.step = 5;
  447. }
  448. );
  449. }
  450. }
  451. };
  452. </script>
  453. <style lang="scss" scoped>
  454. .night-mode {
  455. .label {
  456. color: var(--light-grey-2);
  457. }
  458. .skip-step {
  459. border: 0;
  460. }
  461. }
  462. h1,
  463. h2,
  464. p {
  465. margin: 0;
  466. }
  467. .content-wrapper {
  468. display: flex;
  469. flex-direction: column;
  470. align-items: center;
  471. }
  472. .container {
  473. padding: 25px;
  474. #title {
  475. color: var(--black);
  476. font-size: 42px;
  477. }
  478. .reset-status-box {
  479. display: flex;
  480. flex-direction: column;
  481. align-items: center;
  482. justify-content: center;
  483. height: 356px;
  484. h2 {
  485. margin-top: 10px;
  486. font-size: 21px;
  487. font-weight: 800;
  488. color: var(--black);
  489. text-align: center;
  490. }
  491. .success-icon {
  492. color: var(--green);
  493. }
  494. .error-icon {
  495. color: var(--dark-red);
  496. }
  497. .success-icon,
  498. .error-icon {
  499. font-size: 125px;
  500. }
  501. .button {
  502. margin-top: 36px;
  503. }
  504. }
  505. }
  506. .control {
  507. margin-bottom: 2px !important;
  508. }
  509. </style>