ResetPassword.vue 12 KB

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