ResetPassword.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  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 MainHeader from "@/components/layout/MainHeader.vue";
  270. import MainFooter from "@/components/layout/MainFooter.vue";
  271. import InputHelpBox from "@/components/InputHelpBox.vue";
  272. import validation from "@/validation";
  273. export default {
  274. components: { MainHeader, MainFooter, InputHelpBox },
  275. props: {
  276. mode: {
  277. default: "reset",
  278. enum: ["reset", "set"],
  279. type: String
  280. }
  281. },
  282. data() {
  283. return {
  284. code: "",
  285. email: {
  286. value: "",
  287. hasBeenSentAlready: true,
  288. entered: false,
  289. valid: false,
  290. message: "Please enter a valid email address."
  291. },
  292. password: {
  293. value: "",
  294. visible: false,
  295. entered: false,
  296. valid: false,
  297. message:
  298. "Include at least one lowercase letter, one uppercase letter, one number and one special character."
  299. },
  300. passwordAgain: {
  301. value: "",
  302. visible: false,
  303. entered: false,
  304. valid: false,
  305. message: "This password must match."
  306. },
  307. step: 1
  308. };
  309. },
  310. computed: {
  311. ...mapGetters({
  312. socket: "websockets/getSocket"
  313. }),
  314. ...mapState({
  315. accountEmail: state => state.user.auth.email
  316. })
  317. },
  318. watch: {
  319. "email.value": function watchEmail(value) {
  320. if (!value) return;
  321. if (
  322. value.indexOf("@") !== value.lastIndexOf("@") ||
  323. !validation.regex.emailSimple.test(value)
  324. ) {
  325. this.email.message = "Please enter a valid email address.";
  326. this.email.valid = false;
  327. } else {
  328. this.email.message = "Everything looks great!";
  329. this.email.valid = true;
  330. }
  331. },
  332. "password.value": function watchPassword(value) {
  333. if (!value) return;
  334. this.checkPasswordMatch(value, this.passwordAgain.value);
  335. if (!validation.isLength(value, 6, 200)) {
  336. this.password.message =
  337. "Password must have between 6 and 200 characters.";
  338. this.password.valid = false;
  339. } else if (!validation.regex.password.test(value)) {
  340. this.password.message =
  341. "Include at least one lowercase letter, one uppercase letter, one number and one special character.";
  342. this.password.valid = false;
  343. } else {
  344. this.password.message = "Everything looks great!";
  345. this.password.valid = true;
  346. }
  347. },
  348. "passwordAgain.value": function watchPasswordAgain(value) {
  349. if (!value) return;
  350. this.checkPasswordMatch(this.password.value, value);
  351. }
  352. },
  353. mounted() {
  354. this.email.value = this.accountEmail;
  355. },
  356. methods: {
  357. togglePasswordVisibility(ref) {
  358. if (this.$refs[ref].type === "password") {
  359. this.$refs[ref].type = "text";
  360. this[ref].visible = true;
  361. } else {
  362. this.$refs[ref].type = "password";
  363. this[ref].visible = false;
  364. }
  365. },
  366. checkPasswordMatch(password, passwordAgain) {
  367. if (passwordAgain !== password) {
  368. this.passwordAgain.message = "This password must match.";
  369. this.passwordAgain.valid = false;
  370. } else {
  371. this.passwordAgain.message = "Everything looks great!";
  372. this.passwordAgain.valid = true;
  373. }
  374. },
  375. onInput(inputName) {
  376. this[inputName].entered = true;
  377. },
  378. submitEmail() {
  379. if (
  380. this.email.value.indexOf("@") !==
  381. this.email.value.lastIndexOf("@") ||
  382. !validation.regex.emailSimple.test(this.email.value)
  383. )
  384. return new Toast("Invalid email format.");
  385. if (!this.email.value) return new Toast("Email cannot be empty");
  386. this.email.hasBeenSentAlready = false;
  387. if (this.mode === "set") {
  388. return this.socket.dispatch("users.requestPassword", res => {
  389. new Toast(res.message);
  390. if (res.status === "success") this.step = 2;
  391. });
  392. }
  393. return this.socket.dispatch(
  394. "users.requestPasswordReset",
  395. this.email.value,
  396. res => {
  397. new Toast(res.message);
  398. if (res.status === "success") {
  399. this.code = ""; // in case: already have a code -> request another code
  400. this.step = 2;
  401. } else this.step = 5;
  402. }
  403. );
  404. },
  405. verifyCode() {
  406. if (!this.code) return new Toast("Code cannot be empty");
  407. return this.socket.dispatch(
  408. this.mode === "set"
  409. ? "users.verifyPasswordCode"
  410. : "users.verifyPasswordResetCode",
  411. this.code,
  412. res => {
  413. new Toast(res.message);
  414. if (res.status === "success") this.step = 3;
  415. }
  416. );
  417. },
  418. changePassword() {
  419. if (this.password.valid && !this.passwordAgain.valid)
  420. return new Toast("Please ensure the passwords match.");
  421. if (!this.password.valid)
  422. return new Toast("Please enter a valid password.");
  423. return this.socket.dispatch(
  424. this.mode === "set"
  425. ? "users.changePasswordWithCode"
  426. : "users.changePasswordWithResetCode",
  427. this.code,
  428. this.password.value,
  429. res => {
  430. new Toast(res.message);
  431. if (res.status === "success") this.step = 4;
  432. else this.step = 5;
  433. }
  434. );
  435. }
  436. }
  437. };
  438. </script>
  439. <style lang="less" scoped>
  440. .night-mode {
  441. .label {
  442. color: var(--light-grey-2);
  443. }
  444. .skip-step {
  445. border: 0;
  446. }
  447. }
  448. h1,
  449. h2,
  450. p {
  451. margin: 0;
  452. }
  453. .content-wrapper {
  454. display: flex;
  455. flex-direction: column;
  456. align-items: center;
  457. }
  458. .container {
  459. padding: 25px;
  460. #title {
  461. color: var(--black);
  462. font-size: 42px;
  463. }
  464. .reset-status-box {
  465. display: flex;
  466. flex-direction: column;
  467. align-items: center;
  468. justify-content: center;
  469. height: 356px;
  470. h2 {
  471. margin-top: 10px;
  472. font-size: 21px;
  473. font-weight: 800;
  474. color: var(--black);
  475. text-align: center;
  476. }
  477. .success-icon {
  478. color: var(--green);
  479. }
  480. .error-icon {
  481. color: var(--dark-red);
  482. }
  483. .success-icon,
  484. .error-icon {
  485. font-size: 125px;
  486. }
  487. .button {
  488. margin-top: 36px;
  489. }
  490. }
  491. }
  492. .control {
  493. margin-bottom: 2px !important;
  494. }
  495. </style>